SOJ. Locker puzzle

题目描述                                                                                                                         时间限制:1秒    内存限制:256兆

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. }
 
 
 
 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值