假设有N盏灯(N为不大于5000的正整数)从1到N按顺序依次编号,初始时全为开启状态;有M个人......题目如图
作者:互联网
#include<stdio.h>
void Check(int* p, int m, int n) //数组计数每个数对应的因子个数
{
int i,j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i&&j<=m; j++)
if (i % j == 0)
p[i]++;
}
}
void IsPrime(int* p, int n) //判断奇数
{
printf("最终为关闭状态的灯有:");
for (int i = 1; i <= n; i++)
if (p[i] % 2 == 1) //若因子数为奇数则输出
printf("%d ", i);
}
int main()
{
int M, N; //M为学生数,N为灯个数
printf("请依次输入学生个数、灯个数:\n");
scanf("%d%d", &M, &N);
int* p = (int* )malloc((N+1) * sizeof(int)); //堆空间申请一个(N+1)单位的类数组空间
for (int i = 0; i <= N; i++) //初始化数组为0
p[i] = 0;
Check(p, M, N);
IsPrime(p, N+1);
return 0;
}
运行结果:
标签:盏灯,malloc,5000,int,......,个数,依次,数组,printf 来源: https://blog.csdn.net/Genius_bin/article/details/112264018