cf 919B. Perfect Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liujie2232692543/article/details/79228768


B. Perfect Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

We consider a positive integer perfect, if and only if the sum of its digits is exactly 10. Given a positive integer k, your task is to find the k-th smallest perfect positive integer.

Input

A single line with a positive integer k (1 ≤ k ≤ 10 000).

Output

A single number, denoting the k-th smallest perfect integer.

Examples
input
1
output
19
input
2
output
28
Note

The first perfect integer is 19 and the second one is 28.


 这道B题的题意是很简单的,在这里就不多说了,因为种种原因(手机丢了 大哭),昨晚没打codeforces,今早起来补题,发现叶大神AC了四道……世界总排名800+


我深深地找到了差距,从这道B题,我呕心沥血码了近200行,大神30行就搞定了……

先看看我呕心沥血的代码……:

#include <iostream>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
	int o;
	int a[20001],y=0;
	scanf("%d",&o);
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			if(i + j == 10)
			{
				int p;
				p = j + i * 10;
				a[y++] = p;
			}
		}
	}
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				if(i + j + k == 10)
				{
					int p;
					p = k + j*10 + i*100;
					a[y++] = p;
				}
			}
		}
	}
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				for(int h=0;h<=9;h++)
				{
					if(i + j + k + h == 10)
					{
						int p;
						p = h + k*10 + j*100 + i*1000;
					//	printf("%d ",p);
						a[y++] = p;
					}
				}
			}
		}
	}
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				for(int h=0;h<=9;h++)
				{
					for(int g=0;g<=9;g++)
					{
						if(i + j + k + h + g == 10)
						{
							int p;
							p = g + h*10 + k*100 + j*1000 + i*10000;
						//	printf("%d ",p);
							a[y++] = p;
						}
					}
				
				}
			}
		}
	}
	
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				for(int h=0;h<=9;h++)
				{
					for(int g=0;g<=9;g++)
					{
						for(int z=0;z<=9;z++)
						{
							if(i + j + k + h + g + z == 10)
							{
								int p;
								p = z + g*10 + h*100 + k*1000 + j*10000 + i*100000;
							//	printf("%d ",p);
								a[y++] = p;
							}
						}
					
					}
				
				}
			}
		}
	}
	
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				for(int h=0;h<=9;h++)
				{
					for(int g=0;g<=9;g++)
					{
						for(int z=0;z<=9;z++)
						{
							for(int s=0;s<=9;s++)
							{
								if(i + j + k + h + g + z + s == 10)
								{
									int p;
									p = s + z*10 + g*100 + h*1000 + k*10000 + j*100000 + i*1000000;
								//	printf("%d ",p);
									a[y++] = p;
								}
							}
						
						}
					
					}
				
				}
			}
		}
	}
	
	for(int i=1;i<=9;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				for(int h=0;h<=9;h++)
				{
					for(int g=0;g<=9;g++)
					{
						for(int z=0;z<=9;z++)
						{
							for(int s=0;s<=9;s++)
							{
								for(int d=0;d<=9;d++)
								{
									if(i + j + k + h + g + z + s + d == 10)
									{
										int p;
										p = d + s*10 + z*100 + g*1000 + h*10000 + k*100000 + j*1000000 + i*10000000;
									//	printf("%d ",p);
										a[y++] = p;
									}
								}
							
							}
						
						}
					
					}
				
				}
			}
		}
	}
	printf("%d\n",a[o-1]);
	return 0;	
}


接下来是大神的代码:

 #include<iostream>


#define MAX 10005
using namespace std;

bool Judge(int num)
{
	int sum = 0;
	while(num > 0){
		sum += num%10;
		num /= 10;
	}
	return sum == 10;
}
int main( )
{
	int count = 0;
	int num[MAX];
	int i = 0;
	while(count < MAX)
		if(Judge(++i))
			num[++count] = i;
//	cout << i << ":" << count << endl; 
	int n;
	cin >> n;
	cout << num[n] << endl;
}

这真的是很简单的暴力题,很伤心啊啊啊啊 啊,,,

猜你喜欢

转载自blog.csdn.net/liujie2232692543/article/details/79228768