10分求助

B2092 开关灯

Jason101 @ 2023-08-11 11:27:47

源代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int N,M,s;
    s=0;
    cin>>N>>M;
    cout<<1;
    for(int i=2;i<=N;i++){
        s=0;
       for(int j=1;j<=M;j++)
        {
         if(i%j==0)
         s=s+1;
        }
       if(s%2!=0) cout<<","<<i;
    }
    return 0;
}

by Wyttie @ 2023-08-11 11:32:08

@Dodgehqs101 输入为一行,一个整数N,为灯的数量。你有两个输入


by Jason101 @ 2023-08-12 07:38:02

@Wyttie 改了,谢谢。


by Score_Elevate @ 2023-09-10 17:52:53

@Dodgehqs101 改好了你思路全错

#include<bits/stdc++.h>

using namespace std;

inline int read()
{
    int x = 0, y = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
      if (c == '-')
        y = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9')
      x = x * 10 + c - '0', c = getchar();
    return x * y;   
}

bool f[5000 + 5];
int n;

int main()
{
    memset (f, 0, sizeof (f));//认真审题可知编号为一的缺德玩意把所有灯都给关了
    n = read();
    for (int i = 2; i <= n; i++)//从2开始判断
      for (int j = 2; j <= i; j++)//内循环的j必须<i,因为除数只能是从2到自己编号
        if (i % j == 0) f[i] = !f[i];//判断是否能整除,如果能则将其做负处理
    for (int i = 1; i <= n; i++)
      if (f[i] == 0) cout << i << " ";//0为关灯,如果灯是关的则输出编号
    return 0;
}

求个关注,不可以白嫖,从你我做起


|