Hard Disk Drive
Problem Description
Yesterday your dear cousin Coach Pang gave you a new 100MB hard disk drive (HDD) as a gift because you will get married next year.
But you turned on your computer and the operating system (OS) told you the HDD is about 95MB. The 5MB of space is missing. It is known that the HDD manufacturers have a different capacity measurement. The manufacturers think 1 “kilo” is 1000 but the OS thinks that is 1024. There are several descriptions of the size of an HDD. They are byte, kilobyte, megabyte, gigabyte, terabyte, petabyte, exabyte, zetabyte and yottabyte. Each one equals a “kilo” of the previous one. For example 1 gigabyte is 1 “kilo” megabytes.
Now you know the size of a hard disk represented by manufacturers and you want to calculate the percentage of the “missing part”.
Input
The first line contains an 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 a positive integer within [1, 1000] and unit is the description of size which could be “B”, “KB”, “MB”, “GB”, “TB”, “PB”, “EB”, “ZB”, “YB” in short respectively.
Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the percentage of the “missing part”. The answer should be rounded to two digits after the decimal point.
Sample Input
Sample Output
Case #1: 4.63%
Case #2: 0.00%
这道题是成都现场赛重放的,额。。怎么说呢,特别水的一道题把。。。 题意大概就是:计算机专业的人知道容量之间的转换是1024即2^10,而非计算机专业的认为是1000即10^3, 假设,我们按10^3算,那么和原来按2^10的比,差了总量的百分之多少? 当然容量不可能只有KB到B之间的转换还有MB GM TB PB EB ZB YB。 系数基本上就不用管了,答案肯定是 1-(系数*10^3*相应转换的阶级)/(系数*2^10*相应阶级) 所以系数就会约去,然后差一个阶级就是差一个 1000/1024, 代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int t,mem,jie;
int i;
double a,arr[10];
string str;
for(i=1;i<=8;++i)
{
a=1000.0/1024;
arr[i]=(1-pow(a,i))*100;
}
scanf("%d",&t);
for(i=1;i<=t;++i)
{
cin>>mem>>str;
if(str[1]=='B') jie=0;
else if(str[1]=='K') jie=1;
else if(str[1]=='M') jie=2;
else if(str[1]=='G') jie=3;
else if(str[1]=='T') jie=4;
else if(str[1]=='P') jie=5;
else if(str[1]=='E') jie=6;
else if(str[1]=='Z') jie=7;
else jie=8;
if(jie==0)
{
printf("Case #%d: %.2f%%\n",i,mem);
continue;
}
printf("Case #%d: %.2f%%\n",i,arr[jie]);
}
return 0;
}