A school has n lockers and n students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (close it if it was open and opens it if it is closed). Student S4 begins with locker L4 and changes every fouth locker. Student S5 starts with L5 and changes every fifth locker, etc, until student Sn change Ln.
After all the student have passed through the building and changed the lockers, how many lockers are open? Write a program to find your answer.
For example, if n is 5, then at fir the 5 lockers are in the following status:
L L L L L
After S1's action, we get
O O O O O
After S2's action, we get
O L O L O
After S3's action, we get
O L L L O
After S4's action, we get
O L L O O
After S5's action, we get
O L L O L
So the locker 1 and 4 are open, and number of lockers which are open is 2.
The first line is a positive integer t for the number of test cases.
Each test case contains one line with an integers n (1<=n<=1000).
For each test case, output "test case #testcaseNum:" in first line, then output the number of lockers which are open in the next line.
2 5 100
test case 1: 2 test case 2: 10
01.
// Problem#: 13049
02.
// Submission#: 3560390
03.
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
05.
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
06.
#include <iostream>
07.
#include <cstdio>
08.
using
namespace
std;
09.
10.
int
n, t;
11.
int
f[1100];
12.
13.
int
main()
14.
{
15.
scanf
(
"%d"
, &t);
16.
for
(
int
cas = 1; cas <= t; cas++) {
17.
scanf
(
"%d"
, &n);
18.
for
(
int
i = 1; i <= n; i++) f[i] = 0;
19.
for
(
int
i = 1; i <= n; i++) {
20.
for
(
int
j = i; j <= n; j += i) {
21.
f[j] = 1 - f[j];
22.
}
23.
}
24.
int
ans = 0;
25.
for
(
int
i = 1; i <= n; i++) ans += f[i];
26.
printf
(
"test case %d:\n"
, cas);
27.
printf
(
"%d\n"
, ans);
28.
}
29.
return
0;
30.
}