不懂就问/本地和洛谷全过不去,求大佬给看看

P1957 口算练习题

Do_www @ 2022-12-06 20:02:17

using namespace std;
int main()
{
    stringstream stream;
    int n, a, b;
    cin >> n;
    char flag;
    char z[1000];
    string s;
    for (int i = 0; i <= n; i++) {
        cin >> s;
        if (s[0] >='a' && s[0] <= 'c')
        {
            flag = s[0];
            cin >> a >> b;
        }
        else {
            stream.clear();
            stream << s;
            stream >> a;
            cin >> b;
        }
        if (flag == 'a')
        {
            sprintf(z, "%d+%d=%d", a, b, a + b);
        }
        else if (flag == 'b')
        {
            sprintf(z, "%d-%d=%d", a, b, a - b);
        }
        else {
            sprintf(z, "%d*%d=%d", a, b, a * b);
        }
        cout << z << endl << strlen(z) << endl;
    }
    return 0;
}

by hjsxhst2022 @ 2022-12-06 20:30:35

@wry_123 不懂就问,为什么你不加头文件


by HUFT @ 2022-12-06 20:43:04

@wry_123

for (int i = 0; i < n; i++) {

他会循环n+1次


by Do_www @ 2022-12-06 20:44:22

@hjsxhst2022 没复制过来


by Do_www @ 2022-12-06 20:45:32

@HUFT 我改完了,但很奇怪,用devc++测试成功,但vs就报错


by hjsxhst2022 @ 2022-12-06 20:56:30

@wry_123 IDE


by Gjanuary @ 2022-12-13 12:51:02

我的本地测试也是没问题的结果0分,哪位帅哥帮我看看

#include<iostream>
#include<string.h>
using namespace std;
/*
    getline()中的结束符,结束后,结束符不放入缓存区;
    cin的结束符,结束后,结束符还在缓存区;   
    所以在使用 cin 后若要使用 getline() 必须要把前面cin遗留的结束符处理掉,
    解决方法为:在使用getline()之前,加入一行getchar()来处理cin留下的结束符

*/
int main(){
    int n;// 表示输入的行数
    cin>>n;
    // 去掉缓存区结束符 
    getchar();
    char a[n][13];
    for(int i=0;i<n;i++){
        cin.getline(a[i],13);
    }
    char op;
    int t1=0,t2=0;
    // 进行循环处理 
    for(int i=0;i<n;i++){
        int space = 0;
        int nums = 0;
        t1 = 0,t2 = 0;
        for(int j=0;j<strlen(a[i]);j++){
            //cout<<"行:"<<i<<"列:"<<j<<",值"<<a[i][j]<<endl; 
            if(a[i][j] == 'a'){op = 'a';j+=1;}// 加法 
            else if(a[i][j] == 'b'){op = 'b';j+=1;}// 减法 
            else if(a[i][j] == 'c'){op = 'c';j+=1;}// 乘法
            else{
                if(a[i][j]!=' ' && space == 0){
                    t1 = t1*10 + (a[i][j] - '0');
                    nums++;
                }else if(a[i][j]!=' ' && space == 1){
                    t2 = t2*10 + (a[i][j] - '0');
                    nums++;
                }else{
                    space++;
                }
            } 
        }   
        if(op == 'a'){
            cout<<t1<<"+"<<t2<<"="<<t1+t2<<endl;
            cout<<nums+2+to_string(t1+t2).length();
        }else if(op == 'b'){
            cout<<t1<<"-"<<t2<<"="<<t1-t2<<endl;
            cout<<nums+2+to_string(t1-t2).length();
        }else if(op == 'c'){
            cout<<t1<<"*"<<t2<<"="<<t1*t2<<endl;
            cout<<nums+2+to_string(t1*t2).length();
        }
        if(i != n-1){
            cout<<endl;
        }
    }   
    return 0;
}

|