列印 N 行數字,使得每對數字的最大公約數為 K
GCD
GCD 表示兩個或更多非 0 整數的最大公約數
例如,求 48 和 180 的最大公約數
48 = 2 × 2 × 2 × 2 × 3
180 = 2 × 2 × 3 × 3 × 5

最大公約數 = 2 × 2 × 3 = 12。
給定問題中,應該打印出多行,元素具有指定的最大公約數
Input : N=2 GCD=2 Ouput : 2-4-6-10 14-16-18-22
演算法
START Step 1 -> take input n(e.g. 2) and k(e.g. 2) as int values and i Step 2-> Loop For i to 0 and i<n and i++ Print (k * (6 * i + 1)) Print (k * (6 * i + 2)) Print (k * (6 * i +3)) Print (k * (6 * i + 5)) Print
Step 3 -> end loop STOP
例項
#include<stdio.h>
int main() {
int i,n = 2, k = 2;
for (i = 0; i < n; i++) {
printf("%d-",(k * (6 * i + 1)));
printf("%d-",(k * (6 * i + 2)));
printf("%d-",(k * (6 * i + 3)));
printf("%d",(k * (6 * i + 5)));
printf("
");
}
return 0;
}輸出
如果執行上述程式,將生成以下輸出。
2-4-6-10 14-16-18-22
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP