POJ之路5--1218THE DRUNK JAILER

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值