HardDisk Drive
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 370 Accepted Submission(s): 212
Problem Description
Yesterday your dearcousin Coach Pang gave you a new 100MB hard disk drive (HDD) as a gift becauseyou will get married next year.
But you turned on yourcomputer and the operating system (OS) told you the HDD is about 95MB. The 5MBof space is missing. It is known that the HDD manufacturers have a differentcapacity measurement. The manufacturers think 1 “kilo” is 1000 but the OSthinks that is 1024. There are several descriptions of the size of an HDD. Theyare byte, kilobyte, megabyte, gigabyte, terabyte, petabyte, exabyte, zetabyteand yottabyte. Each one equals a “kilo” of the previous one. For example 1gigabyte is 1 “kilo” megabytes.
Now you know the size ofa hard disk represented by manufacturers and you want to calculate thepercentage of the “missing part”.
Input
The first line containsan integer T, which indicates the number of test cases.
For each test case,there is one line contains a string in format “number[unit]” where number is apositive integer within [1, 1000] and unit is the description of size whichcould be “B”, “KB”, “MB”, “GB”, “TB”, “PB”, “EB”, “ZB”, “YB” in shortrespectively.
Output
For each test case,output one line “Case #x: y”, where x is the case number (starting from 1) andy is the percentage of the “missing part”. The answer should be rounded to twodigits after the decimal point.
Sample Input
2
100[MB]
1[B]
Sample Output
Case #1: 4.63%
Case #2: 0.00%
Hint
分析:
首先得出两点重要的结论。1,假设单位为MB则每MB(人)转化为Q[MB](电脑)将损失百分比为(1-Q)。当有X[MB](人)转化为Y[MB](电脑)时,损失百分比依然为(1-Q)。2,1[KB](人)转化为[KB](电脑)的数量为1000/1024[KB](电脑),损失百分比为(1-1000/1024),则1[MB](人)转化为1[MB](电脑)损失百分比为:(1024^2-1000^2)/1024^2。以此类推,可以得出所有单位的损失百分比。
首先利用python算出各单位的顺势百分比为:
B losed 0.00%
KB losed 2.34%
MB losed 4.63%
GB losed 6.87%
TB losed 9.05%
PB losed 11.18%
EB losed 13.26%
ZB losed 15.30%
YB losed 17.28%
一.利用C++代码直接输出。
代码为:
#include
#include
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
int m;
char s[100];
double result;
scanf("%d[%s]",&m,s);
switch(s[0])
{
case'B':result=0.00;break;
case'K':result=2.34;break;
case'M':result=4.63;break;
case'G':result=6.87;break;
case'T':result=9.05;break;
case'P':result=11.18;break;
case'E':result=13.26;break;
case'Z':result=15.30;break;
case'Y':result=17.28;break;
}
printf("Case #%d: %.2lf%%\n",i,result);
}
return 0;
}
代码二(AC):
#include
#include
using namespace std;
double get(int i)
{
return (pow(1024.0,i)-pow(1000.0,i))/pow(1024.0,i);
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
int n,j;
char s[100];
double result;
scanf("%d[%s]",&n,s);
switch(s[0])
{
case'B':result=get(0);break;
case'K':result=get(1);break;
case'M':result=get(2);break;
case'G':result=get(3);break;
case'T':result=get(4);break;
case'P':result=get(5);break;
case'E':result=get(6);break;
case'Z':result=get(7);break;
case'Y':result=get(8);break;
}
printf("Case #%d: %.2lf%%\n",i,result*100.0);
}
return 0;
}
代码三(一直wrong):
#include//错误的代码
#include
using namespace std;
double get(int i)
{
return (pow(1024.0,i)-pow(1000.0,i))/pow(1024,i);
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
int n,j;
char s[100];
double result;
scanf("%d[%s]",&n,s);
switch(s[0])
{
case'B':result=get(0);break;
case'K':result=get(1);break;
case'M':result=get(2);break;
case'G':result=get(3);break;
case'T':result=get(4);break;
case'P':result=get(5);break;
case'E':result=get(6);break;
case'Z':result=get(7);break;
case'Y':result=get(8);break;
}
printf("Case #%d: %.2lf%%\n",i,result*100.0);
}
return 0;
}
代码三的get函数中 最后一个pow为POW(1024,i)而代码二中为POW(1024.0,I) 区别在此,所以一直wrong
且代码二与代码三的输出都是一样的,自己测试根本测试不出来有什么错误。以后一定要注意,在需要用浮点数的运算,把所有实常数都用浮点数表示。