全WA求助

P1957 口算练习题

TaamZeonhei @ 2021-10-10 17:57:53

第一个样本点输出结果与答案一致却还是WA怎么办?

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
    int ws(int a);
    int n;
    cin>>n;
    string line[n+1];
    int state,k=0;//k means the state is kept
    int x=0;
    int y=0;
    int gap;
    for(int i=0;i<n+1;i++)
    {
        getline(cin,line[i]);
    }
    for(int i=0;i<n+1;i++)
    {
        switch(line[i][0])
        {
            case 'a':state=1;k=0;break;
            case 'b':state=2;k=0;break;
            case 'c':state=3;k=0;break;
            default: k=1;
        }
        for(int ii=0;;ii++)
        {
            if(line[i][line[i].size()-1-ii]!=' ') y+=(line[i][line[i].size()-1-ii]-48)*pow(10,ii);
            else break;
            gap=(line[i].size()-1-ii)-1;
        }
        for(int ii=0;;ii++)
        {
            if(line[i][gap-1-ii]!=' '&&gap-1-ii!=-1) x+=(line[i][gap-1-ii]-48)*pow(10,ii);
            else break;
        }
        switch(state)
        {
            case 1:
                cout<<x<<'+'<<y<<'='<<x+y<<endl;
                cout<<ws(x)+ws(y)+ws(x+y)+2<<endl;
                break;
            case 2:
                cout<<x<<'-'<<y<<'='<<x-y<<endl;
                cout<<ws(x)+ws(y)+ws(x-y)+2<<endl;
                break;
            case 3:
                cout<<x<<'*'<<y<<'='<<x*y<<endl;
                cout<<ws(x)+ws(y)+ws(x*y)+2<<endl;
                break;
        }
        x=0;
        y=0;
    }
    return 0;
}
int ws(int a)
{
    int b=1;
    if(a<0) b++;
    for(;;)
    {
        a/=10;
        if(a==0) break;
        else b++;
    }
    return b;
}

by Shen_Linwood @ 2021-10-10 19:20:44

@TaamZeonhei 你的代码有几个地方比较危险


by Shen_Linwood @ 2021-10-10 20:50:01

最好不要用变量定义数组的大小 string line[n+1];改成string line[52];


by Shen_Linwood @ 2021-10-10 21:05:03

@TaamZeonhei 另外 ws()应该放在main()的前面 https://www.cnblogs.com/jiading/p/10799791.html


by Shen_Linwood @ 2021-10-10 21:07:42

咦 好像int型可以过


by TaamZeonhei @ 2021-10-22 15:30:08

@shenlw 此话怎讲?


by Shen_Linwood @ 2021-10-22 15:33:37

@TaamZeonhei 你可以看一看这个啊https://www.cnblogs.com/jiading/p/10799791.html
另:你的反应有亿点迟钝


by Shen_Linwood @ 2021-10-22 15:35:09

然后就看不出来了


by Shen_Linwood @ 2021-10-22 15:35:48

如果需要 也要等CSP考完之后了


by TaamZeonhei @ 2021-10-29 15:38:04

@shenlw 我是说怎样用int型过。 ps:把函数放在前面也还是不过。


by Shen_Linwood @ 2021-10-30 15:03:21

@TaamZeonhei az


| 下一页