Practice_store_space
#include <bits/stdc++.h>
using namespace std;
string arr[20];
int main()
{
int T;
scanf("%d", &T);
int count = 0;
getchar();
for (int i = 0; i < T; i++)
{ // 输入T行字符串
getline(cin, arr[i]); // 存放字符串
for (int j = 0; j < arr[i].size(); j++)
{
int t = 1;
if (arr[i][j] == ' ')
{ // 判断当前行的定义类型
if (arr[i].substr(0, j) == "int")
{
for (int k = j + 1; k < arr[i].size(); k++)
{
if (arr[i][k] == ',')
{ // 一行多个定义
t++;
}
}
count += (4 * t);
}
else if (arr[i].substr(0, j) == "long")
{
for (int k = j + 1; k < arr[i].size(); k++)
{ // 一行多个定义
if (arr[i][k] == ',')
{
t++;
}
}
count += (8 * t);
}
else if (arr[i].substr(0, j) == "String")
{
int s = 0, y = 0;
for (int k = j + 1; k < arr[i].size(); k++)
{
if (arr[i][k] == '\"' && s == 0)
{ // 开始位置
y = k + 1;
s = 1;
}
else if (arr[i][k] == '\"')
{ // 结束位置
count += (k - y); // 统计
s = 0;
}
}
}
else if (arr[i].substr(0, j) == "int[]")
{
int s = 0, y = 0;
for (int k = j + 1; k < arr[i].size(); k++)
{
if (arr[i][k] == '[')
{ // 开始的位置
s = 1;
y = 0;
}
else if (arr[i][k] == ']')
{ // 结束的位置
s = 0;
count += y * 4; // 统计
}
else if (s == 1)
{ // 中间的数字
y *= 10;
y += arr[i][k] - '0';
}
}
}
else if (arr[i].substr(0, j) == "long[]")
{
int s = 0, y = 0;
for (int k = j + 1; k < arr[i].size(); k++)
{
if (arr[i][k] == '[')
{ // 开始的位置
s = 1;
y = 0;
}
else if (arr[i][k] == ']')
{ // 结束的位置
s = 0;
count += y * 8; // 统计
}
else if (s == 1)
{ // 中间的数字
y *= 10;
y += arr[i][k] - '0';
}
}
}
}
}
}
int a, b, c, d;
a = count % 1024;
count /= 1024; // 剩余的B
b = count % 1024;
count /= 1024; // 剩余的KB
c = count % 1024;
count /= 1024; // 剩余的MB
d = count % 1024; // 判断GB
if (d != 0)
{ // 从GB开始逐渐输出
printf("%dGB", d);
}
if (c != 0)
{
printf("%dMB", c);
}
if (b != 0)
{
printf("%dKB", b);
}
if (a != 0)
{
printf("%dB\n", a);
}
}
#include <bits/stdc++.h>
using namespace std;
void sa(long long ans)
{
if (ans < 1024)
{
cout << ans << "B";
}
else if (ans < 1048576)
{
int t1 = ans / 1024;
int t2 = ans - t1 * 1024;
cout << t1 << "KB" << t2 << "B";
}
else
{
int t1 = ans / 1048576;
int t2 = (ans - t1 * 1048576) / 1024;
int t3 = ans - t1 * 1048576 - t2 * 1024;
cout << t1 << "MB" << t2 << "KB" << t3 << "B";
}
}
int main()
{
// 请在此输入您的代码
int t;
cin >> t;
cin.ignore(); // 清除换行符
long long ans = 0;
int y = 0;
int ii[100000] = {0}; // int[]
int ans_iia[100000] = {0}; // int[]
int s1 = 0; // String中"的个数
int ss[500] = {0}; // String中"的下标
int m = 0;
int kk[100000] = {0}; // long[]中数字的下标
int ans_lla[100000] = {0}; // long[]
string s;
for (int i = 0; i < t; i++)
{
getline(cin, s);
int o = s.size();
if (s[0] == 'i')
{
if (s[3] != '[')
{
for (int j = 5; j < o; j++)
{
if (s[j] == '=')
ans += 4;
if (ans >= 1073741824)
{
cout << "1GB";
return 0;
}
}
} // int 4
else if (s[3] == '[')
{
for (int j = 5; j < o; j++)
{
if (s[j] == 'w')
{
int r = 0;
for (int k = j + 6; k < o; k++)
{
if (s[k] == ']')
{
j = k;
for (int p = 0; p < r; p++)
{
ans_iia[y] = ans_iia[y] * 10 + ii[p];
}
ans += ans_iia[y] * 4;
if (ans >= 1073741824)
{
cout << "1GB";
return 0;
}
// cout<<ans_ii<<endl;
y++;
break;
}
// 将long[]数字存入kk[]
ii[r] = (int)s[k] - 48;
// cout << s[k] << endl;
// cout << kk[r] << endl;
r++;
}
// for (int u = 0; u < k - 1; u++)
// {
// cout << kk[u] << endl;
// }
}
}
} // int[]
} // int
else if (s[0] == 'S')
{
for (int j = 7; j < o; j++)
{
if (s[j] == '"')
{
ss[s1] = j;
s1++;
}
if (ans >= 1073741824)
{
cout << "1GB";
return 0;
}
}
for (int p = 0; p < s1; p++)
{
ans = ans + ss[p + 1] - ss[p] - 1;
if (ans >= 1073741824)
{
cout << "1GB";
return 0;
}
p++;
} // String的字符串长度和
} // string
else if (s[0] == 'l')
{
if (s[4] != '[')
{
for (int j = 6; j < o; j++)
{
if (s[j] == '=')
ans += 8;
if (ans >= 1073741824)
{
cout << "1GB";
return 0;
}
}
} // long 8
else if (s[4] == '[')
{
for (int j = 7; j < o; j++)
{
if (s[j] == 'w')
{
int r = 0;
for (int k = j + 7; k < o; k++)
{
if (s[k] == ']')
{
j = k;
for (int p = 0; p < r; p++)
{
ans_lla[m] = ans_lla[m] * 10 + kk[p];
if (ans >= 1073741824)
{
cout << "1GB";
return 0;
}
}
ans += ans_lla[m] * 8;
m++;
break;
}
kk[r] = (int)s[k] - 48;
r++;
}
}
}
} // long[]
} // long
}
sa(ans);
return 0;
}