CodeForces 697B Barnicle(学习scanf(),科学计数法的转换)

http://codeforces.com/problemset/problem/697/B

B. Barnicle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate.

Barney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance value (it's a real number) on a napkin. The problem is that he wrote it in scientific notation. The scientific notation of some real number x is the notation of form AeB, where A is a real number and B is an integer and x = A × 10B is true. In our case A is between 0 and 9 and B is non-negative.

Barney doesn't know anything about scientific notation (as well as anything scientific at all). So he asked you to tell him the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding.

Input

The first and only line of input contains a single string of form a.deb where ad and b are integers and e is usual character 'e' (0 ≤ a ≤ 9, 0 ≤ d < 10100, 0 ≤ b ≤ 100) — the scientific notation of the desired distance value.

a and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Also, b can not be non-zero if a is zero.

Output

Print the only real number x (the desired distance value) in the only line in its decimal notation.

Thus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes.

Otherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be equal to zero), and q is an integer that have no trailing zeroes (and may not be equal to zero).

Examples
input
8.549e2
output
854.9
input
8.549e3
output
8549
input
0.33e0
output
0.33

题意:

将科学计数法表示的数字转化为一般数字(有看不懂题意的童鞋也是无语了)


思路:

直接模拟吧,模拟了一个钟,结果卡在特殊数据上,代码反复修改,自认为可以了之后又卡了一组数组

Input
1.0e0
Output
1.0
Answer
1
这内心简直是无奈的,最后乖乖的去官网找题解,也学到了新的知识,
		printf("%d%.*s.%s\n",a,b,d,d+b);
		scanf("%[^e]%ne%d",d,&l,&b);
不懂得看这个博客 ----->   点击打开链接


AC Code:

#include<stdio.h>
#include<cstring>
const int MYDD=1103;

int main() {
	int a,k,b;
	char d[MYDD];
	scanf("%d.",&a); //两个scanf不可合为一个
	scanf("%[^e]%ne%d",d,&k,&b);
	//note: "%[^e]" 读入任意多的字符,直到遇到 "=" 停止
	if(k==1&&d[0]==48&&!b) 	printf("%d\n",a);
	//note: 这里要是改为字符型判断d[0]=='0'时间复杂度增加一倍,可达到 30ms 
	else if(b>=k)				printf("%d%s%.*d\n",a,d,b-k,0);
	else 						printf("%d%.*s.%s\n",a,b,d,d+b);
						// 参数 b 输出字符串 d 的个数
	return 0;
}
 

Bug Code:

#include<stdio.h>
#include<cstring>
const int MYDD=1103;

char op[MYDD];
char ans[MYDD];


int main() {
	while(scanf("%s",op)!=EOF) {
		int lenop=strlen(op);
		int d,e;
		for(int j=0; j<lenop; j++) {
			if(op[j]=='.')	d=j;//确定小数点
			if(op[j]=='e')	e=j;//确定 e
		}

		int sumop=0;
		for(int j=0; j<e; j++) {
			if(op[j]!='.')
				sumop=sumop+op[j]-'0';
		}
		if(sumop==0) {//如下情况的处理 0.000000e4等  
			puts("0");
			continue;
		}

		int sumeright=0;// 将 e 的次方字符型转化为整数型
		for(int j=e+1; j<lenop; j++) {
			sumeright=sumeright*10+(op[j]-'0');
		}
		int demiddle=e-d-1;// 小数点和 e 之间的数字个数,即小数部分
		if(sumeright>=demiddle) {
			int remain=sumeright-demiddle;
			int k=0;
			for(int j=0; j<lenop; j++) {
				if(op[j]=='.')	continue;
				if(op[j]=='e')	break;
				ans[k]=op[j];
				k++;

			}
			for(int j=0; j<remain; j++)
				ans[k++]='0';
			printf("%s\n",ans);
		} else {
			int k=0;
			int j;
			for(j=0; j<d+sumeright+1; j++) {
				if(op[j]=='.')	continue;
				ans[k]=op[j];
				k++;
			}
			ans[k++]='.';
			while(op[j]!='e') {
				ans[k]=op[j];
				j++,k++;
			}
			printf("%s\n",ans);
		}
	}
	return 0;
}
/*
Input
1.0e0
Output
1.0
Answer
1

*/ 


阅读更多

更多精彩内容