P1957 口算练习题

24 篇文章 2 订阅

题目描述
王老师正在教简单算术运算。细心的王老师收集了i道学生经常做错的口算题,并且想整理编写成一份练习。 编排这些题目是一件繁琐的事情,为此他想用计算机程序来提高工作效率。王老师希望尽量减少输入的工作量,比如5+8的算式最好只要输入5和8,输出的结果要尽量详细以方便后期排版的使用,比如对于上述输入进行处理后输出 5+8=13 以及该算式的总长度6。王老师把这个光荣的任务交给你,请你帮他编程实现以上功能。

输入格式
第一行为数值i

接着的i行为需要输入的算式,每行可能有三个数据或两个数据。

若该行为三个数据则第一个数据表示运算类型,a表示加法运算,b表示减法运算,c表示乘法运算,接着的两个数据表示参加运算的运算数。

若该行为两个数据,则表示本题的运算类型与上一题的运算类型相同,而这两个数据为运算数。

输出格式
输出2*i行。对于每个输入的算式,输出完整的运算式及结果,第二行输出该运算式的总长度

输入输出样例
输入
4
a 64 46
275 125
c 11 99
b 46 64
输出
64+46=110
9
275+125=400
11
11*99=1089
10
46-64=-18
9
说明/提示
数据规模与约定

0<i<=50

运算数为非负整数且小于10000

对于50%的数据,输入的算式都有三个数据,第一个算式一定有三个数据。

给大家展示一下。。。菜鸡本鸡

编译错误原因是to_string、atoi、itoa的问题,到现在也没有改出来,所以就书写了判断位数代码
在这里插入图片描述

//#pragma GCC optimize(3,"Ofast")
#include<bits/stdc++.h>
#include<string>
using namespace std;
typedef struct{
	int first,second,last,length;
	char what[1];
	char temp[100];
}is;
int main(){
	int n;
	cin>>n;
	is ti[n];
	for(int i=0;i<n;i++){
		cin>>ti[i].temp;
		cout<<unitbuf;
		if(ti[i].temp[0]=='a'||ti[i].temp[0]=='b'||ti[i].temp[0]=='c'){
			ti[i].what[0]=ti[i].temp[0];
			cin>>ti[i].first>>ti[i].second;
		}
		else{
			ti[i].first=atoi(ti[i].temp);
			cin>>ti[i].second;
			ti[i].what[0]=ti[i-1].what[0];
		}
		if(ti[i].what[0]=='a'){
			ti[i].last=ti[i].first+ti[i].second;
		}
		else if(ti[i].what[0]=='b'){
			ti[i].last=ti[i].first-ti[i].second;
		}
		else if(ti[i].what[0]=='c'){
			ti[i].last=ti[i].first*ti[i].second;
		}
		ti[i].length=2;
		if(ti[i].first/1000!=0){
			ti[i].length+=4;
		}
		else if(ti[i].first/100!=0){
			ti[i].length+=3;
		}
		else if(ti[i].first/10!=0){
			ti[i].length+=2;
		}
		else{
			ti[i].length+=1;
		}
		if(ti[i].second/1000!=0){
			ti[i].length+=4;
		}
		else if(ti[i].second/100!=0){
			ti[i].length+=3;
		}
		else if(ti[i].second/10!=0){
			ti[i].length+=2;
		}
		else{
			ti[i].length+=1;
		}
		if(ti[i].last/100000000!=0){
			ti[i].length+=9;
		}
		else if(ti[i].last/10000000!=0){
			ti[i].length+=8;
		}
		else if(ti[i].last/1000000!=0){
			ti[i].length+=7;
		}
		else if(ti[i].last/100000!=0){
			ti[i].length+=6;
		}
		else if(ti[i].last/10000!=0){
			ti[i].length+=5;
		}
		else if(ti[i].last/1000!=0){
			ti[i].length+=4;
		}
		else if(ti[i].last/100!=0){
			ti[i].length+=3;
		}
		else if(ti[i].last/10!=0){
			ti[i].length+=2;
		}
		else{
			ti[i].length+=1;
		}
		if(ti[i].last<0){
			ti[i].length+=1;
		}
	}
	for(int i=0;i<n;i++){
		cout<<ti[i].first;
		if(ti[i].what[0]=='a'){
			cout<<'+';
		}
		if(ti[i].what[0]=='b'){
			cout<<'-';
		}
		if(ti[i].what[0]=='c'){
			cout<<'*';
		}
		cout<<ti[i].second<<'='<<ti[i].last<<endl<<ti[i].length<<endl;
		}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值