20分代码,求c++大神

P2788 数学1(math1)- 加减算式

。。。
by Out_Land @ 2017-08-27 15:15:24


我是P党,目测你的代码没问题,也许是细节,自己多出几组数据测测~~ P代码(自己刚手打的,没优化,见谅): ```cpp program std; var s:ansistring; s1:string; i,j,k,t:longint; begin readln(s); t:=1; for i:=1 to length(s) do if (s[i]<='9') and (s[i]>='0') then s1:=s1+s[i] else begin val(s1,j); s1:=''; k:=k+j*t; if s[i]='+' then t:=1 else t:=-1; end; val(s1,j); k:=k+j*t; writeln(k); end. ```
by Out_Land @ 2017-08-27 15:29:29


```cpp #include<bits/stdc++.h> using namespace std; int main(){ long long a=0,flag=1,i,sum=0; string s; cin>>s; s="+"+s; for(i=0;i<s.size();i++) { if(s[i]=='+') { if(flag==1)sum+=a; if(flag==0)sum-=a; a=0;flag=1; continue; } if(s[i]=='-'){ if(flag==1)sum+=a; if(flag==0)sum-=a; a=0;flag=0; continue; } a=a*10+(s[i]-'0'); } if(flag==1)sum+=a; else sum-=a; cout<<sum; } ``` xxs
by AK_CSP_is_easy @ 2021-11-26 21:09:45


Python2:print(input())
by AK_CSP_is_easy @ 2021-11-26 21:10:59


c++98: ```cpp #include<bits/stdc++.h> using namespace std; int main() { int a,s=0; while(cin>>a) s+=a; cout<<s; } ```
by AK_CSP_is_easy @ 2021-11-26 21:14:09


```c #include <bits/stdc++.h> #define LL long long const int N=10000+5; using namespace std; int n,m; stack<int> s1; stack<char> s2; int lev(char x){ if(x=='+'||x=='-')return 1; if(x=='*'||x=='/')return 2; if(x=='^')return 3; return 0; } void calculate(stack<int> &s1,stack<char> &s2){ int y=s1.top();s1.pop(); int x=s1.top();s1.pop(); char z=s2.top();s2.pop(); if(z=='+')s1.push(x+y); if(z=='-')s1.push(x-y); if(z=='*')s1.push(x*y); if(z=='/')s1.push(x/y); if(z=='^')s1.push(pow(x,y)); } int c(int x){return x!=0;} char str[1000000]; int sum[1000000]; int main(){ scanf("%s",str+1); n=strlen(str+1); for(int i=1;i<=n;i++){ sum[i]+=sum[i-1]; if(str[i]=='(')sum[i]++; if(str[i]==')')sum[i]--; } bool out=false; for(int i=2;i<=n;i++) if( c(lev(str[i])) && c(lev(str[i-1])) ){ out=1; break; } if(( n==1 && c(lev(str[1])) )||sum[n]||out){ cout<<"NO"<<endl; return 0; } stack<int> s1; stack<char> s2; int temp=0; bool flag=false; for(int i=1;i<=n;i++){ if('0'<=str[i]&&str[i]<='9'){ temp=(temp<<3)+(temp<<1)+str[i]-'0'; flag=true; }else{ if(flag){ s1.push(temp); temp=0; flag=false; }if(str[i]=='('){ s2.push(str[i]); continue; }if(str[i]==')'){ while(s2.top()!='(')calculate(s1,s2); s2.pop();continue; } while(!s2.empty()&&lev(s2.top())>=lev(str[i])) calculate(s1,s2); s2.push(str[i]); } } if(flag){ s1.push(temp); temp=0; flag=false; }while(!s2.empty())calculate(s1,s2); cout<<s1.top()<<endl; return 0; }//AC ``` 建议不要用
by shit007 @ 2022-05-28 19:24:05


|