zoukankan      html  css  js  c++  java
  • 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]);
    		
    		}
    
    	}
  • 相关阅读:
    每个Java开发人员都应该知道的10个基本工具
    2019年让程序员崩溃的 60 个瞬间,笑死我了
    面试官:服务器安装 JDK 还是 JRE?可以只安装 JRE 吗?
    腾讯工作近十年大佬:不是我打击你!你可能真的不会写Java
    作为Java开发人员不会饿死的5个理由
    趣味算法:国王和100个囚犯
    阿里第二轮面试:手写Java二叉树
    Linux软件安装——服务管理
    Linux帮助——重要文件
    Linux帮助——常用命令
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788821.html
Copyright © 2011-2022 走看看