奇怪RE
查看原帖
奇怪RE
209923
PRIMITIVE_LIGHTS楼主2021/1/1 18:38

为什么这题getline会全RE,cin就过了呢? RE:

#include<iostream>
#include<cstring>
using namespace std;
char ch;
void f(string word,int &c1,int &c2){
    int w,a1,b1;
    string b="";
    w=a1=b1=0;
    for(int i=0;i<word.size();i++){
        if(word[i]>='0'&&word[i]<='9'||b.empty()&&(word[i]=='+'||word[i]=='-'))b+=word[i];
        else{
            if(word[i]>='a'&&word[i]<='z'){
                ch=word[i];
                if(b=="+"||b=="") a1++;
                else if(b=="-") a1--;
                else a1+=stoi(b);
                b="";
            } 
            else {
                b1+=stoi(b);
                b="";
                b+=word[i];
            }
        }
    }
    if(!b.empty()) b1+=stoi(b);
    c1=a1;
    c2=b1;
} 
int ww1,ww2,www1,www2;
int main(){
     string a;
     getline(cin,a);
     f(a.substr(0,a.find('=')),ww1,www1);
     f(a.substr(a.find('=')+1),ww2,www2);
     int endww=ww1-ww2,endwww=www2-www1;
     double ans=endwww/(double)endww;
     if(ans==-0) ans=0;
     printf("%c=%.3lf",ch,ans);
} 

AC:

#include<iostream>
#include<cstring>
using namespace std;
char ch;
void f(string word,int &c1,int &c2){
    int w,a1,b1;
    string b="";
    w=a1=b1=0;
    for(int i=0;i<word.size();i++){
        if(word[i]>='0'&&word[i]<='9'||b.empty()&&(word[i]=='+'||word[i]=='-'))b+=word[i];
        else{
            if(word[i]>='a'&&word[i]<='z'){
                ch=word[i];
                if(b=="+"||b=="") a1++;
                else if(b=="-") a1--;
                else a1+=stoi(b);
                b="";
            } 
            else {
                b1+=stoi(b);
                b="";
                b+=word[i];
            }
        }
    }
    if(!b.empty()) b1+=stoi(b);
    c1=a1;
    c2=b1;
} 
int ww1,ww2,www1,www2;
int main(){
     string a;
     cin>>a;
     f(a.substr(0,a.find('=')),ww1,www1);
     f(a.substr(a.find('=')+1),ww2,www2);
     int endww=ww1-ww2,endwww=www2-www1;
     double ans=endwww/(double)endww;
     if(ans==-0) ans=0;
     printf("%c=%.3lf",ch,ans);
} 
2021/1/1 18:38
加载中...