- 【深基6.例4】口算练习题
ac代码
- @ 2026-5-23 16:20:17
#include<bits/stdc++.h>
using namespace std;
int wei(int x){
if(x==0) return 1;
int ans=0;
if(x<0) {
x=-x;
ans++;
}
while(x){
x/=10;
ans++;
}
return ans;
}
int z(string x){
int ans=0;
for(int i=0;i<x.size();i++){
if(x[i]=='-') continue;
ans=ans*10+x[i]-'0';
}
if(x[0]=='-') return ans*-1;
else return ans;
}
int t;
string last;
int main(){
cin>>t;
while(t--){
string s;
cin>>s;
if(s<"a"){
int x,y;
cin>>y;
x=z(s);
if(last=="a"){
cout<<x<<"+"<<y<<"="<<x+y<<endl;
cout<<wei(x)+wei(y)+wei(x+y)+2<<endl;
}
else if(last=="b"){
cout<<x<<"-"<<y<<"="<<x-y<<endl;
cout<<wei(x)+wei(y)+wei(x-y)+2<<endl;
}
else if(last=="c"){
cout<<x<<"*"<<y<<"="<<x*y<<endl;
cout<<wei(x)+wei(y)+wei(x*y)+2<<endl;
}
}
else if(s=="a") {
last=s;
int x,y;
cin>>x>>y;
cout<<x<<"+"<<y<<"="<<x+y<<endl;
cout<<wei(x)+wei(y)+wei(x+y)+2<<endl;
}
else if(s=="b") {
last=s;
int x,y;
cin>>x>>y;
cout<<x<<"-"<<y<<"="<<x-y<<endl;
cout<<wei(x)+wei(y)+wei(x-y)+2<<endl;
}
else if(s=="c") {
last=s;
int x,y;
cin>>x>>y;
cout<<x<<"*"<<y<<"="<<x*y<<endl;
cout<<wei(x)+wei(y)+wei(x*y)+2<<endl;
}
}
return 0;
}
1 条评论
-
-
#include <bits/stdc++.h> #define qwq(i,a,b) for(int i=(a);i<=(b);++i) #define qaq(i,a,b) for(int i=(a);i>=(b);--i) using namespace std; typedef long long ll; int main() { int n; cin >> n; string idx = ""; qwq (i, 1, n) { string op; cin >> op; string str = ""; if (op.size() == 1 && op[0] != '0' && op[0] != '1' && op[0] != '2' && op[0] != '3' && op[0] != '4' && op[0] != '5' && op[0] != '6' && op[0] != '7' && op[0] != '8' && op[0] != '9') { // 是运算类型 ll a, b; cin >> a >> b; idx = op; // 记录前一次运算类型 if (op == "a") { // 加法 ll c = a + b; str += to_string(a); str += '+'; str += to_string(b); str += '='; str += to_string(c); } else if (op == "b") { // 减法 ll c = a - b; str += to_string(a); str += '-'; str += to_string(b); str += '='; str += to_string(c); } else { // 乘法 ll c = a * b; str += to_string(a); str += '*'; str += to_string(b); str += '='; str += to_string(c); } } else { if (i == 1) idx = "a"; ll a = stoll(op), b; cin >> b; if (idx == "a") { // 加法 ll c = a + b; str += to_string(a); str += '+'; str += to_string(b); str += '='; str += to_string(c); } else if (idx == "b") { // 减法 ll c = a - b; str += to_string(a); str += '-'; str += to_string(b); str += '='; str += to_string(c); } else { // 乘法 ll c = a * b; str += to_string(a); str += '*'; str += to_string(b); str += '='; str += to_string(c); } } cout << str << '\n' << str.size() << '\n'; } }
- 1
信息
- ID
- 1702
- 时间
- ms
- 内存
- MiB
- 难度
- 6
- 标签
- 递交数
- 29
- 已通过
- 10
- 上传者