题目链接:http://codeforces.com/contest/697/problem/B
【中文题意】给你一个浮点数的计数法表示形式,然后表示出他的正常形式,表示出的数不能有前导0也不能有后导0。
【思路分析】直接模拟就好了,注意几个坑:1. 0.0e0的时候 2. 1.0e0的时候。
//第一个是本弱鸡的代码,写了100多行
【AC代码】
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<algorithm>
using namespace std;
#define LL long long
int main()
{
string str;
while(cin>>str)
{
int len=str.length();
int dightPosition,ePosition;
for(int i=0;i<len;i++)
{
if(str[i]=='.')
{
dightPosition=i;
}
if(str[i]=='e')
{
ePosition=i;
}
}
int num1=0,num2=0;
for(int i=0;i<dightPosition;i++)
{
num1=num1*10+str[i]-'0';
}
for(int i=dightPosition+1;i<ePosition;i++)
{
num2=num2*10+str[i]-'0';
}
if(num1==0&&num2==0)
{
cout<<"0"<<endl;
continue;
}
int square=0;
for(int i=ePosition+1;i<len;i++)
{
square=square*10+str[i]-'0';
}
//cout<<dightPosition<<ePosition<<square<<endl;
if(square==0)
{
if(num2==0)
{
for(int i=0;i<dightPosition;i++)
{
cout<<str[i];
}
}
else
{
for(int i=0;i<ePosition;i++)
{
cout<<str[i];
}
}
cout<<endl;
continue;
}
string str2;
for(int i=0;i<dightPosition;i++)
{
str2+=str[i];
}
if(dightPosition+square==ePosition-1)
{
//cout<<"1."<<endl;
for(int i=dightPosition+1;i<ePosition;i++)
{
str2+=str[i];
}
//cout<<str2<<endl;
}
if(dightPosition+square<ePosition-1)
{
//cout<<"2."<<endl;
for(int i=dightPosition+1;i<dightPosition+square+1;i++)
{
str2+=str[i];
}
str2+='.';
for(int i=dightPosition+square+1;i<ePosition;i++)
{
str2+=str[i];
}
}
if(dightPosition+square>ePosition-1)
{
//cout<<"3."<<endl;
for(int i=dightPosition+1;i<ePosition;i++)
{
str2+=str[i];
}
for(int i=0;i<square-ePosition+dightPosition+1;i++)
{
str2+='0';
}
}
//str2+='\0';
int len2=str2.length(),flag=0;
//cout<<str2<<endl;
for(int i=0;i<len2;i++)
{
if(str2[i]=='0'&&str2[i+1]=='.')
{
flag=1;
}
if(str2[i]=='0'&&flag==1)
{
cout<<str2[i];
}
if(str2[i]!='0')
{
cout<<str2[i];flag=1;
}
}
cout<<endl;
}
return 0;
}
//大佬的JAVA代码
import java.util.*;
import java.math.*;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextBigDecimal().stripTrailingZeros().toPlainString();
if (s.endsWith(".0")) {
s = s.substring(0, s.length() - 2);
}
System.out.println(s);
}
}
//大佬的C++代码
#include<bits/stdc++.h>
using namespace std;
int ma(int a, int b)
{
return (a>b?a:b);
}
int main()
{
string s;
cin >> s;
string round;
int aftere = 0;
int eloc = s.length();
int i,j,k,l;
for(i = 0; i < s.length();i++)
{
if(s[i] == 'e')
eloc = i;
if(i > eloc)
{
aftere*=10;
aftere += (s[i]-'0');
}
}
round.resize(eloc-1);
j = 0;
int dotloc;
for(i = 0; i < eloc;i++)
{
if(s[i]!='.')
{
round[j] = s[i];
j++;
}
else
dotloc = j;
}
dotloc += aftere;
bool all0 = true;
for(i = dotloc; i < round.length();i++)
{
if(round[i] != '0')
all0 = false;
}
for(i = 0; i < ma(round.length(),dotloc);i++)
{
if(all0 && i == dotloc)
return 0;
if(i == dotloc)
cout << '.';
if(i < round.length())
cout << round[i];
else
cout << 0;
}
}