Some info here abt the question:
Problem ID: 1325
Description
There are N lamps placed in a row, sequentially numbered from 1 to N. And N people are also numbered sequentially from 1 to N. The first person means to turned off all the lights , The Second person means to turn on the lamp with multiples of 2; The 3rd person means to reverse the status of lamps with multiples of 3(if the lamp is on, turn it off; if it is off, turn it on) ). The people next them will reverse the status of the lights with number of their own multiples as the 3rd person.
nput
Input: n (n <= 100), the number of lights,
Output
Output: Status of the light, sequence 01, no spaces in between.
Examples
Input 1
2
Output 1
01
Examples Explanation
The first person means to turned off all the lights, the second person means to turn on the lamp with multiples of 2. After the operation, the status of the lamps is 01.
Constraint
n<=100
My code is:
#include <iostream>
using namespace std;
int main(){
int a[101], n;
cin >> n;
for(int i=1; i <= n; i++){
if(i==1){
for(int j=1; j<=n; j++) a[i] = 0;
}
else if(i==2){
for(int j=1; j<=n; j++){
if(j%2==0) a[i]=1;
}
}
else{
for(int j=1; j<=n; j++){
if(j%i==0){
if(a[j]==1) a[j]=0;
else if(a[j]==0) a[j]=1;
}
}
}
}
for(int i=1; i<=n; i++){
cout << a[i];
}
return 0;
}
Someone tell me what’s wrong? Thanks