题目链接:https://www.luogu.com.cn/problem/P1957
眼高手低系列
1 #include <bits/stdc++.h> 2 using namespace std; 3 int len(int number) { //返回number这个数长度 4 int x = 0; //x是number这个数的长度 5 if (number == 0) { 6 return 1; 7 } 8 if (number > 0) 9 while (number) { 10 number /= 10; 11 x++; 12 } 13 if (number < 0) { 14 x = 1; //负号的长度也要算上 15 number = -number; //转为正数 16 while (number) { 17 number /= 10; 18 x++; 19 } 20 } 21 return x; 22 } 23 int main() { 24 int n; 25 cin >> n; 26 int a, b; //操作数 27 string c; //操作符号 28 char l_c; //当本次没输入操作符号时,用上一次输入的操作符号 29 while (n--) { 30 cin >> c; 31 int x; //x是长度 32 switch (c[0]) { 33 case 'a': //如果是+的话 34 cin >> a >> b; //读入操作数 35 cout << a << "+" << b << "=" << a + b << endl; 36 x = len(a + b) + 2; 37 l_c = c[0]; 38 break; 39 case 'b': 40 cin >> a >> b; 41 cout << a << "-" << b << "=" << a - b << endl; 42 x = len(a - b) + 2; 43 l_c = c[0]; 44 break; 45 case 'c': 46 cin >> a >> b; 47 cout << a << "*" << b << "=" << a * b << endl; 48 x = len(a * b) + 2; 49 l_c = c[0]; 50 break; 51 default: //此时字符串c存储的就是第一个操作数 52 cin >> b; //输入第二个操作数 53 a = 0; //然后把字符串c的值,转为int,存入a 54 int l = c.size(); 55 for (int i = 0; i < l; i++) { 56 a = a * 10 + c[i] - '0'; 57 } 58 switch (l_c) { 59 case 'a': 60 cout << a << "+" << b << "=" << a + b << endl; 61 x = len(a + b) + 2; 62 break; 63 case 'b': 64 cout << a << "-" << b << "=" << a - b << endl; 65 x = len(a - b) + 2; 66 break; 67 case 'c': 68 cout << a << "*" << b << "=" << a * b << endl; 69 x = len(a * b) + 2; 70 break; 71 default: 72 break; 73 } 74 break; 75 } 76 x = len(a) + len(b) + x; 77 cout << x << endl; 78 } 79 return 0; 80 }