策略:将实数转化成整数, 记录下小数的位数, 最后的时候将前导0和后导0去掉输出就好了
ac by :SWS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1063
代码:
#include<stdio.h> #include<string.h> int main() { char s[10]; int ans[250],n,i,j,d; while(scanf("%s %d",s,&n) == 2){ int len =strlen(s); //while(len>0&&s[len-1] == '0') len--; int dc = 0,temp = 0; //dc是统计小数的位数 i = 0; for(i = 0; i < len; i ++){ if(s[i] == '.'){ dc = len-i-1; continue; } else{ temp = temP*10+s[i]-'0'; } } // printf("%d %d..\n",temp,dc); memset(ans,sizeof(ans)); ans[0] = 1; //初始化为1 d = 1; for(j = 1; j <= n; j ++){ int c = 0; for(i = 0; i < d; i ++){ int s = ans[i]*temp+c; ans[i] = s%10; c = s/10; } while(c){ ans[++d-1] = c%10; c/=10; } } // for(i = 0; i < 250; i ++) // printf("%d ",ans[i]); i= 0; while(i< dc*n&&ans[i] == 0) i++; //去除小数点后面的无用0 这里有个条件i的范围是在小数点后面的 j = 249; while(j >= dc*n&&ans[j] == 0) j --; //去除前导0 // printf("%d %d\n",j); //d = j-dc*n; for(; j >=i; j --){ if(j == (dc*n)-1) //判断是不是要输出小数点 printf("."); printf("%d",ans[j]); } printf("\n"); } return 0; }