#39357: C語言解答【詳解版】


timmy94402@gmail.com (Timmy)

學校 : 不指定學校
編號 : 257051
來源 : [112.105.67.118]
最後登入時間 :
2024-02-09 02:06:47
b971. 等差數列 -- 板橋高中教學題 | From: [111.242.82.249] | 發表日期 : 2024-02-09 16:27

#include<stdio.h>
#include<stdlib.h>
 
int main() {
int firstTerm, lastTerm, tolerance;  // firstTerm:首項、lastTerm:末項、tolerance:公差
scanf("%d %d %d", &firstTerm, &lastTerm, &tolerance);
if (firstTerm < lastTerm) { // 由小到大的公差數列【思考】:公差一定為正,這樣才會越加越大
    for (firstTerm; firstTerm <= lastTerm; firstTerm += tolerance) {
           printf("%d ", firstTerm);
    }
}
else if (firstTerm == lastTerm) {
         printf("%d", lastTerm);
}
else { // 由大到小的公差數列【思考】:公差一定為負,這樣才會越加越小〔加上一個負值等於公差是負值〕
    for (firstTerm; firstTerm >= lastTerm; firstTerm += tolerance) {
          printf("%d ", firstTerm);
    }
}
printf("\n");
system("pause"); // 注意此行驗證時需註解掉
return 0;
}
 
ZeroJudge Forum