CF1295A Display The Number

我的blog

题目链接:CF1295A Display The Number

[description ]

输入(n)

输出最多使用(n)个小木棍能摆放的数字最大是多少

小木棍摆放每个数字的方式如下图:

[solution ]

贪心

越多位的数肯定越大

我们看到(1)所需的小木棍最少,所以要尽量多的选择(1),注意当(n)为奇数时,第一位选(7)明显比选(1)更优

[code ]

#include<cstdio>
using namespace std;
template<typename T>
inline void read(T&x)
{
	x=0;
	char s=(char)getchar();
	bool flag=false;
	while(!(s>='0'&&s<='9'))
	{
		if(s=='-')
			flag=true;
		s=(char)getchar();
	}
	while(s>='0'&&s<='9')
	{
		x=(x<<1)+(x<<3)+s-'0';
		s=(char)getchar();
	}
	if(flag)
		x=(~x)+1;
	return;
}
int num,n,T;
int main()
{
	read(T);
	while(T--)
	{
		read(n);
		num=n>>1;
		if(n&1)
		{
			putchar('7');
			--num;
		}
		while(num--)
			putchar('1');
		putchar('
');
	}
	return 0;
}
原文地址:https://www.cnblogs.com/wangjunrui/p/12242542.html