社区讨论

求调RE/TLE

P1022[NOIP 2000 普及组] 计算器的改良参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@m4yzzxl2
此快照首次捕获于
2024/12/22 10:37
去年
此快照最后确认于
2025/11/04 12:30
4 个月前
查看原帖
使用 getchar() 读入字符,本地可正常运行,提交洛谷全 RE。 代码:
CPP
#include<bits/stdc++.h>
using namespace std;
struct Node{
    int f,num;
    bool dir,alpha;
}terms[110];
int i,j,s,l,r;
double x;
char alpha;
void input(){   // 输入
    char c=getchar();
    while(c!='='){  // =前
        if(c=='-') terms[++i].f=-1,c=getchar(); // 符号
        else if(c=='+') terms[++i].f=1,c=getchar();
        else terms[++i].f=1;
        int t=0;    // 数字
        while(isdigit(c)){
            t=t*10+c-48;
            c=getchar();
        }
        terms[i].num=t;
        if(isalpha(c)) terms[i].alpha=1,alpha=c,c=getchar();    // 字母
        else terms[i].alpha=0;
        terms[i].dir=0;
    }
    c=getchar();
    while(c!='\n'){ // =后
        if(c=='-') terms[++j+i].f=-1,c=getchar();   // 符号
        else if(c=='+') terms[++j+i].f=1,c=getchar();
        else terms[++j+i].f=1;
        int t=0;    // 数字
        while(isdigit(c)){
            t=t*10+c-48;
            c=getchar();
        }
        terms[i+j].num=t;
        if(isalpha(c)) terms[i+j].alpha=1,alpha=c,c=getchar();  // 字母
        else terms[i+j].alpha=0;
        terms[i+j].dir=1;
    }
}
void move(){    // 移项
    for(int _=1;_<=i+j;_++){
        if(terms[_].alpha&&terms[_].dir||!terms[_].alpha&&!terms[_].dir){
            terms[_].dir^=1;
            terms[_].f*=-1;
        }
    }
}
void combine(){ // 合并
    for(int _=1;_<=i+j;_++){
        if(terms[_].alpha) l+=terms[_].f*terms[_].num;
        else r+=terms[_].f*terms[_].num;
    }
}
void div(){ // 系数化为1
    x=1.0*r/l;
}
int main(){
    input();
    move();
    combine();
    div();
    cout<<alpha<<"="<<fixed<<setprecision(3)<<x;
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...