蓝桥杯2022年第十三届决赛真题-内存空间(C/C++/Java组)

题目如下:

小蓝最近总喜欢计算自己的代码中定义的变量占用了多少内存空间。

为了简化问题,变量的类型只有以下三种:

int:整型变量,一个 int 型变量占用 4 Byte 的内存空间。

long:长整型变量,一个 long 型变量占用 8 Byte 的内存空间。

String:字符串变量,占用空间和字符串长度有关,设字符串长度为 L,则字符串占用 L Byte 的内存空间,如果字符串长度为 0 则占用 0 Byte 的内存空间。

定义变量的语句只有两种形式,第一种形式为:

type var1=value1,var2=value2…;

定义了若干个 type 类型变量 var1、var2…,并且用 value1、value2 …初始化,

多个变量之间用’,’ 分隔,语句以’;’ 结尾,type 可能是 int、long 或 String。例如 int a=1,b=5,c=6; 占用空间为 12 Byte;long a=1,b=5; 占用空间为 16 Byte;String s1=””,s2=”hello”,s3=”world”; 占用空间为 10 Byte。

第二种形式为:

type[] arr1=new type[size1],arr2=new type[size2]…;

定义了若干 type 类型的一维数组变量 arr1、arr2…,且数组的大小为 size1、size2…,多个变量之间用’,’ 进行分隔,语句以’;’ 结尾,type 只可能是 int 或 long。例如 int[] a1=new int[10]; 占用的内存空间为 40 Byte;long[] a1=new long[10],a2=new long[10]; 占用的内存空间为 160 Byte。

已知小蓝有 T 条定义变量的语句,请你帮他统计下一共占用了多少内存空间。结果的表示方式为:aGBbMBcKBdB,其中 a、b、c、d 为统计的结果,GB、MB、KB、B 为单位。优先用大的单位来表示,1GB=1024MB, 1MB=1024KB,1KB=1024B,其中 B 表示 Byte。如果 a、b、c、d 中的某几个数字为 0,那么不必输出这几个数字及其单位。题目保证一行中只有一句定义变量的语句,且每条语句都满足题干中描述的定义格式,所有的变量名都是合法的且均不重复。题目中的数据很规整,和上述给出的例子类似,除了类型后面有一个空格,以及定义数组时 new 后面的一个空格之外,不会出现多余的空格。

输入格式

输入的第一行包含一个整数 T ,表示有 T 句变量定义的语句。

接下来 T 行,每行包含一句变量定义语句。

输出格式

输出一行包含一个字符串,表示所有语句所占用空间的总大小。

样例输入

1
long[] nums=new long[131072];

样例输出

1MB

题目连接

题解 or 思路:

模拟

AC 代码如下:

/*
Make it simple and keep self stupid
author:Joanh_Lan
*/
#pragma GCC optimize(3)
#pragma GCC optimize("inline") // 如果比赛允许开编译器优化的话,可以默写这两段
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <bitset>
#include <set>
#include <random>
#include <ctime>
#include <queue>
#include <stack>
#include <climits>
#define buff                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);
#define int long long
#define ll long long
#define PII pair<int, int>
#define px first
#define py second
typedef std::mt19937 Random_mt19937;
Random_mt19937 rnd(time(0));
using namespace std;
const int mod = 1e9 + 7;
const int inf = 2147483647;
const int N = 100009;
//int Mod(int a,int mod){return (a%mod+mod)%mod;}
//int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
//int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
//int inv(int a,int mod){return qmi(a,mod-2,mod);}
//int lcm(int a,int b){return a*b/__gcd(a,b);}
string str;
int to_num(int l, int r)
{
	int ans = 0;
	for (int i = l; i <= r; i++)
		ans = ans * 10 + str[i] - '0';
	return ans;
}
int calc(int l, int r, int ans)
{
	if (ans == 1)
	{
		for (int i = l; i <= r; i++)
		{
			if (str[i] == '"')
				return r - i - 1;
		}
	}
	bool f = 0;
	int numl = 0, numr = 0;
	bool ok = 0;
	for (int i = l; i <= r; i++)
	{
		if (str[i] == '[')
			f = 1;
		if (str[i] >= '0' && str[i] <= '9' && ok)
			if (!numl)	numl = numr = i;
			else numr = i;
		if (str[i] == '=')	ok = 1;
	}
	if (!f)
		return ans;
	else
		return ans * to_num(numl, numr);
}
int work()
{
	// getchar();
	getline(cin, str);
	// cout << str << '\n';
	int sum = 0, l = 0;
	int ans = 0;
	if (str[l] == 'i')	ans = 4;
	else if (str[l] == 'l')	ans = 8;
	else ans = 1;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == ',' || str[i] == ';')
			sum += calc(l, i - 1, ans), l = i + 1;
	}
	return sum;
}
void solve()
{
	int n;	cin >> n;
	getchar();
	int ans = 0;
	while (n--)
		ans += work();
	// cout << ans << '\n';
	int GB = ans / 1024 / 1024 / 1024;
	ans -= GB * 1024 * 1024 * 1024;
	int MB = ans / 1024 / 1024;
	ans -= MB * 1024 * 1024;
	int KB = ans / 1024;
	ans -= KB * 1024;
	bool f = 0;
	if (GB)
		cout << GB << "GB", f = 1;
	if (MB)
		cout << MB << "MB", f = 1;
	if (KB)
		cout << KB << "KB", f = 1;
	if (ans)
		cout << ans << "B", f = 1;
	if (!f)
		cout << "0B";
	cout << '\n';
}
signed main()
{
	// buff;
	int _ = 1;
	// cin >> _;
	while (_--)
		solve();
}
/*
4
int a=0, b=0;
long x=0,y=0;
String String s1=”hello”,s2=”world”;
long[] arr1=new long[100000],arr2=new long[100000];
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joanh_Lan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值