N
代表測資數DEC_BASE = FIB_BASE (fib)
[40] = 102334155
超過範圍)
int Fibonacci[40] = {0, 1};
int i;
for(i = 2; i < 40; i++){
Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2];
}
N
再用 while
迴圈重複讀入測資並計算,因為我的 code 之後會動到輸入的測資,所以先把輸出格式印出來
int N;
scanf("%d", &N);
while(N--){
int num;
scanf("%d", &num);
printf("%d = ", num);
...
}
flag
布林變數的小技巧來控制遇到有 1 才開始輸出
bool flag = false;
for(i = 39; i >= 2; i--){
if(num >= Fibonacci[i]){
num = num - Fibonacci[i];
flag = true;
printf("1");
}
else if(flag){
printf("0");
}
}
printf(" (fib)\n");
#include<stdio.h>
#include<stdbool.h>
int main(){
int N;
int Fibonacci[40] = {0, 1};
int i;
for(i = 2; i < 40; i++){
Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2];
}
scanf("%d", &N);
while(N--){
int num;
bool flag = false;
scanf("%d", &num);
printf("%d = ", num);
for(i = 39; i >= 2; i--){
if(num >= Fibonacci[i]){
num = num - Fibonacci[i];
flag = true;
printf("1");
}
else if(flag){
printf("0");
}
}
printf(" (fib)\n");
}
return 0;
}