PAT 甲级 1073 Scientific Notation (20 分)

题目描述

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.

输入

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.

输出

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.

思路

就是模拟,判断一下,字符串操作即可

代码

#include<cstdio>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<math.h>
using namespace std;
char str[10000000] = {
    
     0 };
int r;
string ry;
char w[1000000] = {
    
     0 };
int main()
{
    
    
	cin.getline(str, 10000000);
	sscanf(str, "%[^E]", w);
	for (int i = strlen(w) + 1; str[i] != 0; i++)
	{
    
    
		ry += str[i];
	}
	r = atoi(ry.c_str());
	if (r == 0)
	{
    
    
		printf("1.");
		for (int i = 0; i < strlen(w) - 2; i++)
		{
    
    
			printf("0");
		}
		printf("\n");
	}
	else if (r > 0)
	{
    
    
		char op = w[0];
		if (op == '-')
		{
    
    
			printf("-");
		}
		int len = strlen(w) - 3;
		if (r < len)
		{
    
    
			cout << w[1];
			int k = 0;
			for (int i = 3; i < strlen(w); i++)
			{
    
    
				if (k == r)
				{
    
    
					cout << ".";
				}
				cout << w[i];
				k++;
			}
		}
		else if (r == len)
		{
    
    
			for (int i = 1; i < strlen(w); i++)
			{
    
    
				if (w[i] != '.')
				{
    
    
					cout << w[i];
				}
			}
		}
		else
		{
    
    
			for (int i = 1; i < strlen(w); i++)
			{
    
    
				if (w[i] != '.')
				{
    
    
					cout << w[i];
				}
			}
			for (int i = 0; i < r-len; i++)
			{
    
    
				cout << "0";
			}
		}
	}
	else
	{
    
    
		char op = w[0];
		if (op == '-')
		{
    
    
			printf("-");
		}
		printf("0.");
		for (int i = 1; i < -r; i++)
		{
    
    
			cout << "0";
		}
		for (int i = 1; i < strlen(w); i++)
		{
    
    
			if (w[i] != '.')
			{
    
    
				cout << w[i];
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45478482/article/details/120081691
今日推荐