一道简单题,题意是我们平常生活中见到的例子,平常的u盘,说是8G,但在电脑上显示的是7G多一点儿,是什么原因呢?这道题目给出了解释,为了方便计算,生产商的容量是按照1000算的,而在计算机中容量是按1024算的,所以就出现了偏差,这道题目让计算指定的容量,相对来说丢失了多少容量。
直接计算就可以了,可以使用pow函数来快速计算。最后要输出百分数,所以要乘以100。
代码如下:
/*************************************************************************
> File Name: c.cpp
> Author: gwq
> Mail: gwq5210@qq.com
> Created Time: 2014年10月29日 星期三 22时23分42秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
char type[][10] = {"[B]", "[KB]", "[MB]", "[GB]", "[TB]", "[PB]", "[EB]", "[ZB]", "[YB]"};
int main(int argc, char *argv[])
{
int t;
scanf("%d", &t);
for (int cs = 1; cs <= t; ++cs) {
int num;
int pos = 0;
char str[10];
double ans = 0.0;
scanf("%d%s", &num, str);
for (int i = 0; i < 9; ++i) {
if (strcmp(str, type[i]) == 0) {
pos = i;
break;
}
}
if (pos == 0) {
ans = 0.0;
} else {
ans = 100.0 * (1.0 - pow(1000.0, pos) / pow(1024.0, pos));
}
printf("Case #%d: %.2f%%\n", cs, ans);
}
return 0;
}