首页 > 代码库 > HDU 4788 (14.05.12)

HDU 4788 (14.05.12)

Hard Disk Drive

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 647    Accepted Submission(s): 350


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
2 100[MB] 1[B]
 

Sample Output
Case #1: 4.63% Case #2: 0.00%
Hint
 
题意: 就是商家生产硬盘时 计算大小时 换算数值是1000 而我们计算机是按1024算的 所以 商家生产的100MB大小硬盘 在电脑上就只剩95MB了 要我们计算丢失百分比.

做法: 其实我做到后面发现其实很简单的 就拿100MB来说 100*1000 /1024*1000/1024 = 95 .3674 这是计算机认为的量 然后 (1-95.3674/100)*100= 4.63 就是比率了, 如果其实把100换成其他数字 这个比率是不变的! 例如换成10 那么 10*1000/1024*1000/1024 = 9.53674 然后(1- 9.53674/10)*100 = 4.63 然后 既然这样的话 我就都用100来算...

AC代码:
#include<stdio.h>
#include<string.h>

int main() {
	double num;
	char str[50];
	int n, cas = 0;
	int count, len;
	scanf("%d", &n);
	getchar();
	while(n--) {
		gets(str);
		num = 100;
		count = 0;
		len = strlen(str);
		for(int i = 0; i < len; i++) {
			if(str[i] == ‘B‘) {
				if(str[i-1] == ‘K‘)
					count = 1;
				else if(str[i-1] == ‘M‘)
					count = 2;
				else if(str[i-1] == ‘G‘)
					count = 3;
				else if(str[i-1] == ‘T‘)
					count = 4;
				else if(str[i-1] == ‘P‘)
					count = 5;
				else if(str[i-1] == ‘E‘)
					count = 6;
				else if(str[i-1] == ‘Z‘)
					count = 7;
				else if(str[i-1] == ‘Y‘)
					count = 8;
			} 
		}

		if(count == 0)
			printf("Case #%d: 0.00%%\n", ++cas);
		else {
			while(count--) {
				num *= 1000;
				num /= 1024;
			}
			num = 100 - num;
			printf("Case #%d: %.2lf%%\n", ++cas, num);
		}
	}
	return 0;
}