UVA1583 UVALive3355 Digit Generator

Regionals 2005 >> Asia - Seoul

问题链接:UVA1583 UVALive3355 Digit Generator。基础训练级的题,用C语言编写。

看题意,请点击上述链接。

查表法最为合理,即使试探法可能性太多,而且求最小的生成元比较困难。

不打表估计时间上也会超时。

程序中,打表功能封装到函数maketable(),使得主函数变得简洁。另外,打表时需要注意数组下标溢出问题。还需要注意,求的是最小生成元。

AC通过的C语言程序如下:

/* UVA1583 UVALive3355 Digit Generator */

#include <stdio.h>
#include <memory.h>

#define MAXN 100000

int ans[MAXN+1];

void maketable(int max)
{
    int i, digitsum, n;

    memset(ans, 0, sizeof(ans));

    for(i=1; i<max; i++) {
        digitsum = n = i;
        while(n > 0) {
            digitsum += n % 10;
            n /= 10;
        }

        if(digitsum <= max)
            if(ans[digitsum] == 0)
                ans[digitsum] = i;
    }
}

int main(void)
{
    maketable(MAXN);

    int t, n;

    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);

        printf("%d\n", ans[n]);
    }

    return 0;
}
时间: 2024-08-27 18:14:52

UVA1583 UVALive3355 Digit Generator的相关文章

最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583)

Question 例题3-5 最小生成元 (Digit Generator, ACM/ICPC Seoul 2005, UVa1583) 如果x+x的各个数字之和得到y,就是说x是y的生成元.给出n(1<=n<=100000), 求最小生成元.无解输出0.例如,n=216,121,2005时的解分别是198,0,1979. Think 方法一:假设所求生成元记为m,不难发现m<n.换句话说,只需枚举所有的m<n,看看有木有哪个数是n的生成元.此举效率不高,因为每次计算一个n的生成元

UVa 1583 Digit Generator(数)

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 

Problem 005——Digit Generator

1583 - Digit Generator For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore,

uva 1583 B - Digit Generator (暴力)

B - Digit Generator Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description For a positive integer N<tex2html_verbatim_mark> , the digit-sum of N<tex2html_verbatim_mark> is defined as the sum of N&l

Digit Generator(水)

题目链接:http://acm.tju.edu.cn/toj/showp2502.html2502.   Digit Generator Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 2426   Accepted Runs: 1579 For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits.

UVa1583 Digit Generator

#include <stdio.h> int main(){    int T, N, i, k, digitsum, generator;    scanf("%d", &T);    while (--T >= 0)    {        scanf("%d", &N);        for (i = 45; i > 0; --i)        {            generator = N - i;     

生成元(Digit Generator,ACM/ICPC Seoul 2005, UVa1583)

For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N , we call N a generator of M . For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of

Digit Generator, ACM/ICPC Seoul 2005, UVa1583

For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits. When M is the digitsum of N, we call N a generator of M. For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of 256

uva 1583 Digit Generator(Uva-1583)

题目不再写入了,vj:https://vjudge.net/problem/UVA-1583#author=0 主要讲的是找一个数的小于它的一个数,小于它的那个数每一位加起来再加上那个数就会等于原来的数.没有就是0. 这个题实际上感觉也直接暴力for就行.数最大是1e5.那么最多5个9那么就是45,直接用stringstream或者其他的方法进行分位然后寻找就行. 找到就break那么这个数就是最小的. 刘汝佳的做法是直接预处理一个数组然后根据数组找数,实在是感觉非常的高明,同时代码量也比较小.