Linux_Arithmetic_Sequence

yoohm發表於2017-10-16

ArithmeticSequence

求一個由20項組成的等差數列,其偶數項之和為330,奇數項之和為300,而且沒想都是正整數

暫時有點問題,過段時間解決

#include <stdio.h>

int main() {
    int a, d, i, j, k;
    int sum = 0;
    for (a = 1; a <= 21; a++ ) {
        for (d = 1; d <= 4; d++ ) {
            sum = a;           // the first num of the array when a equal zero.

            for (i = 2; i <= 20; i+=2) {
                sum += a + i*d;
            } 

            if (sum == 330) {
                sum = 0;
                for (i = 1; i <= 19; i+=2) {
                    sum += a + i*d;
                }

                if (sum == 300) {
                    for (i = 0; i <= 20; i++) {
                        if (i % 10 == 0) {
                            printf("\n");
                        }
                        printf("%d\t", a + i*d);
                    }
                }
            }

        }
    }
}