PAT甲级刷题之路——1073

PAT甲级秋季考试落败,为了保研,冲冲冲!!

今年是第一年浙大保研需要机试,半路出家完全没有底,看了晴神宝典大概十天以后,今年去考了PAT甲级,然后意料之中的失败了。第一道题解决完素数和最大公约数的问题以后只会用暴力,还没跳出来;第二道题一个测试点没过;第三道题可能题目最后一句话没看懂理解有点偏差,最后一个测试点没过;第四道题,题是看懂了,也觉得自己会写,写出来以后怎么输出还是不对?估计又是题目理解错了,orz,我这该死的英语理解。
总结来说还是自己太菜,离保研还有一段时间,今年的题目还没在网站公布,那就先刷刷其他题!嗯!就从命中注定的1073题开始吧!(其实是感觉自己大概能刷80题左右就浙大机试了,orz

PAT1073 Scientific Notation

1073传送门

原题如下

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [±][1-9].[0-9]+E[±][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent’s signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:
Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent’s absolute value is no more than 9999.
Output Specification:
For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.
Sample Input 1:
+1.23400E-03
Sample Output 1:
0.00123400
Sample Input 2:
-1.2E+10
Sample Output 2:
-12000000000

题目意思

科学计数法的应用。将科学计数法表示的A用普通计数法表示出来(并保留特征数字)。其中科学计数法的整数部分只有一个数字,至少一个数字在小数部分
输入:A的数字长度不超过9999鸽字节,指数绝对值不超过9999.
输出:输出A的普通表示发的数,保留所有特征位包括尾部的0.

GOGOGO!自己的想法

可以归类为字符串模拟吧?用最笨的方法做最稳的题hh
观察科学计数法的结构:
±1-9.0-9+E±0-9

  1. 首先判断正负号,若正则输出要消掉符号
  2. 获取一位的整数部分和’.'到’E’之间的的小鼠部分,可以用string将A的所有特征位保留下来
  3. 找到’E’的位置,并判断其后的正负号,若是’+‘则判断string.length()-1是否大于指数,若大于,则补全,否则减去(不能减去特征位啊,第一次提交就因为这个没有AC,把整数余下的特征位放在小数里);若是’-'则先输出’0.'然后输出指数-1的0再输出string(我在这一部分的代码用了string的insert函数,感觉更方便,也更不容易出错)

答案反馈

第一次提交:2分的测试点出错,初步判断应该是特殊情况没有考虑到。
在这里插入图片描述
错误的点:
果然,因为当特征位大于指数时,我想当然的把尾部砍了,其实可以加小数,然后AC了。
在这里插入图片描述

代码

放上AC的代码了,有点繁琐:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<set>
#include<queue>
#include<map>
#include<stack>
using namespace std;
int main() {
	string str;
	cin >> str;
	int flag_i=0,flag_e=0;//判断整数,指数符号是否为'-'
	if (str[0] == '-')flag_i = 1;
	string sgn;//用来存放特征位
	sgn = str[1];//整数部分
	int i;
	for (i = 3; i < str.length(); i++) {
		if (str[i] == 'E')break;
		sgn += str[i];
	}
	if (str[i + 1] == '-')flag_e = 1;//判断指数正负
	int ex=0;//存放指数
	for (int j = i+2; j < str.length(); j++) {
		//cout << str[j] << ' ';
		ex = ex * 10 + (str[j] - '0');
	}
	//cout << ex << endl;
	if (flag_i == 1)cout << '-';//正负号
	if (flag_e == 0) {
		if (sgn.length() >= ex) {
			for (int k = 0; k <= ex; k++) {
				cout << sgn[k];
			}
			//第一次错的地方
			for (int k = ex+1; k < sgn.length(); k++) {
				if (k == ex+1)cout << '.';
				cout << sgn[k];
			}
			cout << endl;
			//第一次错的地方
		}
		else {
			for (int k = 0; k < sgn.length(); k++) {
				cout << sgn[k];
			}
			for (int k = 0; k < ex - sgn.length()+1; k++) {
				cout << '0';
			}
			cout << endl;
		}
	}
	else {
		for (int k = 0; k < ex; k++) {
			sgn.insert(sgn.begin(), '0');
		}
		sgn.insert(sgn.begin() + 1, '.');
		for (int k = 0; k < sgn.length(); k++) {
			cout << sgn[k];
		}
		cout << endl;
	}
	return 0;
}

结语

菜鸡的PAT训练之路任重而道远,这也是我第一篇博客呀,希望在总结和与大家的交流中中进步呀!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值