题目要求:输入一个数代表多少字节,输出其对应的GBytes/Mbytes/Kbytes/Bytes。
通俗举例:1024000Byte= 1024000/1024=1000Kbytes;余数可以不考虑,取整即可。
void ByteTransformFun_pithy(long long Byte)
{
static const char *p[]={"B", "KB", "MB", "GB"};
int i;
cout << Byte << " Byte = ";
for(i = 0; Byte >= 1024 && i < 3; ++i)
{
Byte = Byte >> 10; // 2^10 = 1024, 右移动相当于做除法
}
cout << Byte << p[i] << endl;
}
int main()
{
long long byte;
cout << "Please Input byte : ";
cin >> byte;
ByteTransformFun_pithy(byte);
return 0;
}
用long 表示长整型。 C中long 有符号64位整数,范围是-2^63-2^63 -1 Int64,而在C++语言里用的变量是long long。