Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.
This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.
Output
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592 9
98.999 10
1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
C++
while(cin>>s>>n)
{
...
}
c
while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want
/*while(scanf(%s%d",s,&n)!=EOF) //this also work */
{
...
}
Source
East Central North America 1988
#include <stdio.h>
#include <stdlib.h>
char* left; // 左操作数
char* right; // 右操作数
int ll; // 左操作数长度
int rl; // 有操作数长度
int ldp; // 做操作数小数位数
int rdp; // 右操作数小数位数
char* result; // 保存结果的全局变量
int length; // 数据的长度
int dp; // 小数位数
void deal(char* s, int n);
void print();
void calculate();
void calculate0(int i, int j);
void update(int remain, int index);
int main(int argc, char *argv[])
{
int n;
char* s = (char*)malloc(7);
while (scanf("%s%d", s, &n) == 2)
deal(s, n);
system("PAUSE");
return 0;
}
void deal(char* s, int n)
{
int sp=0, i, j, k;
char num[6];;
// 计算小数位数
for (j=5; j>=0; j--)
if (s[j] == '.')
break;
else
sp++;
// 将实数转化成整数,保存在num字符数组中
for (i=0; i<5; i++)
{
num[i] = 5-sp-i>0 ? s[i]:s[i+1];
num[i] = num[i] - '0';
}
num[5] = '\0';
// 装在左操作符
free(left);
left = (char*)malloc(6);
for (i=0; i<6; i++)
left[i] = num[i];
ll = 5;
ldp = sp;
// 装在右操作符
free(right);
right = (char*)malloc(6);
for (i=0; i<6; i++)
right[i] = num[i];
rl = 5;
rdp = sp;
// 装在初始结果
free(result);
result = (char*)malloc(6);
for (i=0; i<6; i++)
result[i] = num[i];
length = 5;
dp = sp;
for (k=0; k<n-1; k++)
{
calculate(); // 计算表达式
// printf("The %d time:\n", k);
// print(); // 打印当前结果
// 重新装载操作符
free(left);
left = (char*)malloc(length + 1);
for (i=0; i<length+1; i++)
left[i] = result[i];
ll = length;
ldp = dp;
}
print();
}
void calculate()
{
free(result);
result = (char*)malloc(ll + rl + 1);
int i, j;
for (i=0; i<ll+rl; i++)
result[i] = 0;
result[ll+rl] = '\0';
length = 0;
dp = ldp + rdp;
for (i=0; i<ll; i++)
for (j=0; j<rl; j++)
calculate0(i, j);
result[++length] = '\0';
}
void calculate0(int i, int j)
{
int x = ((int)left[i]) * ((int)right[j]);
// 计算结果保存位置
int temp = result[i+j+1] + x;
length = i+j+1;
result[length] = temp % 10;
temp /= 10;
update(temp, length - 1);
}
void update(int remain, int index)
{
int temp = remain;
while (index >= 0 && temp)
{
temp += result[index];
result[index] = temp % 10;
temp /= 10;
index--;
}
}
void print()
{
char* temp = (char*)malloc(length+1);
int i;
for (i=0; i<length+1; i++)
temp[i] = result[i] + '0';
int maxL = (dp > length) ? dp : length;
maxL++;
char* show = (char*)malloc(maxL + 1);
show[maxL] = '\0'; // 置字符串结束符
for (i=maxL-1; i>=0; i--)
{
if (dp == maxL - i - 1)
{
show[i] = '.'; // 置小数点
} else if (dp > maxL - i - 1)
{
show[i] = temp[length-maxL+i];
} else {
// 需要判断数组是否越界
show[i] = length-maxL+i+1<0 ? '0' : temp[length-maxL+i+1];
}
}
// 去除小数部分多余的尾数
int tail = maxL;
while (show[--tail] == '0');
show[show[tail]=='.' ? tail : tail+1] = '\0'; // 置字符串结束符
// 去除整数部分多余的尾数
int begin = -1;
while (show[++begin] == '0');
printf("%s\n", show+begin);
}
分享到:
相关推荐
poj 1001 Exponentiation用字符串操作的
In several cryptographic systems, a fixed element g of a group of order N is repeatedly raised to many diff erent powers. In this paper we present a practical method of speeding up such systems, using...
实现数论里的模N情况下的幂指数乘法。int modular_exponentiation(int a,int b,int n),供参考
业余爱好。所以,算法不一定好,CODING也不一定佳,效率不一定高,只是能通过online judge而已。
如题所示,亲测可用。求高精度幂,不会的同学可以参考下,会做的同学可以给挑挑毛病!大家以代码会友!
A basic but expensive operation in implementations of several famous public-key cryptosystems is the computation of the multi-exponentiation in a certain finite multiplication group. In 2007, Yang et ...
快速幂
In 2009, Wu proposed a fast modular exponentiation algorithm and claimed that the proposed algorithm on average saved about 38.9% and 26.68% of single-precision multiplications as compared to Dussé–...
matlab运算符代码求幂运算符 地位 第三阶段 实施进度 Traceur 巴别塔 V8() 蜘蛛猴 () 作者 里克·沃尔德隆(Rick Waldron) 克洛德·帕奇(Claude Pache) 布伦丹·艾希(Brendan Eich) ...**
快速指数藻类 Mehmat SFedU(2013)的1个治安法官课程。 组中的快速求幂方法。 老师:Mayevsky Alexey Eduardovich 常用的取幂方法 从左到右无符号: Veremeenko Artyom 从右到左无符号: Pozdnyakov Alexey ...
This article proposes a novel nonlinear network code in the GF(2m) finite field. Different from previous linear network codes that linearly mix multiple input flows,the proposed nonlinear network code...
求幂Возведениевстепень
假设已知,试说明如何运用过程MODULAR-EXPONENTIATION计算对任意
简单的快速幂 Fast-Exponentiation 树状数组 Fenwick-Tree 所有结点对之间的最短路径(Floyd) Floyd-Warshall 凸包算法(Graham扫描法) Graham-Scan 辗转相除法求最大公约数 Greatest-Common-Divisor 堆排序 ...
Fast exponentiation calculation, the program includes: binary expansion successive squares the product of the squares for which the corresponding bit is a binary Development
Lua can handle subtraction, negative numbers, numbers with decimal points, multiplication (using *), division (using /), exponentiation (using ^), and combinations of these. Here are some examples > ...
This time our task is to write a program that imitates a simple desk calculator .Our calculator can accept an infix expression which ...(, ), +, -, *, /, % and ^(exp exponentiation operator, a^b= a b ).
大整数库 用C ++编写的Big Integer库,用于执行基本的算术运算 先决条件 g ++版本4.8.4或更高版本 Boost版本1.63.0或更高版本 python 2.7.6或更高版本 正在安装 首先通过以下方式在本地系统中编译项目: make ...
gradient descent 梯度下降 normal equations linear algebra 线性代数 superscript上标 exponentiation 指数 training set 训练集合 training example 训练样本 hypothesis 假设,用来表示学习算法的输出 LMS ...