Input
The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines contains a single integer between 5 and 100, inclusive, which is the number of cells n.
Output
For each line, you must print out the number of prisoners that escape when the prison has n cells.
直接求解,代码如下:
#include<iostream>
using namespace std;
int main()
{
int a,i,j,n,m;
bool b[100];
for(cin>>a;a>0;a--)
{
for(cin>>n,i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1)b[j-1]=1;
else if((j%i)==0)
b[j-1]=!b[j-1];
}
}
m=0;
for(i=0;i<n;i++)
if(b[i]==1)m++;
cout<<m<<endl;
}
return 0;
}
代码不够短,参考短码之美,可以考虑发现输入输出规律。修改如下:#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,m;
float n;
for(cin>>a;a>0;a--)
{
cin>>n;
m=(int)sqrt(n);
cout<<m<<endl;
}
return 0;
}