功在不舍 @ 2022-07-03 22:37:03
为什么在洛谷平台上cin.getline()会读入一个\n到字符串里,本地就不会
不把len-1就会全WA
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
char last;
char s[1000];
int calc(char* x,int len){
int js=0;
for(int i=0;i<len;i++)
if(x[i]==' ')js++;
return js;
}
int calc_1(char *x,int len){
for(int i=0;i<len;i++)
if(x[i]==' ')return i;
}
int calc_2(char *x,int len){
for(int i=2;i<len;i++)
if(x[i]==' ')return i;
}
int transform(char *x,int start,int end){
int ans1=0;
for(int i=start;i<=end;i++)
ans1=ans1*10+x[i]-'0';
return ans1;
}
int calc_len(int x){
int ans=0;
if(x==0)return 1;
if(x<0){
ans=1;
x=-x;
}
while(x!=0){
x=x/10;
ans++;
}
return ans;
}
int main(){
cin.getline(s,1000);
int l=strlen(s);
n=transform(s,0,l-2);
for(int i=0;i<n;i++){
cin.getline(s,1000);
int len=strlen(s)-1;
int judge=calc(s,len);
if(judge==1){
int pos=calc_1(s,len);
int first=transform(s,0,pos-1);
int second=transform(s,pos+1,len-1);
int ans=0;
if(last=='a'){
ans=first+second;
cout<<first<<"+"<<second<<"="<<ans<<endl;
}
if(last=='b'){
ans=first-second;
cout<<first<<"-"<<second<<"="<<ans<<endl;
}
if(last=='c'){
ans=first*second;
cout<<first<<"*"<<second<<"="<<ans<<endl;
}
cout<<len+1+calc_len(ans)<<endl;
}
if(judge==2){
last=s[0];
int pos=calc_2(s,len);
int first=transform(s,2,pos-1);
int second=transform(s,pos+1,len-1);
int ans=0;
if(s[0]=='a'){
ans=first+second;
cout<<first<<"+"<<second<<"="<<ans<<endl;
}
if(s[0]=='b'){
ans=first-second;
cout<<first<<"-"<<second<<"="<<ans<<endl;
}
if(s[0]=='c'){
ans=first*second;
cout<<first<<"*"<<second<<"="<<ans<<endl;
}
cout<<len-1+calc_len(ans)<<endl;
}
}
return 0;
}
by 8atemak1r @ 2022-07-03 22:38:22
@功在不舍 本地 windows 但是评测机是 Linux 的问题吧,最好不要用 getline
by 功在不舍 @ 2022-07-03 22:39:06
@8atemak1r 哦可能还真是这样我是windows