2022第十三届蓝桥杯决赛C/C++大学A组-C题内存空间
来源:华佗健康网
刷题链接:
https://www.dotcpp.com/oj/train/1037/
第一题:内存空间,大模拟题,中等
题目一:C题 内存空间
真就是一个大模拟题,快速读完题,能记住多少就记多少,比赛的时候要静下心来快速地模拟完,不慌不乱,将五种情况都考虑完并且测试成功后基本提交就没有什么问题。
过程中还要注意一下数据范围,要开Longlong存储等,基本这种模拟题是不需要去考虑时间复杂度的。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int t;
string sa;
typedef long long LL;
LL sum=0;
int main()
{
cin>>t;
for(int i=0;i<t;i++)
{
string type;
cin>>type;
LL num=0;
if(type[type.length()-1]==']') //是数组定义
{
type=type.substr(0,type.length()-2); //去掉[]
getline(cin,sa); //后面的变量初始化
int Size=0;
if(type=="int") Size=4;
else if(type=="long") Size=8;
int len=sa.length();
int l=-1,s=0;
for(int i=0;i<len;i++)
{
if(sa[i]=='[') //值开始
{
l=0;
s=i+1;
}
else if(sa[i]==']') //值结尾
{
string tmp=sa.substr(s,l);
LL temp=atoi(tmp.c_str()); //转化为整数
num+=Size*temp;
l=-1;
}
else if(l!=-1) l++;
}
}
else //普通变量定义
{
cin>>sa; //后面的变量初始化
int len=sa.length();
if(type=="String")
{
int l=-1;
for(int i=0;i<len;i++)
{
if(sa[i]=='"')
{
if(l==-1) l=0; //值开始
else //值结尾
{
num+=l;
l=-1;
}
}
else if(l!=-1) l++;
}
}
else
{
LL n=0;
for(int i=0;i<len;i++)
{
if(sa[i]==',') n++;
}
n+=1; //变量个数为逗号数加一
if(type=="int") num+=n*4;
else if(type=="long") num+=n*8;
}
}
sum+=num;
}
LL tmp=pow(2,30);
if(sum/tmp)
{
cout<< sum/tmp<<"GB";
sum%=tmp;
}
tmp=pow(2,20);
if(sum/tmp)
{
cout<< sum/tmp<<"MB";
sum%=tmp;
}
tmp=pow(2,10);
if(sum/tmp)
{
cout<< sum/tmp<<"KB";
sum%=tmp;
}
if(sum) cout<<sum<<"B";
return 0;
}
/*
5 //包含所有情况
long[] a1=new long[10],a2=new long[131072];
int[] a1=new int[10];
int a=1,b=5,c=6;
long a=1,b=5;
String s1=””,s2=”hello”,s3=”world”;
*/
因篇幅问题不能全部显示,请点此查看更多更全内容