codechef Birthday Candles 题解

本文探讨如何利用有限数量的数字蜡烛排列出不同年龄的数字,通过实例分析找到无法表达的最小正整数。

Birthday Candles

The chef is preparing a birthday cake for one of his guests,
and his decided to write the age of the guest in candles on the cake.
There are 10 types of candles, one for each of the digits '0' through '9'.
The chef has forgotten the age of the guest, however, so doesn't know whether he has enough candles of the right types.
For example, if the guest were 101 years old, the chef would need two '1' candles and one '0' candle.
Given the candles the chef has, your task is to determine the smallest positive integer that cannot be represented with those candles.

Input:

Input will begin with an integer T≤100, the number of test cases.
Each test case consists of a single line with exactly 10 integers, each between 0 and 8, inclusive.
The first integer of each test case represents the number of '0' candles the chef has,
the second integer represents the number of '1' candles the chef has, and so on.

Output:

For each test case, output on a single line the smallest positive integer that cannot be expressed with the given candles.

Sample input:

3
2 1 1 4 0 6 3 2 2 2
0 1 1 1 1 1 1 1 1 1
2 2 1 2 1 1 3 1 1 1
 

Sample output:

4
10
22

最小不能凑出来的数字组合。

仔细想想我们是如何使用0到9的一个数字组合出千千万万个数值的。

注意特殊情况: 零缺小的时候

#pragma once
#include <stdio.h>

int BirthdayCandles()
{
	static const int CANDLES = 10;
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int A[CANDLES] = {0}, minCan = (1<<30), minId = 0;
		for (int i = 0; i < CANDLES; i++)
		{
			scanf("%d", &A[i]);
			if (minCan > A[i])
			{
				minCan = A[i];
				minId = i;
			}
		}
		if (0 == minId)
		{
			for (int i = CANDLES - 1; i > 0 ; i--)//注意有数等于0数的时候
			{
				if (minCan >= A[i])
				{
					minCan = A[i];
					minId = i;
				}
			}
			if (0 == minId) putchar('1');
		}
		while (minCan >= 0)
		{
			putchar(minId + '0');
			minCan--;
		}
		putchar('\n');
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值