2018 ACM/ICPC 沈阳站 J How Much Memory Your Code Is Using?

题目描述

In the C++ language, the values of variables are stored somewhere in the computer memory as zeros and ones. Our program does not need to know the exact location where a variable is stored since one can simply refer to it by its name.
What the program needs to be aware of is the kind of data stored in every variable. Storing a simple integer is not the same as storing a letter or a large floating-point number. Even though they are all represented by zeros and ones, they are not interpreted in the same way, and in many cases, they don’t
occupy the same amount of memory.
Fundamental data types, implemented directly by the language, represent the basic storage units supported natively by most systems. They can be mainly classified into four types.
Character types: They can represent a single character, such as ‘A’ or ‘$’. The most basic type is char, which is a one-byte character.
Numerical integer types: They can store a whole number value, such as 7 or 1024. They exist in a variety of sizes. The most basic one is int, which is a four-byte integer. long long is a larger one which occupies twice as many memories as int does. A wider type of integer int128 may appear in some
new systems, which is a 16-byte integer and rarely useful.
floating-point types: They can represent real values, such as 3.14 or 0.01, with different levels of precision, depending on which of the three floating-point types is used. float, double and long double correspond to int, long long and int128 where the former types hold the same amount of memory as the latter types used respectively.
Boolean type: The boolean type, known in C++ as bool, can only represent one of two states, true or false. It is a one-byte type.
Now you have a code in C++, n lines of which apply several variables and arrays. Also, I have checked it so I can claim that all variables and arrays in this code are global without any conflicts.
As a novice, each line in your code may only apply for one variable or one array; all types of these variables and arrays are mentioned above; any other types unmentioned can not appear in the code.
Your task in this problem is to calculate the number of memories in total that the code is using. Output your answer in Kibibyte (1 Kibibyte is 1024 bytes) and round it up to the nearest integer.

输入

The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 100.
For each test case, the first line contains a positive integer n, which is up to 1000, indicating the total number of lines to apply (i. e. allocate memory) for variables and arrays in the code. Each of the following n lines may declare a variable in the form
• type variable name;
or declare a new array in the form
• type array name[array size];
where type must be one name of the aforementioned types. All variable name and array name are distinct strings containing only lowercase letters whose lengths are up to 10, and all array size are positive integers which are up to 105. Except for spaces in or after the name of some type, no extra spaces are allowed in the input. In other words, we guarantee that no consecutive spaces appear in the input.

输出

For each test case, output a line containing “Case #x: y” (without quotes), where x is the test case number starting from 1, and y indicates the total number of allocated memories in Kibibyte which is rounded up to the nearest integer.

样例输入

2
8
bool a;
char b;
int c;
long long d;
__int128 e;
float f;
double g;
long double h;
1
int a[1000];

样例输出

Case #1: 1
Case #2: 4

提示

In the second sample case, the memory usage is 4000 bytes, which should be rounded up to 4 Kibibytes.

签到型的模拟,4000B的代码,总体来说也没什么坑,就是按题意模拟就ok。

代码实现:

/*
Look at the star
Look at the shine for U
*/
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define sl(x) scanf("%lld",&x)
using namespace std;
const int N = 1e6+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
char temp[1005],now[1005],s[1005];
int main()
{
    ll t,n,i,j,k,cas = 1;
    for(sl(t);t--;)
    {
        sl(n);
        ll ans = 0;
        for(i = 0;i < n;i++)
        {
            scanf("%s",temp);
            scanf("%s",now);
            if(strcmp(temp,"long") == 0)
            {
                getchar();
                gets(s);
                if(strcmp(now,"long") == 0)
                {
                    ll tt = 0,flag = 0;
                    for(j = 0;s[j];j++)
                        if(s[j] == '[')
                        {
                            j++;
                            flag = 1;
                            while(s[j] && s[j] != ']')
                            {
                                tt = tt*10+(s[j]-'0');
                                j++;
                            }
                        }
                    if(flag) ans += tt*8;
                    else ans += 8;
                }
                else
                {
                    ll tt = 0,flag = 0;
                    for(j = 0;s[j];j++)
                        if(s[j] == '[')
                        {
                            j++;
                            flag = 1;
                            while(s[j] && s[j] != ']')
                            {
                                tt = tt*10+(s[j]-'0');
                                j++;
                            }
                        }
                    if(flag) ans += tt*16;
                    else ans += 16;
                }
            }
            else if(strcmp(temp,"int")==0 || strcmp(temp,"float") == 0)
            {
                ll tt = 0,flag = 0;
                for(j = 0;now[j];j++)
                    if(now[j] == '[')
                    {
                        j++;
                        flag = 1;
                        while(now[j] && now[j] != ']')
                        {
                            tt = tt*10+(now[j]-'0');
                            j++;
                        }
                    }
                if(flag) ans += tt*4;
                else ans += 4;
            }
            else if(strcmp(temp,"double") == 0)
            {
                ll tt = 0,flag = 0;
                for(j = 0;now[j];j++)
                    if(now[j] == '[')
                    {
                        j++;
                        flag = 1;
                        while(now[j] && now[j] != ']')
                        {
                            tt = tt*10+(now[j]-'0');
                            j++;
                        }
                    }
                if(flag) ans += tt*8;
                else ans += 8;
            }
            else if(strcmp(temp,"__int128") == 0)
            {
                ll tt = 0,flag = 0;
                for(j = 0;now[j];j++)
                    if(now[j] == '[')
                    {
                        j++;
                        flag = 1;
                        while(now[j] && now[j] != ']')
                        {
                            tt = tt*10+(now[j]-'0');
                            j++;
                        }
                    }
                if(flag) ans += tt*16;
                else ans += 16;
            }
            else if(strcmp(temp,"char")==0 || strcmp(temp,"bool") == 0)
            {
                ll tt = 0,flag = 0;
                for(j = 0;now[j];j++)
                    if(now[j] == '[')
                    {
                        j++;
                        flag = 1;
                        while(now[j] && now[j] != ']')
                        {
                            tt = tt*10+(now[j]-'0');
                            j++;
                        }
                    }
                if(flag) ans += tt;
                else ans += 1;
            }
        }
            if(ans % 1024) ans  = ans/1024+1;
            else ans = ans/1024;
            printf("Case #%lld: %lld\n",cas++,ans);
    }
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值