翻牌子问题

有n块牌子,正面白色,反面黑色,编号为1至n。第1个人把所有的牌子翻成正面,第2个人把所有编号为2的倍数的牌子翻成反面,第3个人把所有编号为3的倍数的牌子翻个面(即原来正面的翻成反面,原来反面的翻成正面),以此类推。一共有k个人,问最后哪些牌子是正面的?

Input

n和k。   (k<=n<=1000)

Output

正面牌子的编号,两两之间以空格分开。

Sample Input

7 3

Sample Output

1 5 6 7
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,k,number;
    int cnt;
    while(cin>>n) {
        cin>>k;
        bool flag=0;
        for(int i=1; i<=n; i++) {
            number=i;
            cnt=0;
            for(int j=2; j<=k; j++) {
                if(number%j==0) {
                    cnt++;
                }
            }
            if(cnt%2==0) {
                if(flag)cout<<" ";
                flag=1;
                cout<<number;
            }
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44061561/article/details/94589244