| ||
Accepted : 238 | Submit : 1065 | |
Time Limit : 1000 MS | Memory Limit : 65536 KB |
Finally, which light is on?题目描述有n盏灯,编号1~n。一开始灯都是关着的,每个灯有一个开关,按奇数次为打开,按偶数次为关闭。我们先把编号为1的倍数的灯按一下开关,再把编号为2的倍数的灯按一下开关,依次下去,一直到把编号为n的倍数灯按一下,请问最后有多少盏灯是亮的? 输入第一行是一个整数 T(1≤T≤10000) ,表示样例的个数。 以后的每行一个样例,为一个整数 n(1≤n≤109) 。 输出每行输出一个样例的结果。 样例输入2 1 5 样例输出1 2 样例解释第2个样例,先把1~5都打开;然后把2,4关掉;然后把3关掉;然后把4打开;然后把5关掉;所以,最后剩1和4是亮着的。 |
来源:湘潭大学 2016年新生趣味程序设计竞赛http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1258
模拟找规律,不知道怎么证明,求告知。
一(感觉复杂了)
#include<bits/stdc++.h>
using namespace std;
int a[1000010];
int main()
{
a[0]=3,a[1]=8;
int p=6,i=1,t,n;
while(a[i]<=1000100000)
{
i++;
a[i]=a[i-1]+1+p;
p+=2;
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int num=lower_bound(a,a+i+2,n)-a+1;
printf("%d\n",num);
}
}
二(这个挺好)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m=3,i;
int sum=0;
scanf("%d",&n);
for( i=0;sum<n;i++)
sum+=m,
m+=2;
printf("%d\n",i);
}
}