Dear, the U disk you sent is not 4G~ (C++) kkmd66

Description:

Have you also noticed that the available space of purchased storage devices, such as U disks, mobile hard drives, etc., is always smaller than what the seller advertises. Buy a 4G U disk, but the actual available space is only 3.5G!

we know:

1TB = 1024GB
1GB = 1024MB
1MB = 1024KB
1KB = 1024B The
computer uses binary, 1024 = 210; and we use decimal in our daily life, the manufacturer uses an approximate value of 1000 instead of 1024 for convenience, namely:

1TB = 1000GB
1GB = 1000MB
1MB = 1000KB
1KB = 1000B
So the space of the U disk you buy will shrink. For example, we expect 4G space to have 4 * 1024 * 1024 * 1024 = 4294967296 bytes, but in reality there are only 4 * 1000 * 1000 * 1000 = 4000000000 bytes, which is 294967296 bytes less!

I hope you can help me develop a program: just enter the size of the USB stick and the program can tell me how many bytes are actually missing. For example, if you enter "4GB", the program will output "294967296"

Input:

1. There will be multiple groups of input data.
2. Each group of data occupies one row.
3. Each set of data consists of two parts: an integer n (0 <= n < 1000) in front, followed by a unit (B, KB, MB, GB, or TB). There is no space between the number and the unit.
4. If n is equal to 0, the input ends and the program exits. Note: The unit is not specified here, which means that entering "0B", "0KB", "0MB", "0GB" and "0TB" can exit the program.

Output:

1. Corresponding to the input, output the actual reduced storage space.
2. Each group of output occupies one line.

Sample Input:

256MB
512MB
4GB
256GB
320GB
512GB
999TB
0MB

Sample Output:

12435456
24870912
294967296
18877906944
23597383680
37755813888
99412116148224

#include <valarray>
#include "iostream"
#include "string"
#include "iomanip"

using namespace std;

/**
 * kkmd66
 * @return
 */

int main() {
    
    
       
    

    string str;

    while (cin >> str && str[0] != '0') {
    
    
       
    

        int number = atoi(str.substr(0, str.size() - 2).c_str());
        double result;

        if (str[str.size() - 2] == 'K') {
    
    
       
    
            result = number * (pow(1024, 1) - pow(1000, 1));
        }
        if (str[str.size() - 2] == 'M') {
    
    
       
    
            result = number * (pow(1024, 2) - pow(1000, 2));
        }
        if (str[str.size() - 2] == 'G') {
    
    
       
    
            result = number * (pow(1024, 3) - pow(1000, 3));
        }
        if (str[str.size() - 2] == 'T') {
    
    
       
    
            result = number * (pow(1024, 4) - pow(1000, 4));
        }
        cout << fixed << setprecision(0) << result << endl;
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324224745&siteId=291194637