口算练习题
题目描述
王老师正在教简单算术运算。细心的王老师收集了i道学生经常做错的口算题,并且想整理编写成一份练习。 编排这些题目是一件繁琐的事情,为此他想用计算机程序来提高工作效率。王老师希望尽量减少输入的工作量,比如 5+8 \texttt{5+8} 5+8 的算式最好只要输入 5 \texttt 5 5 和 8 \texttt 8 8,输出的结果要尽量详细以方便后期排版的使用,比如对于上述输入进行处理后输出 5+8=13 \texttt{5+8=13} 5+8=13 以及该算式的总长度 6 6 6。王老师把这个光荣的任务交给你,请你帮他编程实现以上功能。
输入格式
第一行为数值 i i i
接着的 i i i 行为需要输入的算式,每行可能有三个数据或两个数据。
若该行为三个数据则第一个数据表示运算类型, a \texttt a a 表示加法运算, b \texttt b b 表示减法运算, c \texttt c c 表示乘法运算,接着的两个数据表示参加运算的运算数。
若该行为两个数据,则表示本题的运算类型与上一题的运算类型相同,而这两个数据为运算数。
输出格式
输出 2 × i 2\times i 2×i 行。对于每个输入的算式,输出完整的运算式及结果,第二行输出该运算式的总长度
样例 #1
样例输入 #1
4
a 64 46
275 125
c 11 99
b 46 64
样例输出 #1
64+46=110
9
275+125=400
11
11*99=1089
10
46-64=-18
9
提示
数据规模与约定
对于 50 % 50\% 50% 的数据,输入的算式都有三个数据,第一个算式一定有三个数据。
对于所有数据, 0 < i ≤ 50 0<i\leq 50 0<i≤50,运算数为非负整数且小于 10000 10000 10000。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<map>
using namespace std;
int main(){
int n;
cin>>n;
int p;
string k;
while(n--){
cin>>k;
if(k == "a"){
p = 1;
int x,y;
cin>>x>>y;
cout<<x<<"+"<<y<<"="<<x+y<<endl;
string xx = to_string(x);
string yy = to_string(y);
string xy = to_string(x+y);
int len = xx.length()+yy.length()+xy.length()+2;
cout<<len<<endl;
continue;
}
if(k == "b"){
p = 2;
int x,y;
cin>>x>>y;
cout<<x<<"-"<<y<<"="<<x-y<<endl;
string xx = to_string(x);
string yy = to_string(y);
string xy = to_string(x-y);
int len = xx.length()+yy.length()+xy.length()+2;
cout<<len<<endl;
continue;
}
if(k == "c"){
p = 3;
int x,y;
cin>>x>>y;
cout<<x<<"*"<<y<<"="<<x*y<<endl;
string xx = to_string(x);
string yy = to_string(y);
string xy = to_string(x*y);
int len = xx.length()+yy.length()+xy.length()+2;
cout<<len<<endl;
continue;
}
if(p == 1){
int x = stoi(k);
int y;
cin>>y;
cout<<x<<"+"<<y<<"="<<x+y<<endl;
string yy = to_string(y);
string xy = to_string(x+y);
int len = k.length()+yy.length()+xy.length()+2;
cout<<len<<endl;
continue;
}
if(p == 2){
int x = stoi(k);
int y;
cin>>y;
cout<<x<<"-"<<y<<"="<<x-y<<endl;
string yy = to_string(y);
string xy = to_string(x-y);
int len = k.length()+yy.length()+xy.length()+2;
cout<<len<<endl;
continue;
}
if(p == 3){
int x = stoi(k);
int y;
cin>>y;
cout<<x<<"*"<<y<<"="<<x*y<<endl;
string yy = to_string(y);
string xy = to_string(x*y);
int len = k.length()+yy.length()+xy.length()+2;
cout<<len<<endl;
continue;
}
}
return 0;
}
标记思想!
字符串–数字:stoi()
数字–字符串:to_string()