PAT 1073. Scientific Notation (20)

这一题自己花的时间有点多。

要注意几个问题:

1,注意题目的输入格式,小数点就在固定的位数上,

2,输入用整个字符串读取

3,正号不输出

4 ,考虑正指数和负指数两种情况就行了

#include <iostream>
#include <cstring>
using namespace std;

char a[20000];

int E; //大E的位置

int exp(int n) //找大E的位置
{
	int temp = 0;
	int i;
	for(i = 0;i<n;i++)
		if(a[i] == 'E'){
			E = i;
			break;
		}
	
	bool flag = false;
	for(i++;i<n;i++)
	{
		if(a[i] == '-')
			flag = true;
		if(a[i] >= '0' && a[i] <= '9')
			temp = temp*10 + a[i] -'0';
	}
	
	if(flag)
		temp *= -1;
	return temp;
}
int main()
{

	scanf("%s",a);

	int n = strlen(a);

	int i = 2;  
	int ex = exp(n);
	bool flag = true;
	if(ex < 0)
	{
		flag = false;
		ex *= -1;
	}
	if(a[0] == '-')
		cout << a[0];

	if(ex == 0)
	{
		for(int j = 1;j < E;j++)
			cout << a[j];
		return 0;
	}

	if(flag)
	{
		int j = 1;
		for(;j<i;j++)
			cout << a[j];
		i++;
		while(ex--)
		{
			if(i<E)
				cout << a[i++];
			else
				cout << 0;
		}
		if( i < E){
			cout << ".";
			while(i < E)
				cout << a[i++];
		}

	}
	else
	{
			cout << "0.";
			ex = ex + 1 - i; 
			while(ex--)
				cout << 0;
			cout << a[1];
			i++;
			while(i < E)
				cout << a[i++];

	}
	cout << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值