C语言90分求助!!!

B2092 开关灯

ymysleep @ 2022-04-25 20:13:38

#include<stdio.h>
#include<math.h>
int main()
{
    int arr[5000] = { 0 };
    int n = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (j % i == 0)
            {
                if (arr[j] == 0)
                    arr[j] = 1;
                else
                    arr[j] = 0;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 1)
        {
            printf("%d ", i);
        }
    }

    return 0;

}

by LJ07 @ 2022-04-25 20:21:03

@ymysleep 显然您数组开小了


by LJ07 @ 2022-04-25 20:21:29

5000不够,要开到5001


by qfpjm @ 2022-04-25 20:21:33

@ymysleep 把数组放外面,在开大一些就好了

#include<stdio.h>
#include<math.h>
int arr[5005];
int main()
{
    int n = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (j % i == 0)
            {
                if (arr[j] == 0)
                    arr[j] = 1;
                else
                    arr[j] = 0;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 1)
        {
            printf("%d ", i);
        }
    }

    return 0;

}

by ymysleep @ 2022-04-26 13:28:47

@LJ07 谢谢


by ymysleep @ 2022-04-26 13:29:12

@Ted_hjl 谢谢


by najja @ 2022-07-10 15:16:32


using namespace std; 
int m,n,a[5001]={1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,729,784,841,900,961,1024,1089,1156,1225,1296,1369,1444,1521,1600,1681,1764,1849,1936,2025,2116,2209,2304,2401,2500,2601,2704,2809,2916,3025,3136,3249,3364,3481,3600,3721,3844,3969,4096,4225,4356,4489,4624,4761,4900,5041,5184,5329,5476,5625,5776,5929,6084,6241,6400,6561,6724,6889,7056,7225,7396,7569,7744,7921,8100,8281,8464,8649,8836,9025,9216,9409,9604,9801,10000};
int main()
{
    cin>>n>>m;
    for(int i=0;i<5001;i++)
    {
        if(a[i]>n) break;
        cout<<a[i]<<" ";
    }
    cout<<endl;
 } ```

by najja @ 2022-07-10 15:17:00

he


by LeeJC @ 2022-10-17 19:18:21

不懂就问,我也是写C的,佬你这个math在这里是起什么作用的


|