文章
99
粉丝
120
获赞
8
访问
96.9k
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
cin >> s;
int sym = 1, flag = 1; //sym表示数字之前的符号,flag表示等号左右
int a = 0, b = 0;//系数,常数
int i = 0;
while (i < s.size()) {
if (s[i] == '=') {
sym = 1;
flag = -1;
}
else if (s[i] == '+')
sym = 1;
else if (s[i] == '-')
sym = -1;
else { //遇到数字或x
int t = 0;
while (i < s.length() && s[i] >= '0' && s[i] <= '9') {
t = 10 * t + (s[i] - '0');
i++;
}
if (s[i] == 'x' && t == 0) {
a += sym * flag;
}
else if (s[i] == 'x')
a += t * sym * flag;
else {
b += t * sym * flag;
continue; //直接进行下一次循环是因为:上面while循环中处理数字之后已经++i
}
}
i++;
}
if (a == 0 && b == 0) {
printf("infinite solutions\n");
}
else if (a != 0 && b % a == 0) {
printf("x=%d\n", -b / a);
}
else {
printf("no solution\n");
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发