2022-06-18青少年软件编程(C语言)等级考试(一级)T5 开关灯

文章提供了多个C++代码示例,解释了如何解决开关灯问题,这是一个常见的编程练习,涉及循环控制和逻辑操作。代码展示了不同方法来模拟多个人依次开关灯的过程,最终输出关闭状态的灯的编号。此问题与电子学会组织的青少年编程等级考试中的题目相关。
摘要由CSDN通过智能技术生成

1.5编程基础之循环控制 31:开关灯

OpenJudge - 31:开关灯

1109:开关灯

信息学奥赛一本通(C++版)在线评测系统




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;
}




 


 




电子学会青少年编程等级考试

https://blog.csdn.net/dllglvzhenfeng/category_11735875.html

中国电子学会考评中心

中国电子学会考评中心

软件编程(C语言)

中国电子学会考评中心




电子学会 青少年编程等级考试 C语言 1级

电子学会 青少年编程等级考试 C语言 1级_dllglvzhenfeng的博客-CSDN博客

电子学会 青少年软件编程等级考试 C语言1级

电子学会 青少年软件编程等级考试 C语言1级_dllglvzhenfeng的博客-CSDN博客

全国青少年软件编程(C语言)等级考试试题 2019年9月(一级含答案)

全国青少年软件编程(C语言)等级考试试题 2019年9月(一级含答案)_c语言浮点数向零舍入_大鹏大鹏大鹏的博客-CSDN博客

青少年编程等级考试 电子学会 C语言 1级 51 、开关灯

青少年编程等级考试 电子学会 C语言 1级 51 、开关灯_dllglvzhenfeng的博客-CSDN博客

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dllglvzhenfeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值