• PAT (Advanced Level) Practice 1073 Scientific Notation (20分)


    1.题目

    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

    2.代码

    #include<stdio.h>
    int main()
    {
    	char a[10000];
    	int b[10000], c[10000], i, j, t = 1, k = 0, flag = 1, number,k2=0;
    	scanf("%s", a);
    	if (a[0] == '-')
    		t = -1;
    	for (i = 1, j = 0; a[i] != 'E'; i++)
    	{
    		if (a[i] == '.')
    			continue;
    
    		b[j] = a[i] - '0';
    		j++;
    	}
    	j--;
    	i++;
    	if (a[i] == '-')
    		flag = 0;
    	i++;
    	for (; a[i] != ''; i++)
    	{
    		
    		c[k] = a[i] - '0';
    		k++;
    		k2++;
    	}k -= k2;
    	
    
    	number = c[k];
    	
    	for (i = 0; i < k2-1; i++)
    	{
    		number = number * 10 + c[k + 1];
    	}
    
    	
    	if (t != 1)
    
    		printf("-");
    
    	if (flag == 1)
    	{
    		if (j < number+1)
    		{
    			for (i = 0; i <= j; i++)
    				printf("%d", b[i]);
    			i++;
    			for (; i <= number+1; i++)
    				printf("0");
    		}
    
    		if (j >= number+1)
    		{
    			for (i = 0; i <= number; i++)
    				printf("%d", b[i]);
    			printf(".");
    			for (i = number+1; i <= j; i++)
    				printf("%d", b[i]);
    		}
    	}
    
    		if (flag == 0)
    		{
    			printf("0.");
    			for (i = 0; i < number-1; i++)
    				printf("0");
    			for (i = 0; i <=j; i++)
    				printf("%d", b[i]);
    		
    		}
    
    	}
  • 相关阅读:
    加法问题,编程入门课
    三.Python变量,常量,注释
    二、python介绍
    一. python 安装
    Syntax behind sorted(key=lambda :)
    Java动态数组
    Package name does not correspond to the file path (IntelliJ IDEA)
    理解Java构造器中的"this"
    The Constructor with No Arguments
    160229-01、web页面常用功能js实现
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788821.html
Copyright © 2020-2023  润新知