1.5编程基础之循环控制 31:开关灯
1109:开关灯
C++参考代码一:
#include<iostream>
using namespace std;
int a[5001];
int main()
{
int b,c,flag=1,i,j;
cin>>b>>c;
for(j=1;j<=c;j++)
for(i=1;i<=b;i++)
{
if(i%j==0)
{
if(a[i]==1) a[i]=0;
else a[i]=1;
//if(a[i]==0) a[i]=1;
}
}
for(int i=1;i<=b;i++)
{
if(a[i]){
if( flag==1)
{
cout<<i;
flag=0;
}
else
{
cout<<","<<i;
}
}
}
return 0;
}
C++参考代码二:
/*
http://noi.openjudge.cn/ch0105/31/
*/
#include<iostream>
#include<cstring> //使用memset()函数须调用cstring
using namespace std;
int a[5001]; //全局变量
int main()
{
int n,m,f=1;
cin>>n>>m;
memset(a,0,sizeof(a)); //初始化置0,0表示开启状态
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
if(j%i==0)
a[j]=!a[j]; //i的倍数的灯做相反处理
for(int i=1;i<=n;i++)
if(a[i]) //1表示关闭状态
{
if(f) f=0; //第1次不用先输出逗号
else cout<<",";
cout<<i; //顺序输出关闭的灯的编号
}
return 0;
}
C++参考代码三:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m,x,y,j=0;
cin>>n>>m;
bool a[5001];
int b[5001];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(x=2;x<=m;x++)
{
for(y=x;y<=n;y+=x)
{
if(y%x==0)
{
a[y]=!a[y];
}
}
}
for(int i=1;i<=n;i++)
{
if(a[i]==0)
{
//cout<<i<<",";
b[j]=i;
//cout<<"b["<<j<<"]="<<b[j]<<endl;
j++;
}
}
//cout<<"j="<<j<<endl;
for(int k=0;k<j-1;k++)
{
cout<<b[k]<<",";
}
cout<<b[j-1]<<endl;
return 0;
}
C++参考代码四:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m,index=1;
bool on[5001];
cin>>n>>m;
//第一个人(1号)将灯全部关闭
memset(on,0,sizeof(on));
//第二个人(2号)将编号为2的倍数的灯打开,
//第三个人(3号)将编号为3的倍数的灯做相反处理
//(即,将打开的灯关闭,将关闭的灯打开)。
//依照编号递增顺序,以后的人都和3号一样,
//将凡是自己编号倍数的灯做相反处理。
for(int i=2;i<=m;i++)
{
for(int j=i;j<=n;j+=i)
{
on[j]=!on[j];
}
}
//顺次输出关闭的灯的编号,其间用逗号间隔。
for(int i=1;i<=n;i++)
{
if(on[i]==0)
{
if(index==1)
{
index=0;
cout<<i;
}
else cout<<","<<i;
}
}
cout<<endl;
return 0;
}
C++参考代码五:
#include <iostream>
using namespace std;
int main()
{
int N, M, i, j, index=0;
bool on[5001]; // 记录灯的开关状态
cin >> N >> M;
for (i=0; i<5001; i++) on[i] = false; // 第1人
for (i=2; i<=M; i++){ // 从第2个人开始模拟
for (j=i; j<=N; j+=i){
on[j] = !on[j];
}
}
for (i=1; i<=N; i++){
if (!on[i]){
if (index == 0) cout << i;
else cout << "," << i;
index++;
}
}
cout << endl;
return 0;
}
C++参考代码六:
#include<iostream>
using namespace std;
int light[5005];
int k=0,count[5005];
int first=1;
int main(){
int m,n;
cin>>n>>m;
for(int i=1;i<=n;i++)
light[i]=1;
for(int i=1;i<=m;i++){
for(int j=i;j<=n;j++){
if(j%i==0)
light[j]=light[j]*(-1);
}
}
/*
for(int i=1;i<=n;i++){
if(light[i]==-1)
count[++k]=i;
}
for(int i=1;i<k;i++){
cout<<count[i]<<",";
}
cout<<count[k]<<endl;
*/
for (int i=1;i<=n;++i)
{
if ( light[i]==-1 )
{
if(first) first=0;
else printf(",");
printf("%d",i);
}
}
printf("\n");
return 0;
}
C++参考代码七:、
/*
1.5编程基础之循环控制 31开关灯 蒋长灵(AC)
http://noi.openjudge.cn/ch0105/31/
*/
#include <bits/stdc++.h>
using namespace std;
int a[5001];
int main()
{
int n,m;
cin>>n>>m;
memset(a,0,sizeof(a));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(j%i==0)
{
if(a[j]==0)
{
a[j]=1;
}
else
{
a[j]=0;
}
}
}
}
int sum=0;
for(int i=1;i<=n;i++)
{
if(a[i]==1)
{
sum++;
}
}
int i=1;
while(sum)
{
if(a[i]==1)
{
cout<<i;
if(sum>1)
{
cout<<",";
}
sum--;
}
i++;
}
return 0;
}
电子学会青少年编程等级考试
中国电子学会考评中心
软件编程(C语言)
电子学会 青少年编程等级考试 C语言 1级
电子学会 青少年软件编程等级考试 C语言1级
全国青少年软件编程(C语言)等级考试试题 2019年9月(一级含答案)
全国青少年软件编程(C语言)等级考试试题 2019年9月(一级含答案)_c语言浮点数向零舍入_大鹏大鹏大鹏的博客-CSDN博客
青少年编程等级考试 电子学会 C语言 1级 51 、开关灯
青少年编程等级考试 电子学会 C语言 1级 51 、开关灯_dllglvzhenfeng的博客-CSDN博客