40WA求助 感觉代码没问题啊
查看原帖
40WA求助 感觉代码没问题啊
711574
jingchuan_Hou楼主2022/4/7 19:35
#include <bits/stdc++.h>

using namespace std;

int n;//n道题 
string str;//输入的字符串 
char k;//接收一个运算符 
int x, y;//两个数
 
int main()
{
	cin >> n;//输入n道题 
	
	for (int i = 0; i < n; i++)
	{
		cin >> str;//输入一个字符串 (定义为字符串是因为有可能为数字) 
		if (str[0] == 'a' || str[0] == 'b' || str[0] == 'c')//判断是不是运算符 
		{
			k = str[0];
			cin >> x >> y;
		}
		else
		{
			x = 0;
			for (int i = 0; str[i] != '\0'; i++)
			{
				x += i * 10 + str[i];
			}
			cin >> y; 
		}
		
		int ans;//运算结果
		 
		if (k == 'a')
		{
			ans = x + y;
			cout << x << "+" << y << "=" << x + y << endl;
		}
		else if (k == 'b')
		{
			ans = x - y;
			cout << x << "-" << y << "=" << x - y << endl;
		}
		else if (k == 'c')
		{
			ans = x * y;
			cout << x << "*" << y << "=" << x * y << endl;
		}
		
		int cnt = 0;//记录数字个数
		 
		if (ans < 0) cnt++;//ans < 0 多一个负号 
		
		while(x != 0)
		{
			x = x / 10;
			++cnt;
		}
		while(y != 0)
		{
			y = y / 10;
			++cnt;
		}
		while(ans != 0)
		{
			ans = ans / 10;
			++cnt;
		}
		
		cout << 2 + cnt << endl;
	}
	
	return 0;
}
2022/4/7 19:35
加载中...