51nod 1433:0和5
小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5。小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数。使得这个数尽可能大,而且可以被90整除。
注意:
1.这个数没有前导0,
2.小K不需要使用所有的牌。
Input
每个测试数据输入共2行。 第一行给出一个n,表示n张牌。(1<=n<=1000) 第二行给出n个整数a[0],a[1],a[2],…,a[n-1] (a[i]是0或5 ) 表示牌上的数字。
Output
共一行,表示由所给牌组成的可以被90整除的最大的数,如果没有答案则输出”-1”(没有引号)
Input示例
4 5 0 5 0
Output示例
0
完全没有思路的一道题。。。然后用点头盾看了测试数据,发现9的倍数个5就能被9整除,我自己也是不明白为什么。。。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;
int n,cnt5,cnt0,temp;
int main()
{
	//freopen("i.txt","r",stdin);
	//freopen("o.txt","w",stdout);
	int i;
	cin>>n;
	cnt5=0;
	cnt0=0;
	for(i=1;i<=n;i++)
	{
		cin>>temp;
		if(temp==5)
		{
			cnt5++;
		}
		else
		{
			cnt0++;
		}
	}
	if(cnt0>0)
	{
		cnt5=cnt5/9;
		for(i=1;i<=cnt5;i++)
		{
			cout<<555555555;
		}
		if(cnt5==0)
		{
			cout<<0<<endl;
		    return 0;
		}
		for(i=1;i<=cnt0;i++)
		{
			cout<<0;
		}
		cout<<endl;
	}
	else
	{
		cout<<-1<<endl;
	}
	return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
 
                    
                
 收藏
 收藏 取消关注
 取消关注 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号