1073 Scientific Notation (20 point(s))

1073 Scientific Notation (20 point(s))

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

找规律、STL字符串处理。

读题关键字:1.数组长度最长9999位(必须字符串处理);2.即使是正数也要给出"+";3.根据样例,指数部分可以有前导0。

思路:1.提取小数部分和指数部分;2.根据指数大于/小于零来分情况处理,其中大于0的话又分成三种情况。

STL string类总结:

1. 初始化和读入

string s;//默认是空串
string s2(s1);//s2初始化为s1的副本
string s(N,'c');//s初始化为N个'c'
string s;cin>>s;//读取字符串,遇到空格的时候结束
string s;getline(cin,s);//读取字符串,空格可读入,直到回车结束
string s;getline(cin,s,'a'); //读取字符串,直到'a'结束,其中任何字符包括回车都能够读入

2. 基本操作

string s="abc";
bool isEmpty = s.empty();    //判断是否为空
int length = s.size();    //字符串长度
int length = s.length();    //同上

3 .添加(append)

string s1="abc";
string s2="123";
s1.append(s2);//在s1的后面添加s2,s1=="abc123"
s1="def";
int pos = 1;
s1.append(s2,pos,s2.length()-pos);//添加s2的pos下标开始剩下的字符,s1=="def23"
s2.append(5,'.');//在s2的后面添加5个".",s2=="123....."

4. 插入(insert)

string s="heo";int pos =2 ,n=2;
s.insert(pos,n,'l');//在字符串s的pos位置插入n个'l' s=="hello" 

s = "ello"; pos = 0;
string s1 = s.insert(pos,"h");//在s的pos位置插入'h' s1=="hello" 

string a = "aa~";pos=0;
string s2 = s1.insert(pos,a);//在s的pos位置插入字符串a s2=="aa~hello" 

5. 翻转、子串

string s="abcd";
reverse(s.begin(),s.end());//翻转字符串,并返回,s=="dcba"
int pos = 1,n=2;
string s1 = s.substr(pos);//s的子串,从pos开始(包括0)到末尾的所有字符的子串,并返回。s1=="cba"
string s2 = s.substr(pos,n);//s的子串,从pos开始(包括0)的n个字符的子串,并返回。s2=="cb"

6. 删除(erase)

string s = "hhhhel~~~looo";
int pos = 11; 
s.erase(pos);//删除s的pos位置的字符,直到结尾 s=="hhhhel~~~lo"
	
pos = 0;int n=3;
s.erase(pos,n); //删除s的pos位置开始的n个字符 s=="hel~~~lo" 
	
string::iterator it;
s.erase(s.begin()+3,s.begin()+6);//删除从first到last中间的字符(first和last都是string类型的迭代器) s=="hello"

7. 替换(replace)

8. 查找(find)

string s="abcdcba";
int pos1 = s.find('b');//查找s中第一次出现b的位置,并返回。pos1==1
int pos2 = s.rfind('b');//查找s中最后一次出现b的位置,并返回。pos2==5
int pos3 = s.find("cd");//使用于字符和字符串 。pos3==2

未完待续... 

#include<iostream>
#include<string>
using namespace std;
int string2int(string s) {//把[+-]003一类的字符串转成数字 
	bool isNeg = false;
	if (s[0] == '-') isNeg = true;
	s.erase(s.begin());
	int ans = 0;
	for (int i = 0; i<s.length(); i++) {
		ans = ans * 10 + s[i] - '0';
	}
	if (isNeg) return -ans;
	else return ans;
}
string moveStr(string s, int offset) {
	if (offset == 0) return s;
	else if (offset > 0) {//x.xxx向右移动
		int fractionLen = s.length() - 2;//小数部分长度 
		s.erase(s.begin() + 1);//去掉小数点 
		if (fractionLen == offset) return s;
		else if (offset > fractionLen) {//移位大于小数部分长度 
			s.append(offset - fractionLen, '0');//末尾补零 
			return s;
		}
		else {//移位小于小数部分长度 
			s=s.insert(offset + 1, ".");//加上小数点
			//bug!!! 写成了fractionLen-offset+1 
			return s;
		}
	}
	else {//x.xxx向左移动 
		s.erase(s.begin() + 1);
		s = s.insert(0, -offset, '0');//前方补零 
		s = s.insert(1, ".");//第一位后面补上小数点 
		return s;
	}
}
int main(void) {
	string s;
	cin >> s;
	if (s[0] == '-') cout << "-";
	s.erase(s.begin());//去除符号位 
	int pos = s.find('E');
	string expStr = s.substr(pos+1, s.length()-pos-1); //获得指数部分字符串,不包括E
	//cout<<expStr<<endl;
	
	s = s.erase(pos, s.length() - pos);//删除指数这个部分
	int exp = string2int(expStr);//获得指数 
	
	//cout<<exp<<endl;
	//cout<<s<<endl;
	cout << moveStr(s, exp) << endl;
	return 0;
}

其它AC代码: 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    char s[10010];
    scanf("%s",s);
    if(s[0]=='-')
        printf("-");
    int pos=0;
    int len=strlen(s);
        while(s[pos]!='E')
           {
               pos++;
           }
    int exp=0;
    for(int i=pos+2;i<len;i++)
    {
        exp=exp*10+(s[i]-'0');
    }
    if(exp==0)
    {
        for(int i=1;i<pos;i++)
            printf("%c",s[i]);
    }
    if(s[pos+1]=='-')
    {
        printf("0.");
        for(int i=0;i<exp-1;i++)
            printf("0");
        printf("%c",s[1]);
        for(int i=3;i<pos;i++)
            printf("%c",s[i]);
    }
    else
    {
        for(int i=1;i<pos;i++)
        {
            if(s[i]=='.')
                continue;
            printf("%c",s[i]);
            if(i==exp+2&&pos-3!=exp)
                printf(".");
        }
        for(int i=0;i<exp-(pos-3);i++)
            printf("0");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值