33

洛谷P1957口算练习题题解

 4 years ago
source link: http://www.cnblogs.com/Craker/p/12591806.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前言:

题目传送门: https://www.luogu.com.cn/problem/P1957

其实这很简单

纯模拟撒~~~~

正文开始:

_话说 ,就当本蒟蒻正高高兴兴的刷水题时,居然
 碰到了这个laji题_

于是就开始了一顿乱写:

思路部分:

这道题很大的一个痛点就在于输入

4

a 64 46

275 125 //这里咋就没有字母了呢????

c 11 99

b 46 64

看来,三个cin不行

于是,经过几分钟的思考,

我大致确定了两种方案:

1.直接getline一行:

可是.....这个如何转换为数字的问题吗,还是去问前排一波的Ak大佬吧(逃)

2.其他办法:

先cin一个char类型的呗,至于那种毒瘤情况待会再解决

于是代码:

cin>>y;
if(y>='a'&&y<='c'){
	w=y;
	cin>>s1; 
}
cin>>s2;

补充小知识:char可是只会读入一个的oh

于是当275 125 这种数据出现的时候,y,s1,s2会变成这样:

y=‘2’;

s2=75;

s3=125;

好了,于是一种想法就理所应当的产生了:吧y变为数字,再与s2拼起来不就得了?

于是:

char y;long long s1,s2;
cin>>y>>s1>>s2;
s1=(y-'0')*pow(10,line(s1))+s1;
cout<<s1;
return 0;

其中有一句line(s1)是读取s1有几位的函数(当然要自己写啦)

long long line(long long i){
	
	long long j=0,k=i;
	if(i<=0){
		k=-k;
		j++;
	}
	
	while(k>0){
		k=k/10;
		j++;
	}
	return j;
}

很简单,一个模板而已;

可是,你难道没发现什么bug:当如测试点2的5002时会:变为52!!!!!!!

哪儿有问题呢?

原来,当程序读入5002时灰度为‘5’;002->2!!

所以,另辟新路吧!

仔细想想,原来可以这样!!

可把后面的s1用字符串读入

然后与y一拼,一起化为数字:

cin>>e;
e=y+e;
int le=e.size();int kw=1;int ss=0;
for(int i=le-1;i>=0;i--){
	ss+=(kw*(e[i]-'0'));
	kw=kw*10;
}
s1=ss;

于是,只需要再注意一些细节问题便好:

int main(){
	long long n;  cin>>n;
	for(long long i=1;i<=n;++i){
		getzf();
		//cout<<"s1:"<<s1<<" "<<s2<<endl;
		if(w=='a'){
			cout<<s1<<"+"<<s2<<"="<<s1+s2<<endl;
			cout<<line(s1)+2+line(s2)+line(s1+s2)<<endl;
		}
		else if(w=='b'){
			cout<<s1<<"-"<<s2<<"="<<s1-s2<<endl;
			cout<<line(s1)+2+line(s2)+line(s1-s2)<<endl;
			//cout<<"line:"<<line(s1)<<" "<<line(s2)<<" "<<line(s1-s2)<<endl; 
		}
		else if(w=='c'){
			cout<<s1<<"*"<<s2<<"="<<s1*s2<<endl;
			cout<<line(s1)+2+line(s2)+line(s1*s2)<<endl;
		}
	}
	return 0;
}

注意不要多输出换行oh!!!!!

那会爆零的!!!

code,完整代码:

#include<bits/stdc++.h>
using namespace std;
string a,e; long long s1,s2;char w,y;//////////////////////iephyhoyhjyeohyohytuohyoihj/////iihjn
long long line(long long i){
	
	long long j=0,k=i;
	if(i<=0){
		k=-k;
		j++;
	}
	
	while(k>0){
		k=k/10;
		j++;
	}
	return j;
}
void getzf(){
	cin>>y;
	//cout<<"y:"<<y<<endl;
	if(y>='a'&&y<='c'){
		w=y;
		cin>>s1; 
	}
	else{
		cin>>e;
		e=y+e;
		int le=e.size();int kw=1;int ss=0;
		for(int i=le-1;i>=0;i--){
			ss+=(kw*(e[i]-'0'));
			kw=kw*10;
		}
		s1=ss;
	}
	cin>>s2;
	//cout<<"s1:"<<s1<<"s2:"<<s2<<endl;
}
int main(){
	long long n;  cin>>n;
	for(long long i=1;i<=n;++i){
		getzf();
		//cout<<"s1:"<<s1<<" "<<s2<<endl;
		if(w=='a'){
			cout<<s1<<"+"<<s2<<"="<<s1+s2<<endl;
			cout<<line(s1)+2+line(s2)+line(s1+s2)<<endl;
		}
		else if(w=='b'){
			cout<<s1<<"-"<<s2<<"="<<s1-s2<<endl;
			cout<<line(s1)+2+line(s2)+line(s1-s2)<<endl;
			//cout<<"line:"<<line(s1)<<" "<<line(s2)<<" "<<line(s1-s2)<<endl; 
		}
		else if(w=='c'){
			cout<<s1<<"*"<<s2<<"="<<s1*s2<<endl;
			cout<<line(s1)+2+line(s2)+line(s1*s2)<<endl;
		}
	}
	return 0;
}

了结!!

总结一下,下次做模拟题需注意:

1.函数化编程!!!!方便调试,方便理解

这才是做较难模拟题的王道!!!!

2.注意细节,避免爆零!!!

极简:将复杂的东西分为小块,像搭积木一样解决各类难题,这也是每个学科创造的大致路径

删繁就简三秋树,标新立异二月花

byebye~~~~!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK