石头数字的总和

Manasa 和 她的朋友出去徒步旅行。她发现一条小河里边顺序排列着带有数值的石头。她开始沿河而走,发现相邻两个石头上的数值增加 a 或者 b. 这条小河的尽头有一个宝藏,如果Manasa能够猜出来最后一颗石头上的数值,那么宝藏就是她的。假设第一个石头的上数值为0,找出最后一个石头的可能的所有数值。

输入格式

第一行包含整数 T, 代表测试数据的组数。 
每组数组包含三行: 
第一行包含 n,代表石头的个数 
第二行包含 a
第三行包含 b

输出格式 升序输出最后一颗石头上所有可能的数值, 用空格隔开。

取值范围 
1 ≤ T ≤ 10 
1 ≤ nab ≤ 103

样例输入 00

2
3 
1
2
4
10
100

样例输出 00

2 3 4 
30 120 210 300 

样例解析

第一组数据所有可能的数值为:

  1. 0,1,2
  2. 0,1,3
  3. 0,2,3
  4. 0,2,4

所以答案是: 2 3 4.

第二组数据所有可能的数值为:

  1. 10, 20, 30
  2. 10, 20, 120
  3. 10, 110, 210
  4. 100, 200, 300

所有答案是: 30 120 210 300

 

解决代码:

#include <stdio.h>

int main(int argc, const char * argv[])
{
    // insert code here...
    
    int testCount = 0;

    int depth = 4;
    int a = 10;
    int b = 100;
    
    scanf("%d", &testCount);
    for (int i=0; i<testCount; i++) {
        scanf("%d", &depth);
        scanf("%d", &a);
        scanf("%d", &b);
        
        if (depth == 0) {
            printf("%d\n", 0);
            continue;
        }
        
        int sum = 0;
        depth = depth - 1;
        
        //是较大值在前面,这样输出结果是升序的。
        if (a<b) {
            int tmp = a;
            a = b;
            b = tmp;
        }
        
        if (a == b) {
            printf("%d\n", a * depth);
            continue;
        }
        
        for (int i=0; i<=depth; i++) {
            int sumA = a * i;
            int sumB = b * (depth - i);
            sum = sumA + sumB;
            printf("%d ", sum);
            sum = 0;
        }
        printf("\n");
    }
    return 0;
}
代码

 

posted @ 2014-08-21 10:10  子柱  阅读(429)  评论(0编辑  收藏  举报