hdu 1753 坑爹的高精度
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1753
wa了N次,惭愧啊
View Code
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 const int N=1000; 5 using namespace std; 6 int num[N]; 7 8 int main(){ 9 string str1,str2; 10 while(cin>>str1>>str2){ 11 string s1[2],s2[2],s3,s,s4; 12 int i=0,j=0,k=0,temp=0; 13 while((str1[i]!='.'&&i<str1.size())||(str2[j]!='.'&&j<str2.size())){ 14 if(str1[i]!='.'&&i<str1.size()){ 15 s1[0]+=str1[i]; 16 i++; 17 }else if(str2[j]!='.'&&j<str2.size()){ 18 s1[1]+=str2[j]; 19 j++; 20 } 21 } 22 i++,j++; 23 while(i<str1.size()||j<str2.size()){ 24 if(i<str1.size()){ 25 s2[0]+=str1[i]; 26 i++; 27 }else if(j<str2.size()){ 28 s2[1]+=str2[j]; 29 j++; 30 } 31 } 32 memset(num,0,sizeof(num)); 33 int len1=s2[0].size()-1; 34 int len2=s2[1].size()-1; 35 if(len1==-1){ 36 s=s2[1]; 37 }else if(len2==-1){ 38 s=s2[0]; 39 }else { 40 int len=len1<len2?len1:len2; 41 for(i=len,j=len,k=0;i>=0&&j>=0;i--,j--){ 42 temp+=s2[0][i]+s2[1][j]-96; 43 num[k++]=temp%10; 44 temp/=10; 45 num[k]=temp; 46 } 47 while(i>=0){ 48 temp+=s2[0][i--]-48; 49 num[k++]=temp%10; 50 temp/=10; 51 num[k]=temp; 52 } 53 while(j>=0){ 54 temp+=s2[1][j--]-48; 55 num[k++]=temp%10; 56 temp/=10; 57 num[k]=temp; 58 } 59 while(k>=0&&k>len&&num[k]==0)k--; 60 if(temp)k--; 61 while(k>=0){ 62 s+=num[k--]+48; 63 } 64 for(i=len+1;i<=len1;i++){ 65 s+=s2[0][i]; 66 } 67 for(i=len+1;i<=len2;i++){ 68 s+=s2[1][i]; 69 } 70 } 71 i=s.size()-1; 72 while(i>=0&&s[i]=='0')i--; 73 if(i<0)s4=""; 74 else { 75 for(k=0;k<=i;k++){ 76 s4+=s[k]; 77 } 78 } 79 memset(num,0,sizeof(num)); 80 len1=s1[0].size(),len2=s1[1].size(); 81 k=0; 82 for(i=len1-1,j=len2-1;i>=0&&j>=0;i--,j--){ 83 temp+=s1[0][i]+s1[1][j]-96; 84 num[k++]=temp%10; 85 temp/=10; 86 num[k]=temp; 87 } 88 while(i>=0){ 89 temp+=s1[0][i--]-48; 90 num[k++]=temp%10; 91 temp/=10; 92 num[k]=temp; 93 } 94 while(j>=0){ 95 temp+=s1[1][j--]-48; 96 num[k++]=temp%10; 97 temp/=10; 98 num[k]=temp; 99 } 100 while(k>=0&&num[k]==0)k--; 101 while(k>=0){ 102 s3+=num[k--]+48; 103 } 104 if(s3.size()==0)s3="0"; 105 if(s4.size()){ 106 cout<<s3<<"."<<s4<<endl; 107 }else 108 cout<<s3<<endl; 109 } 110 return 0; 111 }