Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

bangmangkan yi xia

Posted by y05dzz at 2006-08-15 16:54:55 on Problem 2969
#include<iomanip.h>
#include<string.h>
int main()
{
	char s[1011];
	while(cin>>s)
	{
		int dig[10] = {0}, sum = 0,i,j;
		for (i = 0;i<(int)strlen(s); i++)
		{
			dig[s[i] - '0']++;
			sum += s[i] - '0';
		}
		if (dig[0] + dig[5] == 0)
		{
			cout<<"impossible"<<endl;
			return 0;
		}
		else
		{
			if(dig[0]>0)
			{
				if(sum%3==0)
				{
					if(sum==0)
					{
						cout<<0;
					}
					else
						for(i=9;i>=0;i--)
						{
							for(j=dig[i];j>0;j--)
							{
								cout<<i;
							}
						}
						cout<<endl;
				}
				else
				{
					for(i=0;i<=9;i++)
					{
						if(dig[i]>0&&((i-(sum%3))%3==0))
						{
							dig[i]--;
							break;
						}
					}
					if(i>9)
					{
						while(sum%3!=0)
						{
							for(j=1;j<=9;j++)
							{
								if(dig[j]>0)
								{
									dig[j]--;
									sum-=j;
									break;
								}
							}
							if(j>9)
							{
								cout<<'0'<<endl;
								break;
							}
						}
						if(sum==0)
						{
							cout<<0;
						}
						else
							if(sum%3==0)
							{
								for(i=9;i>=0;i--)
								{
									for(j=dig[i];j>0;j--)
									{
										cout<<i;
									}
								}
								cout<<endl;
							}
					}
					else
					{
						if(dig[1]+dig[2]+dig[3]+dig[4]+dig[5]+dig[6]+
							dig[7]+dig[8]+dig[9]==0)
						{
							cout<<0;
						}
						else
							for(i=9;i>=0;i--)
							{
								for(j=dig[i];j>0;j--)
								{
									cout<<i;
								}
							}
							cout<<endl;
					}
				}
			}
			else
				if(dig[5]>0&&dig[0]==0)
				{
					dig[5]--;
					if(sum%3==0)
					{
						for(i=9;i>=0;i--)
						{
							for(j=dig[i];j>0;j--)
							{
								cout<<i;
							}
						}
						cout<<5<<endl;
					}
					else
					{
						for(i=0;i<=9;i++)
						{
							if(dig[i]>0&&((i-(sum%3))%3==0))
							{
								dig[i]--;
								sum-=i;
								break;
							}
						}
						if(i>9)
						{
							while(sum%3!=0)
							{
								for(j=1;j<=9;j++)
								{
									if(dig[j]>0)
									{
										dig[j]--;
										sum-=j;
										break;
									}
								}
								if(j>9)
								{
									cout<<"impossible"<<endl;
									break;
								}
							}
							if(sum%3==0)
							{
								for(i=9;i>=0;i--)
								{
									for(j=dig[i];j>0;j--)
									{
										cout<<i;
									}
								}
								cout<<endl;
							}
						}
						else
						{
							for(i=9;i>=0;i--)
							{
								for(j=dig[i];j>0;j--)
								{
									cout<<i;
								}
							}
							cout<<5<<endl;
						}
					}
				}
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator