测试都通过,read (ASCII 13), expected = 哪里的问题?

P1957 口算练习题

Lifenglifeng @ 2021-01-01 17:57:59

#include <bits/stdc++.h>

using namespace std;
string  nss[51];

int spacepos(string cc){
    int pos=0;
    for( int i=0;i<(int)cc.length();i++){
        if(cc[i]==32) {
            pos=i;
            break;
        }

    }
    return pos;

}
int changeNum(string cc){

int n=0;

for(int i=0;i<(int)cc.length();i++){

    n=n*10+cc[i]-'0';

}
return n;

}

int nsize(int num){
int sum=1;
while(num/10){
    sum++;
    num=num/10;
}
return sum;
}

void printResult(char ty,string cc,int t){
   string son1="";
   string son2="";

  if(t==0){
   int first=spacepos(cc);

   string temp="";

    temp=cc.substr(first+1,cc.length());

   int second=spacepos(temp);
    son1=temp.substr(0,second);
   son2=temp.substr(second+1,temp.length());

}
   if(t==1){
    int first=spacepos(cc);

    son1=cc.substr(0,first);
    son2=cc.substr(first+1,cc.length());
      }

    int r=0;
   if(ty=='a'){
      r =changeNum(son1)+changeNum(son2);
      cout<<son1<<'+'<<son2<<'='<<r;
      cout<<endl<<son1.length()+1+son2.length()+1+nsize(r);
       }
    if(ty=='b'){
        r =changeNum(son1)-changeNum(son2);
        int length=0;
        if(r<0)
        {
           length=nsize(r)+1;
        }
        else length=nsize(r);
         cout<<son1<<'-'<<son2<<'='<<r;
         cout<<endl<<son1.length()+1+son2.length()+1+length;

    }

    if(ty=='c'){

         r =changeNum(son1)*changeNum(son2);
            cout<<son1<<'*'<<son2<<'='<<r;
            cout<<endl<<son1.length()+1+son2.length()+1+nsize(r);

    }

}

int main()
{
   int n;

   char ty;//

   cin>>n;
   string str="\n";
   getline(cin,str);
   for(int i=0;i<n;i++){
     getline(cin,nss[i]);
   }
   ty=nss[0][0];

   printResult(ty,nss[0],0);

   if(n>1){
   cout<<endl;
    for(int i=1;i<n;i++){

        if(nss[i][0]>='a' &&nss[i][0]<='c'){

            ty=nss[i][0];
            printResult(ty,nss[i],0);
        }
        if(nss[i][0]>='0'&&nss[i][0]<='9'){

             printResult(ty,nss[i],1);

        }
if(i<n-1)
 cout<<endl;

    }

   }

    return 0;
}

by Lifenglifeng @ 2021-01-02 20:39:32

全部输入保存,然后再统一输出,cin getline 中的回车符吃掉 各个系统估计不同。 其实发现边输入边输出也是可以的。那样就简单了


|