「洛谷」P1957 口算练习题
链接:https://www.luogu.com.cn/problem/P1957
知识点:
1、字符串转整型
1 int zhuan(string a){ 2 int ans1 = 0; 3 for(int i = a.size()-1;i>=0;i--){ 4 int h = a[i]-'0'; 5 ans1+=h*pow(10,a.size()-i-1); 6 } 7 return ans1; 8 }
2、整数位数
1 int total(int k){ 2 int tot = 0; 3 if(k<0||k==0){ 4 tot++; 5 }//负数和0特判 6 while(k){ 7 k/=10; 8 tot++; 9 } 10 return tot; 11 }
AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int total(int k){ 4 int tot = 0; 5 if(k<0||k==0){ 6 tot++; 7 } 8 while(k){ 9 k/=10; 10 tot++; 11 } 12 return tot; 13 } 14 int zhuan(string a){ 15 int ans1 = 0; 16 for(int i = a.size()-1;i>=0;i--){ 17 int h = a[i]-'0'; 18 ans1+=h*pow(10,a.size()-i-1); 19 } 20 return ans1; 21 } 22 string p; 23 int a,b,c; 24 int biao; 25 int main(){ 26 int n; 27 cin>>n; 28 for(int i = 1;i<=n;i++){ 29 cin>>p; 30 if(p=="a"){ 31 cin>>a>>b; 32 c = a+b; 33 cout<<a<<'+'<<b<<'='<<c<<endl; 34 cout<<total(a)+total(b)+total(c)+2<<endl; 35 biao = 0; 36 }else if(p=="b"){ 37 cin>>a>>b; 38 c = a-b; 39 cout<<a<<'-'<<b<<'='<<c<<endl; 40 cout<<total(a)+total(b)+total(c)+2<<endl; 41 biao = 1; 42 }else if(p=="c"){ 43 cin>>a>>b; 44 c = a*b; 45 cout<<a<<'*'<<b<<'='<<c<<endl; 46 cout<<total(a)+total(b)+total(c)+2<<endl; 47 biao = 2; 48 }else{ 49 a = bian(p); 50 cin>>b; 51 if(biao==0){ 52 c = a+b; 53 cout<<a<<'+'<<b<<'='<<c<<endl; 54 cout<<total(a)+total(b)+total(c)+2<<endl; 55 biao = 0; 56 }else if(biao==1){ 57 c = a-b; 58 cout<<a<<'-'<<b<<'='<<c<<endl; 59 cout<<total(a)+total(b)+total(c)+2<<endl; 60 }else{ 61 c = a*b; 62 cout<<a<<'*'<<b<<'='<<c<<endl; 63 cout<<total(a)+total(b)+total(c)+2<<endl; 64 biao = 2; 65 } 66 } 67 } 68 return 0; 69 }

浙公网安备 33010602011771号