开关门
刚开始所有的门都是开着的,那么我定义一个数组,数组内所有元素都是bool类型,先将所有值都赋值为1,并且开始循环,每一次将n的倍数做相反处理,一直到n时停止,最后数组内值为1的下标输出,这样就可以得到所有开着的门的序号。
#include<iostream>
using namespace std;
int main(){
int n;
bool x=1;
cin>>n;
int *a=new int[n+1];
for(int b=1;b<=n;b++){
a[b]=x;
}
for(int c=2;c<=n;c++){
for(int d=1;d<=n;d++){
if(d%c==0){
a[d]=!a[d];
}
}
}
for(int e=1;e<=n;e++){
if(a[e]==1){
cout<<e<<" ";
}
}
return 0;
}
查看13道真题和解析

