首页 > 试题广场 >

计算机内存

[编程题]计算机内存
  • 热度指数:15607 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}我们可以看到题目描述的上方有一个空间限制 256\,\mathrm{MB}。在计算机中,一个整数占据 4 字节的内存;
\hspace{23pt}\bullet\,1\,\mathrm{MB}=1024\,\mathrm{KB}
\hspace{23pt}\bullet\,1\,\mathrm{KB}=1024\,\mathrm{B}
\hspace{23pt}\bullet\,1\,\mathrm{B}=1 字节。
\hspace{15pt}那么,给定 n MB 的内存,请问可以存储多少个整数?

输入描述:
\hspace{15pt}在一行中输入一个整数 n1 \leqq n \leqq 256),表示内存大小(单位 MB)。


输出描述:
\hspace{15pt}输出一个整数,表示在 n MB 内存中最多可以存储的整数个数。
示例1

输入

1

输出

262144

说明

\hspace{15pt}1\,\mathrm{MB}=1024\times1024\,\mathrm{B}=2^{20}\,\mathrm{B},每个整数占用 4 字节,因此可存储 2^{20}/4=262144 个整数。
示例2

输入

256

输出

67108864

说明

\hspace{15pt}256\,\mathrm{MB}=256\times2^{20}\,\mathrm{B},每个整数占用 4 字节,因此可存储 256\times2^{20}/4=256\times2^{18}=67108864 个整数。

备注:
本题已于下方时间节点更新,请注意题解时效性:
1. 2025-06-03 优化题面文本与格式。
#include <iostream>
using namespace std;


int main(){
    int n; 
    cin >> n;
    n <<= 18;
    cout << n << endl;
    return 0;
}

发表于 2025-12-08 01:40:28 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        System.out.println((int)(a*Math.pow(2,18)));
    }
}
发表于 2025-08-12 15:55:57 回复(0)
s = int(input())
print( f"{s *1024*1024//4}")
发表于 2025-07-17 11:03:02 回复(0)
用位运算解决
由于n的单位是MB,可以将n左移20位(1MB=2^20 B),再将n右移2位(int占4字节,4=2^2)即可得到结果

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n=s.nextInt();
        n<<=20;
        n>>=2;
        System.out.println(n);
    }
}

发表于 2025-06-30 12:17:23 回复(0)
n = int(input())
if 1<=n<=256 :
    a = n*1024*1024
    n_1 = a//4
    print(n_1)
发表于 2025-12-16 18:16:49 回复(0)
mb = int(input())
b = mb*(2**20)
int_typt = int(b/4)
print(int_typt)
发表于 2025-12-07 12:04:38 回复(0)
int main() {
    long long  a;
    cin >> a;
    cout << (a << 18);
}
发表于 2025-12-06 10:51:10 回复(0)
n = int(input())
print(f"{(n*1024*1024)/4:.0f}")

发表于 2025-11-09 22:23:08 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    int n = 0,num = 0;
    scanf("%d",&n);

    num = (n * pow(2,20))/4;
    printf("%d",num);
    return 0;
}

发表于 2025-11-06 21:55:43 回复(0)
n = int(input())
print(n << 18)
   
发表于 2025-10-27 22:02:23 回复(0)
#include <stdio.h>

int main() {
    int x,a;
    scanf("%d",&x);
    a =x*1024*1024/4;
    printf("%d",a);
    return 0;
}
发表于 2025-10-26 16:00:34 回复(0)
#include <stdio.h>

int main() {
    int n = 0;
    long int count = 0;

    scanf("%d\r\n", &n);

    if (n >= 1 && n <= 256) {

        //   count = (1024 * 1024 * n) / 4;

        count = 262144 * n;

        printf("%ld\r\n", count);

        return 0;

    } else {

        printf("输入的数字有误,请重新输入\r\n");

        return -1;

    }
   
}
发表于 2025-10-02 20:56:06 回复(0)
#include <stdio.h>
#include<math.h>
int main() {
    int  a;
    int b = pow(2, 20);
    scanf("%d", &a);

    printf("%d", a * b / 4);
    return 0;
}

发表于 2025-09-17 22:44:22 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        double byte_num = 0;
        if(number>=1&&number<=256)
        {
            byte_num=number*(Math.pow(2,20)/4);
        }
        System.out.println(String.format("%.0f",byte_num));
    }
}
发表于 2025-09-15 00:14:37 回复(0)
#include<iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    cout << n * 1024 * 1024 / 4;
}
发表于 2025-09-13 18:47:28 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    int n;
    scanf("%d",&n);
    int a=n*(pow(1024,2));
    int final=a/4;
    printf("%d",final);
    return 0;
}
发表于 2025-09-08 09:56:23 回复(0)
import sys

a = int(input())
print(262144 * a)
发表于 2025-08-10 13:08:53 回复(0)
n = int(input())
print(n*1024**2//4)
发表于 2025-08-06 16:25:38 回复(0)

也许位运算是最快的解决方式,代码中含有注释。

#include <stdio.h>

int main() {

    int n = 0;

    scanf("%d", &n);

    /*
    * n << 18 相当于 n * 1024 * 1024 / 4 ==> n * 2^10 * 2^10 * 2^{-2}
    * ==> n * 2^{10 + 10 -2} ==> n * 2^18
    * 
    * 整数的乘除法与左/右移的关系:
    * 1. 除法
    *   n / 2 ==> n / 2^1 ==> n >> 1
    *   n / 4 ==> n / 2^2 ==> n >> 2
    * 2. 乘法
    *   n * 2 ==> n * 2^1 ==> n << 1
    *   n * 4 ==> n * 2^2 ==> n << 2
    */
    int result = n << 18;

    printf("%d", result);

    return 0;
}
发表于 2025-08-05 19:54:43 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a;

    b = a*(1024*1024)/4;
    cout << b << endl;
}
// 64 位输出请用 printf("%lld")
发表于 2025-08-05 15:48:11 回复(0)