|
http://acm.pku.edu.cn/JudgeOnline/problem?id=1001
求高精度幂的题目,题目的难度在于处理如同 .00010 的输入
和输出的前导后导零的问题,还有小数点的位置可能在处理输入时候被影响 写了好久代码,但是同学用JAVA只需要一会就能搞定了,郁闷... 附上AC代码:
1Source Code 2 3Problem: 1001 User: hongtaozhy 4Memory: 304K Time: 0MS 5Language: G++ Result: Accepted 6 7Source Code 8#include<stdio.h> 9#include<string.h> 10#include<math.h> 11void reverse(char*str); 12void multi(char*num1,char*num2,char*result); 13 char a[100]; 14 char str[100]; 15 char sum[200]; 16 char tem[200]; 17 char tem2[100]; 18int main(){ 19 int flag; 20 int n; 21 int key; 22 int flag2; 23 int kk; 24//freopen("a.txt","r",stdin); 25//freopen("a2.txt","w",stdout); 26 while(scanf("%s%d",a,&n)==2){ 27 memset(str,0,sizeof(str)); 28 kk=0; 29 flag=-1; 30 flag2=6; 31 int g=0; 32 int j=0; 33 int t = 0; 34 if(a[0]=='.'){ g=1;} 35 for(int i = 0 ; i < 6 ; i++){ 36 if(a[i]=='0'&&kk==0)continue; 37 kk=1; 38 a[t++]=a[i]; 39 } 40 a[t]='\0'; 41 42 if(a[0]=='.') { 43 for(int i =strlen(a);i>=0;i--) 44 a[i+1]=a[i]; 45 a[0]='0'; 46 }{ 47 int i; 48 for(i=0 ; i < strlen(a);i++ ) 49 if(a[i]=='.') break; 50 if(i==strlen(a)) {a[i]='.';a[i+1]='0';a[i+2]='\0';}} 51 52 for(int i=0;i<strlen(a);i++) 53 if(a[i]=='.') { flag=i;kk=1;} 54 else { 55 if(a[i]=='0'&&flag!=-1&&flag2==strlen(a)) flag2=i; 56 if(a[i]!='0'&&flag!=-1) flag2=strlen(a); 57 str[j++]=a[i]; 58 } 59 60 int ne=strlen(a)-flag-1; 61 for(j = strlen(str)-1 ; j >= 0 ; j-- ){ 62 if(str[j] == '0'&&ne--){ 63 str[j]='\0'; 64 } 65 else break; 66 } 67 flag=flag2-flag-1; 68 //接入结束 69 if(g==1) 70 printf("."); 71 memset(sum,0,sizeof(sum)); 72 memset(tem,0,sizeof(tem)); 73 memset(tem2,0,sizeof(tem2)); 74 key=0; 75 sum[0]='1'; 76 sum[1]='\0'; 77 for(int i=0; i < n ; i++) 78 { 79 if(key!=1){ 80 key=1; 81 strcpy(tem2,str); 82 multi(sum,tem2,tem); 83 memset(sum,0,sizeof(sum)); 84 memset(tem2,0,sizeof(tem2)); 85 } 86 else{ 87 strcpy(tem2,str); 88 key=0; 89 multi(tem,tem2,sum); 90 memset(tem,0,sizeof(tem)); 91 memset(tem2,0,sizeof(tem2)); 92 } 93 } 94 if(key==1){ 95 for(int i = 0 ; tem[i] != '\0' ; i++ ){ 96 if( tem[i] == '0' && i == 0 ) continue; 97 if(i==strlen(tem)-(flag*n)&&g!=1) printf("."); 98 printf("%c",tem[i]); 99 } 100 } 101 else{ 102 for(int i = 0 ; sum[i] != '\0' ; i++ ){ 103 if( sum[i] == '0' && i == 0 ) continue; 104 if(i==strlen(sum)-(flag*n)&&g!=1) printf("."); 105 printf("%c",sum[i]); 106 } 107 } 108 printf("\n"); 109 } 110return 0; 111} 112void multi(char*num1,char*num2,char*result) 113{ 114 int i,j,len1,len2,len; 115 116 len1=strlen(num1); 117 len2=strlen(num2); 118 reverse(num2); 119 reverse(num1); 120 121 for (i=0;i<len1;i++) 122 num1[i] -= '0'; 123 for (i=0;i<len2;i++) 124 num2[i] -= '0'; 125 126 for (i=0;i<len2;i++) 127 for (j=0;j<len1;j++) 128 { 129 len=i+j; 130 result[len] += num2[i]*num1[j]; 131 result[len+1] += result[len]/10; 132 result[len] %= 10; 133 } 134 len=len1+len2-1; 135 for (i=0;i<len;i++) 136 result[i] += '0'; 137 if (result[len]) result[len] += '0'; 138 reverse(result); 139} 140 141void reverse(char*str) 142{ 143 int i; 144 char c; 145 for (i=0;i<strlen(str)/2;i++) 146 { 147 c=str[i]; 148 str[i]=str[strlen(str)-i-1]; 149 str[strlen(str)-i-1]=c; 150 } 151} 152 153 154
|