A1073.Scientific Notation

题意:

就是给出一个科学计数法的字符串,要求输出表示的整数

思路分析:

首先考虑数的正负号,如果字符串第一个字符s[0]是'-',则应先输出'-',否则原样输出。之后获取指数的值,在此之前需要先取得'E'的下标index,而后从index+2向后枚举字符串,并将字符串转为10进制,即为指数exp。

1.如果s[index+1]='-'即指数为负数,说明小数点需要向左移,故而先计算前导0的个数qiandao,qiandao = exp-1。输出的方法为:先输出"0.",而后输出qiandao个数的0,之后原样输出'E'之前的系数部分,碰到'.',则不输出。

2.如果s[index+1]='+'即表示指数为正数,小数点需要右移,而需要输出的后导0的个数为exp-index+3(自己推导下),原样输出'E'之前系数部分时,当枚举变量i==exp+2时则还需输出一个'.',当然此时不需要输出后导0。

注意点

  • 指数部分有可能为+0,-0;这种情况下应该原样输出
  • 注意模拟过程中.应该放的位置。

参考代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
char s[maxn];

int main(void){
	scanf("%s",s);
	int len = strlen(s);
	int index = strchr(s,'E') - s;		//找到E的下标位置
	if(s[0] == '-') cout<<"-";
	int exp = 0;		//指数
	for(int i = index + 2;i < len;i++) {
		exp = exp*10 + s[i] - '0';
	}
	
	if(exp == 0) {			//如果指数为0,则原样输出 
		for(int i = 1;i < index;i++) cout<<s[i];
	} else {
		if(s[index+1] == '-') {			//如果指数为负数 
			int qiandao = exp - 1;		//计算需要输出的前导0
			cout<<"0.";
			for(int i = 0;i < qiandao;i++) cout<<"0";		//输出前导0 
			for(int i = 1;i < index;i++) {
				if(s[i] == '.') continue;
				cout<<s[i];
			}
		} else if(s[index+1] == '+') {
			for(int i = 1;i < index;i++) {
				if(s[i] == '.') continue;
				cout<<s[i];
				if(i == exp+2 && i != index-1) cout<<".";
			}
			if(exp > index - 3) {		//输出后导0; 
				for(int i = 0;i < exp-index+3;i++) cout<<"0";
			}
		}
	}
	return 0;
}

总结

本题细节较多,需要注意一些特殊地方的处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Q21: Which of the following is a valid user-defined output stream manipulator header? a. ostream& tab( ostream& output ) b. ostream tab( ostream output ) c. istream& tab( istream output ) d. void tab( ostream& output ) Q22: What will be output by the following statement? cout << showpoint << setprecision(4) << 11.0 << endl; a. 11 b. 11.0 c. 11.00 d. 11.000 Q23: Which of the following stream manipulators causes an outputted number’s sign to be left justified, its magnitude to be right justified and the center space to be filled with fill characters? a. left b. right c. internal d. showpos Q24: Which of the following statements restores the default fill character? a. cout.defaultFill(); b. cout.fill(); c. cout.fill( 0 ); d. cout.fill( ' ' ); Q25: When the showbase flag is set: a. The base of a number precedes it in brackets. b. Decimal numbers are not output any differently. c. "oct" or "hex" will be displayed in the output stream. d. Octal numbers can appear in one of two ways. Q26: What will be output by the following statements? double x = .0012345; cout << fixed << x << endl; cout << scientific << x << endl; a. 1.234500e-003 0.001235 b. 1.23450e-003 0.00123450 c. .001235 1.234500e-003 d. 0.00123450 1.23450e-003 Q27: Which of the following outputs does not guarantee that the uppercase flag has been set? a. All hexadecimal numbers appear in the form 0X87. b. All numbers written in scientific notation appear the form 6.45E+010. c. All text outputs appear in the form SAMPLE OUTPUT. d. All hexadecimal numbers appear in the form AF6. Q28: Which of the following is not true about bool values and how they're output with the output stream? a. The old style of representing true/false values used -1 to indicate false and 1 to indicate true. b. A bool value outputs as 0 or 1 by default. c. Stream manipulator boolalpha sets the output stream to display bool values as the strings "true" and "false". d. Both boolalpha and noboolalpha are “sticky” settings.
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值