首页 > 试题广场 >

Scientific Notation (20)

[编程题]Scientific Notation (20)
  • 热度指数:1712 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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 file 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,
示例1

输入

+1.23400E-03

输出

0.00123400
/*
https://www.nowcoder.com/questionTerminal/e2249eec4ca5438abe50c33b4be98de4
Scientific Notation (20)  2018-12-20
General mean: give you a munber in Scientific notation, print the munber in decimal
Result: Though it is the second times I do it, it problem still waste me a lot of time.
The problem mainly is that my confident is not enough to solve it in a short time. Such 
kind of question need you to calm down and be careful, Skillfuly handle a string is very
important. It problem remind me that I should imporve my ability of handle a string, skillfuly
use all kind of funtion in c++ library, and better review my java so I can have other choise 
in emergency.
*/
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
string input;
int main(){
    cin >> input;
    cout << (input[0] == '-' ? "-" : "");
    input.erase(0,1);        //string& erase (size_t pos = 0, size_t len = npos);
    input.erase(1, 1);        //erase the '.'
    int exp = atoi(input.substr(input.find("E")+1).c_str()); //the munber after "E"
    input.erase(input.find("E"));    //erase all the char from 'E' to end.
    if (exp < 0){
        input.insert(input.begin(), -exp, '0');
        input.insert(1, ".");
        
    }
    else if(exp<input.length()-1){
        input.insert(exp+1, ".");
    }
    else {
        input.append( exp - input.length()+1,'0');
    }
    cout << input << endl;
    return 0;
}



发表于 2018-12-20 17:12:36 回复(0)
a,b = input().split('E')
m = '' if a[0] == '+' else '-'
a = a[1:].replace('.','')
if int(b) < 0:
    print(m + '0.' + '0' * (abs(int(b)) - 1) + a)
elif len(a) - 1 <= int(b):
    print(m + a + '0' * (int(b) - len(a) + 1))
else:
    print(m + a[:1 + int(b)] + '.' + a[1 + int(b):])

发表于 2020-03-07 11:58:04 回复(1)
1、保留所有有效位数
2、指数大于0时,输出要分三种情况
#include<iostream>
#include<string>
using namespace std;

int main() {
	string s;
	cin>>s;
	char op1,op2;
	int E,dot;
	string zhengshu,xiaoshu,zhishu;
	E=s.find('E',0);
	dot=s.find('.',0);
	op1=s[0];
	op2=s[E+1];
	zhengshu=s.substr(1,1);
	xiaoshu=s.substr(dot+1,E+1-dot-2);
	zhishu=s.substr(E+2); 
	if(op1=='-')cout<<"-";
	if(op2=='-'){
		cout<<"0.";
		int i=stoi(zhishu)-1;
		while(i--)cout<<"0";
		cout<<zhengshu<<xiaoshu;
	} 
	else if(op2=='+'){
		cout<<zhengshu[0];
		int e=stoi(zhishu);
		int size=xiaoshu.size();
		if(e<size){
			cout<<xiaoshu.substr(0,e);
			cout<<".";
			cout<<xiaoshu.substr(e,size-e);
		}
		else if(e==size){
			cout<<xiaoshu.substr(0,e);
		}else{
			cout<<xiaoshu.substr(0,size);
			int i=e-size;
			while(i--)cout<<"0";
		}
	}
    return 0;
}


发表于 2020-02-02 14:02:51 回复(0)
 import java.util.*;
import java.math.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        BigDecimal s=sc.nextBigDecimal();
        System.out.println(s.toPlainString());
    }
}

发表于 2018-10-05 20:39:43 回复(0)
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void string2num(string& s, int& n) {
  stringstream ss;
  ss << s;
  ss >> n;
}

void init(string& s, const int& n) {
  if (n == 0)
    return;
  else if (n < 0) {
    s.erase(1, 1);
    // cout << s << endl;
    for (int i = 1; i <= (-n); i++) s.insert(0, "0");
    // cout << s << endl;
    s.insert(1, ".");
    // cout << s << endl;
  } else if (n > 0) {
    s.erase(1, 1);
    if (n < s.size() - 1) {
      s.insert(n + 1, ".");
    } else if (n >= s.size() - 1) {
      int need = n - (s.size() - 1);
      for (int i = 1; i <= need; i++) s = s + '0';
    }
  }
}

int main() {
  int pos_E, int_exp;
  string line, str_num, str_exp;
  bool positive = true;
  cin >> line;
  if (line[0] == '-') positive = false;
  pos_E = line.find('E');
  str_num = line.substr(1, pos_E - 1);
  str_exp = line.substr(pos_E + 1);
  string2num(str_exp, int_exp);
  init(str_num, int_exp);
  if (!positive) cout << "-";
  cout << str_num << endl;
  return 0;
}

发表于 2017-09-05 14:27:11 回复(0)
解题报告里有个字符串的
我再贴一个基于字符数组的吧 效率应该比string高一点 毕竟那个封装过了
思路很简单:
1.读入数据
2.算出指数e
3. 如果负数先输出-
如果指数够大,输出数组并在后补0
如果指数够小,输出0.0...0然后输出数组
输出数组的前e+1位,输出小数点,输出数组余下位
代码如下:
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

int str2int(vector<char> str) {
	int num = 0;
	for (int i = 0;i < str.size();i++) {
		num *= 10;
		num += str[i] - '0';
	}
	return num;
}

int main(int argc, const char* argv[]) {
	char ch = cin.get();
	bool positive = ch - '-';
	vector<char> str;
	ch = cin.get();
	str.push_back(ch);
	cin.get();
	while ((ch = cin.get()) - 'E')str.push_back(ch);
	ch = cin.get();
	bool positiveE = ch - '-';
	vector<char> strE;
	while ((ch = cin.get()) - '\n')strE.push_back(ch);
	int e = str2int(strE);
	if (!positiveE)e = -e;
	if (!positive)cout << "-";
	int len = str.size();
	if (e >= len - 1) {
		for (int i = 0;i < len;i++)cout << str[i];
		e -= len - 1;
		while (e--)cout << 0;
		//system("pause");
		return 0;
	}
	if (e < 0) {
		cout << "0.";
		while (++e)cout << 0;
		for (int i = 0;i < len;i++)cout << str[i];
		//system("pause");
		return 0;
	}
	e++;
	int i = 0;
	while (e--)cout << str[i++];
	cout << ".";
	while (i < len)cout << str[i++];
	//system("pause");
	return 0;
}

编辑于 2017-03-01 15:26:48 回复(0)
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
int rutu(char a);
int ru(string b);
void print(string a, string b);
int main()
{
	bool flag = true;
	string a,b;
	cin >> a;
	b = a.substr(a.find("E") + 1);
	/*cout << b << endl;*/
	if (a[0] == '-')cout << a[0];
	print(a, b);
	return 0;
}
void print(string a, string b)
{
	int x = ru(b),loc;
	queue<char> m;
	for (int i = 1; i < a.size() - b.size() - 1; i++) {
		if (a[i] == '.') {
			loc = i;
		}
		else {
			m.push(a[i]);
		}
	}
	//cout << "###  " << m.size() << endl;
	if (b[0] == '-') loc -= x;
	else loc += x;
	if (loc <= 0) {
			cout << "0.";
			while (loc != 1)
			{
				loc++;
				cout << "0";
			}
			int sy = m.size();
		for (int i = 0; i < sy; i++) {
			cout << m.front();
			m.pop();
		}
	}
	else if (loc < m.size()) {
		int sy = m.size();
		for (int i = 0; i < sy; i++) {
			if (loc == i) {
				cout << ".";
				i--;
			}
			cout << m.front();
			m.pop();
		}
	}
	else {
		int sy = m.size();
		loc -= m.size();
		for (int i = 0; i < sy; i++) {
			cout << m.front();
			m.pop();
		}
		while (loc != 1)
		{
			loc--;
			cout << "0";
		}
	}
}
int ru(string b)
{
	int x = 0, j = 1;
	for (int i = b.size() - 1; i > 0; i--) {
		x += (rutu(b[i]) * j);
		j *= 10;
	}
	return x;
}
int rutu(char a)
{
	return (int)(a - '0');
}

发表于 2021-11-02 12:36:17 回复(0)
#include<bits/stdc++.h>
using namespace std;
string integerPart,fractionalPart,exponentPart;
void split(const string& s){
    auto iter1 = s.cbegin(), iter2 = s.cbegin();
    for (; iter1 != s.cend(); ++iter1){
        if (*iter1 == '.'){
            iter2 = iter1;
            integerPart = string(s.cbegin(), iter2);
        }
        if (*iter1 == 'E'){
            fractionalPart = string(iter2.operator+(1), iter1);
            exponentPart = string(iter1.operator+(1), s.cend());
            break;
        }
    }
//    cout << integerPart<< endl;
//    cout << fractionalPart << endl;
//    cout << exponentPart << endl;


}
string solve(){
    int exponent = atoi(exponentPart.c_str());
    int fractionLen = fractionalPart.length();
    string rtnString = "";
    if (exponent == 0)
        rtnString = (integerPart + "." +fractionalPart);
    else if (exponent>0){
        if (exponent >= fractionLen){
            rtnString = (integerPart + fractionalPart);
            int cnt = exponent - fractionLen;
            while (cnt-- > 0)
                rtnString += "0";
        }
        else {
            rtnString = (integerPart + fractionalPart.substr(0,exponent)
                    + "."+ fractionalPart.substr(exponent));
        }
    }
    else {
        // exponent < 0
        exponent = -exponent;
        char symbol = integerPart[0];
        integerPart.erase(0,1);
        rtnString = symbol +string("0.") ;
//        cout << rtnString << endl;
        --exponent;
        while(exponent--)
            rtnString += "0";

        rtnString +=(integerPart+fractionalPart);
//        cout << integerPart << endl;
    }
    if (rtnString[0] == '+')
        rtnString.erase(0,1);
    return rtnString;
}
int main(){
    string scientificNum;
    cin >> scientificNum;
    split(scientificNum);
    cout << solve() << endl;
    return 0;
}

发表于 2021-02-05 17:33:37 回复(0)
Java就是欺负人😂
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 bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		// 就一行代码
		BigDecimal bigDecimal = new BigDecimal(bufferedReader.readLine());
		System.out.println(bigDecimal.toPlainString());
	}

}


编辑于 2020-04-11 09:39:55 回复(0)
不知道为什么这种题看着简单,我每次却要花很多时间
#include<iostream>
(720)#include<vector>//  1.23400   12340.0  
#include<string>
using namespace std;
int main() {
	string s;
	cin >> s;
	string xiaoshu;
	string zhishu;
	if (s[0] == '-') {
		printf("-");
	}
	s = s.erase(0, 1);
	int i = 0, j = 0;
	for (; i < s.size(); i++) {
		if (s[i] == 'E') {
			xiaoshu = s.substr(0, i);
			zhishu = s.substr(i + 1);
			break;
		}
	}
	int symbol = 1;
	if (zhishu[0] == '-') {
		symbol = -1;
	}
	zhishu.erase(0, 1);
	int zh = stoi(zhishu, 0, 10);
	while (xiaoshu[j] != '.') {
		j++;
	}
	if (symbol > 0) {
		if (zh < xiaoshu.size() - j - 1) {
			xiaoshu.erase(j, 1);
			xiaoshu.insert(j + zh, ".");
		}
		else {
			string tmp;
			int n = zh - xiaoshu.size() + j + 1;
			while (n > 0) {
				tmp = tmp + "0";
				n--;
			}
			xiaoshu = xiaoshu.erase(j, 1) + tmp;
		}
	}
	else {//1.234 -3   11234
		string tmp = "0.";
		if (zh) {
			while (zh > 1) {
				tmp = tmp + "0";
				zh--;
			}
			xiaoshu = tmp + xiaoshu.erase(j, 1);
		}
	}
	printf("%s", xiaoshu.c_str());
}


发表于 2020-03-27 15:54:18 回复(0)
s=input()
a=s.find('E')
res=s[1:a].replace('.','')
x=int(s[a+1:])+1
if x>0:
    if x>=len(res):
        res=res+'0'*(x-len(res))
    else:
        res=res[:x]+'.'+res[x:]
else:
    res='0.'+'0'*(-x)+res
if s[0]=='-':
    res='-'+res
print(res)

发表于 2018-09-06 11:37:35 回复(0)
乙级题目:https://www.nowcoder.com/questionTerminal/d6d37c3010c04d0297572ea175510806?toCommentId=1451473
// 思路: 分类讨论,移动小数点即可
#include <iostream>
#include <string>
#include <math.h>
#include <sstream>
using namespace std;

int string2int(string a)
{
    stringstream ss;
    ss << a;
    int out;
    ss >> out;
    return out;
}

int main()
{
    string a;
    cin >> a;
    char symbol = a[0];
    string integer = a.substr(1, a.find('.') - 1);
    string _float = a.substr(a.find('.') + 1, a.find('E') - a.find('.') - 1);
    string power = a.substr(a.find('E') + 1, a.size() - a.find('E'));
    //cout << integer << " " << _float << " " << power << endl;
    int pow = string2int(power);
    if (symbol == '-')
    {
        cout << "-";
    }
    if (pow >= 0)
    {
        cout << integer;
        int i = 0;
        if (pow >= _float.size())
        {
            cout << _float;
            for (int j = pow - _float.size(); j > 0; j--)
            {
                cout << "0";
            }
        }
        else
        {
            for (i = 0; i < pow; i++)
            {
                cout << _float[i];
            }
            cout << ".";
            for (; i < _float.size(); i++)
            {
                cout << _float[i];
            }
        }
    }
    else
    {
        int i = 0;
        cout << "0.";
        for (i = pow; i < -1; i++)
        {
            cout << "0";
        }
        cout << integer;
        cout << _float;
    }
}  

发表于 2018-08-26 08:15:45 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main(){
 string a;
 char b[100010];
 int c=0;
 int n=0,n1=0;
 int m=0;
  cin>>a;
  if(a[0]=='-')
      cout<<'-';
  int i;
   for(i=1;i<a.length();i++){
     if(i==2)
       continue;
     if(a[i]=='E')
       break;
     b[n]=a[i];
     n++;
    }  
 i++;
 if(a[i]=='+')
     n1=1;
 i++;
 for(i;i<a.length();i++){
  c=c*10+a[i]-'0';
 }
 if(c==0){
  cout<<b[0]<<'.';
  for(i=1;i<n;i++){
   cout<<b[i];
  }
 }else if(n1==0){
    cout<<"0.";
    for(int i=1;i<c;i++){
     cout<<"0";
    }
    cout<<b; 
 }
 else{
  if(c>=n-1){
   cout<<b;
   for(int i=0;i<c-n+1;i++){
    cout<<"0";
   }
  }
  else{
   for(int i=0;i<n;i++){
    if(i==c+1){
     cout<<'.';
    }
    cout<<b[i];
   }
  }
 }
 return 0;
}
发表于 2018-05-19 23:24:14 回复(0)

用字符串保存基数部分,用整数保存指数部分。根据指数的大小,分情况输出字符串,补“0”,把小数点放在合适位置。

# include <cstdio>
# include <cstring>

char num[20100]; //x.xxx 不带点的,小数点在第0位后
char sign;
int exp;

int main() {
    char t;
    scanf("%[+-]", &sign);
    scanf("%[123456789]", num);
    scanf("%c", &t); //.
    scanf("%[0123456789]", num + 1);
    scanf("%c", &t); //E
    scanf("%d", &exp);

    if(sign == '-') printf("%c", sign); //输出+-
    if (exp == 0) {
        printf("%c.", num[0]);
        printf("%s", num + 1);
    }
    else if (exp > 0) {
        int len = strlen(num);
        if (exp >= len - 1) { //有效位数不够,还要补0
            printf("%s", num);
            for (int i = len - 1; i < exp; i++) {
                printf("0");
            }
        }
        else { //有效数字位数够了,还有小数点
            for (int i = 0; i < exp + 1; i++) {
                printf("%c", num[i]);
            }
            printf(".");
            printf("%s", num + exp + 1);
        }
    }
    else { //exp < 0
        exp = -exp;
        printf("0.");
        for (int i = 1; i < exp; i++) {
            printf("0");
        }
        printf("%s", num);
    }


    return 0;
}
发表于 2018-03-13 08:49:36 回复(0)
#include <cstdio>
#include <cstring>
#include <cctype>

const int N = 10007;
struct Real {
    int e, i;
    int f[N], lf;
    Real() {}
    Real(char *s)
    {
        memset(f, 0, sizeof(f));
        sscanf(s, "%d", &i);
        s += 3;
        lf = 0;
        f[lf++] = i > 0 ? i : -i;
        for(; isdigit(*s); s++) {
            f[lf++] = *s - '0';
        }
        s += 1;
        sscanf(s, "%d", &e);
        e++;
        //0.xxxxExx
    }
    void print()
    {
        if(i < 0) {
            printf("-");
        }
        if(e <= 0) {
            printf("0.");
            for(;e<0;e++){
                printf("0");
            }
            for(int j = 0; j < lf; j++) {
                printf("%d", f[j]);
            }
        } else {
            for(int j = 0; j < lf || j < e; j++) {
                if(e == j) {
                    printf(".");
                }
                printf("%d", f[j]);
            }
        }
        printf("\n");
    }
};
int main()
{
    char str[N];
    scanf("%s", str);
    Real r(str);
    r.print();
    return 0;
}

编辑于 2017-08-25 18:03:34 回复(0)
啥头像
正常处理,有几个注意事项:
        1.正负号
        2.小数点的位置和有无,计算好
        3.准确切分字符串

贴一个c++代码如下:
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
    string str, base, expStr; int exponent; cin >> str;
    int posE = str.find('E');
    bool isMinus = false;
    // 划分指数和底数部分
    expStr = str.substr(posE+1, str.length()-1-posE);
    exponent = atoi(expStr.c_str());
    if(str[0] == '-') {
        isMinus = true;
    }
    base = str.substr(1, posE-1);
    base.erase(1, 1);
    str = base;
    // 处理科学计数法
    if(exponent >= 0) {
        if(base.length()-1 < exponent) {
            for(int i=0; i<(exponent-base.length()+1); i++) {
                str += '0';
            }
        } else {
            str.insert(exponent+1, 1, '.');
        }
    } else {
        exponent = -exponent;
        for(int i=0; i<exponent-1; i++) {
            str = '0' + str;
        }
        str = "0." + str;
    }
    if(isMinus) {
        str = '-' + str;
    }
    cout << str << endl;
    return 0;
} 


发表于 2015-12-26 15:50:27 回复(0)
这个题目考查科学计数法。
首先,我们必须注意到科学计数法的格式是1.xE(+ -)m.
据此,我们可以分三种情况讨论:m==0 m>0 m<0.
注意,我们只记录E之前的数字,不记录小数点。
当m==0时,直接输出这个数。
当m<0时,在1之前添加(m-1)个0 ,然后输出这个数。
当m>0时,根据m和x的位数的大小(m>x,不输出小数点,反之输出小数点),输出剩余部分即可。
发表于 2015-06-30 21:22:32 回复(0)