Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
一点心得(附代码)记输入的浮点数其小数点后位数为n,末尾无效0位数为m,譬如12.100,则n=3,m=2。按以上定义,进行r次幂后,n' = n*r, m' = m*r。输出时,n'的值用于确定小数点位置,m'的值用于避免输出无效0,以及判断整数(整数条件为n ==m) 代码如下: #include <stdio.h> #include <string.h> #include <math.h> #define SIZE 130 int main(){ int a[SIZE]; //99999^25约为10^124,因此用大小为130的数组表示计算结果 char str[10]; int r, n, cf; int k; int zero_cnt; //记录尾部无效0个数 while(EOF != scanf("%s%d", str, &r)){ for(int i = 0; i < SIZE; i++) a[i] = 0; zero_cnt = 0; for(int i = 5; str[i] != '.' && i >= 0; i--){ //计算zero_cnt if(str[i] == '0'){ zero_cnt++; } else break; } int pos = strchr(str, '.')-str; //pos为字符串中小数点的位置索引,5-pos即为小数位数 for(int i = pos; i <= 5; i++) //移除小数点,转为整数进行幂计算 str[i] = str[i+1]; sscanf(str, "%d", &n); for(int nn = n, i = 0; nn != 0; i++){ a[i] = nn%10; nn /= 10; } for(int i = 0; i < r-1; i++){ cf = 0; for(int j = 0; j < SIZE-1; j++){ a[j] = a[j]*n + cf; cf = a[j]/10; a[j] %= 10; } } for(k = SIZE-1; k >= 0; k--) //避免输出前导0 if(a[k] != 0) break; for(int i = k; i >= r*(5-pos); i--) //输出整数部分 printf("%d", a[i]); if(zero_cnt < 5-pos) //判断是否为整数 printf("."); for(int i = r*(5-pos)-1; i >= r*zero_cnt; i--) //输出小数部分 printf("%d", a[i]); printf("\n"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator