P1040 表达式计算
时间: 1000ms / 空间: 131072KiB / Java类名: Main
描述
给出一个表达式,其中运算符仅包含+,要求求出表达式的最终值
输入格式
仅一行,即为表达式
输出格式
仅一行,既为表达式算出的结果
测试样例1
输入
1+1
输出
2
备注
表达式总长度<=1500

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())!=' ') 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 }
P1041 表达式计算2
时间: 1000ms / 空间: 131072KiB / Java类名: Main
描述
给出一个表达式,其中运算符仅包含+,-,要求求出表达式的最终值
保证数据中不会出现负数,并且同时保证,如果你按从左到右的顺序计算,同样也不会出现负数的情况。
保证数据中不会出现负数,并且同时保证,如果你按从左到右的顺序计算,同样也不会出现负数的情况。
输入格式
仅一行,即为表达式
输出格式
仅一行,既为表达式算出的结果
测试样例1
输入
1+1-1
输出
1
备注
表达式总长度<=255
表达式中数字位数<=255
表达式中数字位数<=255

#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; }
P1042 表达式计算3
时间: 1000ms / 空间: 131072KiB / Java类名: Main
描述
给出一个表达式,其中运算符仅包含+,-,*,/,^要求求出表达式的最终值
在这里,"/"为整除
最终结果为正整数,数据保证不需要使用高精度!
在这里,"/"为整除
最终结果为正整数,数据保证不需要使用高精度!
输入格式
仅一行,即为表达式
输出格式
仅一行,既为表达式算出的结果 结果小于maxlongint,且整个计算的过程中,也不会超过maxlongint
测试样例1
输入
2^3+1
输出
9
备注
表达式总长度<=20

#include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long using namespace std; int n,top=1,top2=1; char ch[105]; int opt[105],num[105]; int opt2[105],num2[105]; int main() { scanf("%s",ch+1); n=strlen(ch+1); for(int i=1;i<=n;i++) if(ch[i]>='0'&&ch[i]<='9')num[top]=num[top]*10+ch[i]-'0'; else { top++; if(ch[i]=='+')opt[top]=1; if(ch[i]=='-')opt[top]=2; if(ch[i]=='*')opt[top]=3; if(ch[i]=='/')opt[top]=4; if(ch[i]=='^')opt[top]=5; } for(int i=2;i<=top;i++) if(opt[i]==5) for(int j=1;j<num[i];j++) { opt2[++top2]=3; num2[top2]=num[i-1]; } else opt2[++top2]=opt[i],num2[top2]=num[i]; int tmp=num[1],ans=0; for(int i=2;i<=top2;i++) { if(opt2[i]==1)ans+=tmp,tmp=num2[i]; if(opt2[i]==2)ans+=tmp,tmp=-num2[i]; if(opt2[i]==3)tmp*=num2[i]; if(opt2[i]==4)tmp/=num2[i]; } printf("%d ",ans+tmp); return 0; }
先预处理把乘方变成乘法,然后从左到右计算
用一个临时的变量tmp来处理运算的优先级,即把一连串的乘除一起算完,再考虑加入答案
遇到一个加减符号就把tmp记入答案,tmp重新赋值
否则用tmp进行运算
//orz hzwer