UVALive3045 POJ2000 ZOJ2345 Gold Coins

Regionals 2004 >> North America - Rocky Mountain

问题链接:UVALive3045 POJ2000 ZOJ2345 Gold Coins。基础练习题,用C语言编写。

题意简述:骑士第1天获得1个金币,之后的2天获得2个金币,之后的3天获得3个金币,......,之后的i天获得i个金币,......。问到第n天总共获得多少个金币。

问题分析:这是一个数列求和问题,用程序解决比用数学解决方便很多。

程序中,用数组ans[]存储金币之和,即ans[i]为到第i天为止金币之和,设置ans[i]的初始值为0(程序员要尽量避免使用缺省值,以免得到意外的计算结果,除非缺省值绝对有保障)。

AC的C语言程序如下:

/* UVALive3045 POJ2000 ZOJ2345 Gold Coins */

#include <stdio.h>

#define MAXN 10000

int ans[MAXN+1] = {0};

void maketable()
{
    int i, j, k;

    j = 1;  /* Ai, S=1,2,2,3,3,3,4,4,4,4,...... */
    k = 1;  /* 同值计数变量:j值够j个(用k来计数, k=j时)则j增1 */
    for (i=1; i<=MAXN; i++){
        ans[i] = j + ans[i - 1];
        if (k == j){
            j++;
            k = 0;
        }
        k++;
    }
}

int main(void)
{
    int n;

    maketable();

    while(scanf("%d", &n) != EOF && n != 0)
        printf("%d %d\n", n, ans[n]);

    return 0;
}
时间: 2024-11-14 23:17:39

UVALive3045 POJ2000 ZOJ2345 Gold Coins的相关文章

HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)

Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the

hust 1170 - Baskets of Gold Coins

题目描述 You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes

H - Gold Coins(2.4.1)

H - Gold Coins(2.4.1) Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of

codechef - Bytelandian gold coins 题解

In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to ma

Baskets of Gold Coins

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1862    Accepted Submission(s): 1108 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

[ACM] POJ 2000 Gold Coins

Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 13098 Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of the next two days

HDU2401 Baskets of Gold Coins【水题】【推理】

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1648    Accepted Submission(s): 966 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

hdoj 2401 Baskets of Gold Coins

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 1104 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

Gold Coins(闲来无事水一发)

Gold Coins Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21155 Accepted: 13265 Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of the next two days (th