c_cpp GFG-门

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp GFG-门相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>
using namespace std;

// http://www.geeksforgeeks.org/puzzle-16-100-doors/

int main(){

    int n;
    // cout<<"Enter the num of doors"<<endl;
    cin>>n;
    vector<int> a;
    for(int i=1;i<=n;i++){
        int count=0;
        for(int j=1;j<=n;j++){
            //he toggles the ith door when going jth time
            //if i is divisible by j
            //eg: 3rd door is toggled for 1st,3rd time
            if(i%j==0){
                count++;
            }
        }
        if(count%2!=0){
            a.push_back(i); //doors were initially closed
                            //after even operations it will still stay closed
        }
    }
    for(int i=0;i<a.size();i++){
        cout<<a[i]<<" ";
    }
    return 0;
}

以上是关于c_cpp GFG-门的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp GFG发现号码

c_cpp GFG排序矩阵行

c_cpp GFG最低成本路径

c_cpp GFG重复和失踪

c_cpp GFG谁将获胜

c_cpp GFG按特定顺序排序