标签:
1+1
2
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 string ans(1501, ‘0‘); 6 7 void pl(const string& s, size_t& l) 8 { 9 int jw = 0; 10 for (size_t i = 0; i<l; ++i) 11 { 12 int x = (ans[i] - ‘0‘) + (s[i] - ‘0‘) + jw; 13 jw = 0; 14 if (x>9) 15 { 16 ans[i] = (x - 10) + ‘0‘; 17 ++jw; 18 } 19 else 20 ans[i] = x + ‘0‘; 21 } 22 while (jw) 23 { 24 int x = ans[l] - ‘0‘ + jw; 25 jw = 0; 26 if (x>9) 27 { 28 ++jw; 29 ans[l] = (x - 10) + ‘0‘; 30 ++l; 31 } 32 else 33 ans[l] = x + ‘0‘; 34 } 35 } 36 inline size_t max(const size_t& a, const size_t& b) 37 { 38 return a > b ? a : b; 39 } 40 int main() 41 { 42 unsigned int c; 43 string cc; 44 cc.clear(); 45 size_t len = 0; 46 while ((c=getchar())!=‘\n‘) 47 { 48 if (c != ‘+‘) 49 cc += c; 50 else 51 { 52 size_t p = cc.size(); 53 for (size_t i = 0; i<p / 2; ++i) 54 { 55 unsigned int temp = cc[i]; 56 cc[i] = cc[p - i - 1]; 57 cc[p - i - 1] = temp; 58 } 59 pl(cc, p); 60 len = max(len,p); 61 cc.clear(); 62 } 63 } 64 size_t p = cc.size(); 65 for (size_t i = 0; i<p / 2; ++i) 66 { 67 unsigned int temp = cc[i]; 68 cc[i] = cc[p - i - 1]; 69 cc[p - i - 1] = temp; 70 } 71 pl(cc, p); 72 for (string::reverse_iterator i = ans.rend() - len; i != ans.rend(); ++i) 73 cout << *i; 74 cout << endl; 75 return 0; 76 }
//解法一尚未调试完成仅供思路
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 int ans[1501],num[1501],ansl=0; 7 string str1; 8 void get(int x) 9 { 10 for(int i=0;i<x;i++) 11 { 12 ans[i]+=num[i]; 13 } 14 ansl=max(ansl,x-1); 15 for(int i=0;i<ansl;i++) 16 { 17 ans[i+1]+=ans[i]/10; 18 ans[i]%=10; 19 } 20 if(ans[ansl]>0)++ansl; 21 } 22 void add() 23 { 24 int i=0; 25 int x=str1.length(); 26 while(x--) 27 { 28 if(str1[x]==‘+‘) 29 { 30 get(i); 31 i=0; 32 memset(num,0,sizeof(num)); 33 } 34 else num[i++]=str1[x]-48; 35 } 36 get(i); 37 } 38 int main() 39 { 40 cin>>str1; 41 add(); 42 for(int i=ansl-1;i>=0;i--) 43 cout<<ans[i]; 44 return 0; 45 }
1+1-1
1
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int ans[1501],num[1501],ansl=0; string str1; void add(int x) { for(int i=0;i<x;i++) { ans[i]+=num[i]; } ansl=max(ansl,x-1); for(int i=0;i<ansl;i++) { ans[i+1]+=ans[i]/10; ans[i]%=10; } if(ans[ansl]>0)++ansl; } void jian(int x) { for(int i=0;i<x;i++) { if(ans[i]<num[i]) { ans[i]+=10; ans[i+1]--; } ans[i]-=num[i]; } } void get() { int i=0; int x=str1.length(); while(x--) { if(str1[x]==‘+‘) { add(i); i=0; memset(num,0,sizeof(num)); } else if(str1[x]==‘-‘) { jian(i); i=0; memset(num,0,sizeof(num)); } else num[i++]=str1[x]-48; } add(i); } int main() { cin>>str1; get(); for(int i=ansl-1;i>=0;i--) cout<<ans[i]; return 0; }
2^3+1
9
标签:
原文地址:http://www.cnblogs.com/gc812/p/5785048.html