洛谷 开关灯 (计蒜客 - T1079)

洛谷 开关灯 (计蒜客 - T1079)_第1张图片

#include
#include
using namespace std; 
int main(){
	int n;//int m;//人 
	cin>>n>>m;
	int a[n];
	for (int i = 1;i <= n;i++){
		a[i] = 0;
	}
	for (int i = 2;i <= n;i++){
		if (i % 2 == 0)
			a[i] = 1;
	}
	for (int i = 3;i <= m;i++){
		for (int j = 1;j <= n;j++){
			if (j % i == 0)
				if (a[j] == 0) a[j] = 1;
				else if (a[j] == 1) a[j] = 0;
		}
	}
	int num = 0;
	for (int i = 1;i <= n;i++){
		if (a[i] == 0){
			num++;
		}
	}
	for (int i = 1,j = 0;i <= n;i++){
		if (a[i] == 0){
			cout<<i;
			j++;
			if (j < num){
				cout<<','; 
			}
		}
	}
    return 0;
}

你可能感兴趣的:(C++,学习,c++)