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

Re:这题给点解题思路吧.......搜索?

Posted by 317358117 at 2010-08-07 22:26:46 on Problem 1316
In Reply To:这题给点解题思路吧.......搜索? Posted by:hdm2968 at 2006-02-10 18:44:51
> 好像没多少人说说这题怎样做



我的思路:(大牛见笑)利用一个数和这个数各个数字之和除9同余,假设t是"自私数",设它可以被n分解,n的各位和是s,那么就有t%9=(n%9+s%9)%9,如果t%9为奇数,那么(t%9+9)/2就等于s%9和n%9,否则t%9等于s%9和n%9,这样就求出了n%9,然后对n%9~t的每一个n测试,如果能分解就不是"自私数"否则就是"自私数",

#include<iostream>
#include<stdio.h>
using namespace std;
int o(int t)
{
	int mod,wei,i,j,sum,num;
	if(t<10)wei=1;	else if(t<100)wei=2;	else if(t<1000)wei=3;else wei=4;
	for(i=0;i<2;i++)
	{
		sum=9*i+t%9;
		if(sum%2==0)
		{
			mod=sum/2;
			num=(t-wei*9)/9*9+mod;
			for(j=0;num<t;j++)
			{
				num=mod+j*9;
				
				int s=num,k=0;
				while(s!=0)
				{
					k+=s%10;
					s/=10;
				}
				if(t==k+num)	return 0;
			}
		}
	}

	return 1;
}
int main()
{
	int t=1;
	while(t!=9999)
	{
		if(o(t++)==1)	printf("%d\n",t-1);
	}
	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