1 条题解
-
0
自动搬运
来自洛谷,原作者为

Ahws_rwhy
真実はいつもひとつ! AH 目前在役Oier& 每日一%@dws_t7760搬运于
2025-08-24 23:13:47,当前版本为作者最后更新于2025-05-11 22:29:55,作者可能在搬运后再次修改,您可在原文处查看最新版自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多
以下是正文
呼,花了一个小时,总算调出来了。思路:
此题是一道模拟题,思路容易想到,因为不知道 中哪个是被擦掉的,所以分别对这四种情况进行分类讨论即可,具体讨论,题解区有大佬一一列举出来了,故不再累述。这篇题解主要讲的是如何将 字符数组分别转化为数字的,这里我用的是
stoi函数将字符数组转化为数字的,接着,关键的问题是我们如何将 四个字符数组中的内容(数字与符号)确定下来。首先我们来看个例子:
,显然 中的内容是
1, 中的内容是+, 中的内容是?, 中的内容是 。我们可以一个一个读入字符,并判断字符是数字还是符号还是?,如果第一个字符是?,我们就可以确定 中的内容是?,否则,我们可以将该字符统计在 数组中;如果读到了运算符,那么该符号必定是 中的内容,但要注意 的内容可能为?,我们要特判掉这种情况;如果 中有内容且该字符不是?且没有出现等号,我们可以将该字符统计在 数组中,如果 中有内容且该字符是?,那么将?添加进 数组,结束当前循环即可;如果出现了等号且给字符不是 ,将该字符添加在 数组中,如果出现了等号且给字符是?,那么将?添加进 数组,结束当前循环即可。其中有许多细节具体看代码:
while (scanf("%c", &x) != EOF && x != '\n') { t++; if (x == '=' && flagd == 0) { deng[0] = x; flagd = 1; continue; } if ((x == '+' || x == '-' || x == '*' || x == '/' || x == '?') && t != 1 && !flagop) { flagop = 1; op[0] = x; continue; } if (x == '?') { if (t == 1 && !flaga) { a[0] = '?'; flaga = 1; } else if (!flagb && !flagd) { b[0] = '?'; flagb = 1; } else if (!flagc && flagd) { c[0] = '?'; flagc = 1; } continue; } // 正确存储数字字符 if (!flagd && !flagop && !flagb) { ta[cnta++] = x; } else if (!flagd && flagop && !flagb) { b[cntb++] = x; } else if (flagd && !flagc) { tc[cntc++] = x; } }之后的操作就比较简单了,注意分类讨论即可。
int ans = atoi(c), b1 = atoi(b), a1 = atoi(a); // cout << ans << " " << b1 << " " << a1 << endl; // cout << c[0] << endl; // 计算结果 if (a[0] == '?') { if (op[0] == '+') cout << (ans - b1); else if (op[0] == '-') cout << (ans + b1); else if (op[0] == '/') cout << (ans * b1); else if (op[0] == '*') cout << (ans / b1); } else if (op[0] == '?') { if (a1 + b1 == ans) cout << "+"; else if (a1 - b1 == ans) cout << "-"; else if (a1 / b1 == ans && b1 != 0) cout << "/"; else if (a1 * b1 == ans) cout << "*"; } else if (b[0] == '?') { // cout << "NBBBB!" << endl; if (op[0] == '+') cout << (ans - a1); else if (op[0] == '-') cout << (a1 - ans); else if (op[0] == '*') cout << (ans / a1); else if (op[0] == '/') cout << (a1 / ans); } else if (c[0] == '?') { if (op[0] == '+') cout << (a1 + b1); if (op[0] == '-') cout << (a1 - b1); if (op[0] == '*') cout << (a1 * b1); else if (op[0] == '/') cout << (a1 / b1); }Java代码:import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); char[] arr=str.toCharArray(); int index=-1; for (int i = 0; i < arr.length; i++) { if(arr[i]=='?'){index=i;break;} } if(index==arr.length-1){ char op=' ';int a=-1; for (int i = 0; i <arr.length ; i++) { char c=arr[i]; if(c=='+'||c=='-'||c=='*'||c=='/'){ op= c; a=i; break; } } String A=str.substring(0,a); String B=str.substring(a+1,arr.length-2); int x=Integer.valueOf(A); int y=Integer.valueOf(B); if(op=='+'){ System.out.print(x+y); } else if (op=='-') { System.out.print(x-y); } else if (op=='*') { System.out.print(x*y); } else if (op=='/') { System.out.print(x/y); } } else if ((arr[index+1]>='0'&&arr[index+1]<='9')&&(arr[index-1]>='0'&&arr[index-1]<='9')) { String A=str.substring(0,index); int a=-1; for (int i = 0; i < arr.length; i++) { if(arr[i]=='='){a=i;break;} } String B=str.substring(index+1,a); String C=str.substring(a+1); int x=Integer.valueOf(A); int y=Integer.valueOf(B); int z=Integer.valueOf(C); if(x+y==z){ System.out.print("+"); } else if (x-y==z) { System.out.print("-"); } else if (x*y==z) { System.out.print("*"); } else if (x/y==z) { System.out.print("/"); } } else if (index==0) { char op=arr[1]; int a=-1; for (int i = 2; i <arr.length ; i++) { char c=arr[i]; if(c=='='){ a=i; break; } } String B=str.substring(2,a); String C=str.substring(a+1); int b=Integer.valueOf(B); int c=Integer.valueOf(C); if(op=='+'){ System.out.print(c-b); } else if (op=='-') { System.out.print(c+b); } else if (op=='*') { System.out.print(c/b); } else if (op=='/') { System.out.print(c*b); } }else { char op=' '; int d=-1; for (int i = 0; i < arr.length; i++) { char c=arr[i]; if(c<'0'||c>'9'){ op=c; d=i; break; } } String A=str.substring(0,d); String C=str.substring(d+3); int a=Integer.valueOf(A); int c=Integer.valueOf(C); if(op=='+'){ System.out.print(c-a); } else if (op=='-') { System.out.print(a-c); } else if (op=='*') { System.out.print(c/a); } else if (op=='/') { System.out.print(a/c); } } } }
- 1
信息
- ID
- 12079
- 时间
- 2000ms
- 内存
- 512MiB
- 难度
- 3
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者