java和C 1024 科学计数法 (20 分)


原题

https://pintia.cn/problem-sets/994805260223102976/problems/994805297229447168

java代码

注意把scanner换成BufferedReader不会超时

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str = reader.readLine();
        BigDecimal bigDecimal = new BigDecimal(str);//接受科学计数法格式的字符串
        System.out.println(bigDecimal.toPlainString());//转化成没有指数字段的字符串
    }
}

容易失分的测试点

+1.23400E+03
1234.00
+1.23400E+06
1234000
+1.23400E+00
1.23400

以下与下面的c代码思路一样,但是有两个超时

package pat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] split = reader .readLine().split("E");
        char[] base = split[0].toCharArray();
        int exponent= Integer.valueOf(split[1]);
        int count=0;

        if (base[0]=='-') System.out.print("-");

        if(exponent<0){
            System.out.print("0.");
            exponent=Math.abs(exponent);
            while (exponent-1>0) {
                System.out.print("0");
                exponent--;
            }
            for (int i=1;iexponent)
                    System.out.print(".");
                System.out.print(base[i]);
                count++;
            }
            while (exponent-count>0){
                System.out.print("0");
                exponent--;
            }
        }
    }
}

c代码

与java代码不同的是自己split字符'E'前后的字符串。
学到string::substr函数、stoi(string,start,end)函数(string to Integer字符串转化为整型)

#include 
#include 
using namespace std;

int main()
{
    string str;
    cin>>str;
    int i=0;
    while (str[i]!='E') i++;
    string base=str.substr(1,i-1);
    int exponent=stoi(str.substr(i+1));//把string转化成整型

    if(str[0]=='-') cout<<'-';

    if(exponent<0){
        cout<<"0.";
        for(int i=0;iexponent)
            cout<<'.';

            cout<0)
        {
            cout<<'0';
            exponent--;
        }
        
    }
    return 0;
}

别人的代码

#include 
#include 
using namespace std;

int main()
{
    string str;
    cin>>str;
    int i=0;
    while (str[i]!='E') i++;
    string base=str.substr(1,i-1);
    int exponent=stoi(str.substr(i+1));//把string转化成整型

    if(str[0]=='-') cout<<'-';

    if(exponent<0){
        cout<<"0.";
        for(int i=0;i

进阶版,我没看懂,贴在这儿下https://www.cnblogs.com/gh110/p/12158233.html

#include 
#include 
using namespace std;

int main()
{
	int d, e, count = 0;
	char Dec[10000];
	scanf("%d.%[0-9]E%d", &d, Dec, &e);//%[0-9]正则匹配
	printf("%s", d < 0 ? "-" : "");

	while (e++ < 0)
		printf("0%s", count++ == 0 ? "." : "");
	e--;
	printf("%d", abs(d)); 

	for (int i = 0; i < strlen(Dec) || i < e; i++)
		//strlen 是一个函数,它用来计算指定字符串 str 的长度,但不包括结束字符(即 null 字符)
		printf("%s%c", i == e && !count ? "." : "", i < strlen(Dec) ? Dec[i] : '0');
}