标签:style blog http io 2014 for
题目链接:uva 1530 - Floating Point Numbers
题目大意:给出一个16位的二进制数,用来表示一个浮点数,第一位为符号,1~7位表示一个十进制的数s,e=63-s;剩下的8位为小数部分,默认整数部分为1,得到f,然后最后a=f*2^e,要求用科学计数法输出a。
解题思路:模拟就好了,注意0的情况特殊处理,以及科学计数法的整数部分不能为0.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> const int N = 20; const double eps = 1e-6; char s[N]; int main () { printf("Program 6 by team X\n"); while (scanf("%s", s) == 1) { if (strcmp(s+1, "000000000000000") == 0) { printf(" 0.000000e+000\n"); continue; } int sign = (s[0] == ‘1‘ ? 1 : 0); int e = 0; for (int i = 1; i <= 7; i++) { if (s[i] == ‘1‘) e += (1<<(7-i)); } double a = 1; for (int i = 8; i < 16; i++) { if (s[i] == ‘1‘) a += pow(2, 7-i);; } a *= pow(2, e-63); e = log10(a); a /= pow(10, e); if (a < 1) { a *= 10; e--; } printf("%c", sign ? ‘-‘ : ‘ ‘); printf("%.6lfe", a); printf("%c", e < 0 ? ‘-‘ : ‘+‘); printf("%03d\n", abs(e)); } printf("End of program 6 by team X\n"); return 0; }
uva 1530 - Floating Point Numbers(数论),码迷,mamicode.com
uva 1530 - Floating Point Numbers(数论)
标签:style blog http io 2014 for
原文地址:http://blog.csdn.net/keshuai19940722/article/details/24738549