24h購物| | PChome| 登入
2012-05-07 07:40:29| 人氣1,993| 回應0 | 上一篇 | 下一篇

[UVA] 763 - Fibinary Numbers

推薦 0 收藏 0 轉貼0 訂閱站台


  Fibinary Numbers 

The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence are:
begin{displaymath}1, 2, 3, 5, 8, 13, 21 , dots
end{displaymath}

Where each term is the sum of the two preceding terms (note that there is only one 1 in the sequence as defined here). Using this scheme, the sequence ``1010'' could be interpreted as $1 cdot 5 + 0 bullet 3 + 1 bullet 2 + 0 bullet 1 = 7$. This representation is called a Fibinary number.


Note that there is not always a unique Fibinary representation of every number. For example the number 10 could be represented as either 8 + 2 (10010) or as 5 + 3 + 2 (1110). To make the Fibinary representations unique, larger Fibonacci terms must always be used whenever possible (i.e. disallow 2 adjacent 1's). Applying this rule to the number 10, means that 10 would be represented as 8+2 (10010).

Input and Output 

Write a program that takes two valid Fibinary numbers and prints the sum in Fibinary form. These numbers will have at most 100 digits.

In case that two or more test cases had to be solved, it must be a blank line between two consecutive, both in input and output files.

Sample Input 

10010
1

10000
1000

10000
10000

Sample Output 

10100

100000

100100




// ANSI C 0.024 s Rank 10
#include <stdio.h>
#include <string.h>

int main() {
char str[110];
int first = 0;
while(scanf("%s", str) == 1) {
int ans[115] = {}, i, j, len;
len = strlen(str);
for(i = len-1, j = 0; i >= 0; i--, j++)
ans[j] += str[i]-'0';
scanf("%s", str);
len = strlen(str);
for(i = len-1, j = 0; i >= 0; i--, j++)
ans[j] += str[i]-'0';
if(first)
puts("");
first = 1;
int AC = 0;
for(i = 0; i <= 110; i++) {
if(ans[i] && ans[i+1]) {
ans[i]--;
ans[i+1]--;
ans[i+2]++;
AC = 1;
}
if(ans[i] > 1) {
ans[i] -= 2;
if(i == 0)
ans[1]++;
else if(i == 1)
ans[2]++, ans[0]++;
else
ans[i+1]++, ans[i-2]++;
AC = 1;
}
if(i == 109 && AC == 1)
AC = 0, i = -1;
}
for(i = 110; i > 0; i--)
if(ans[i])
break;
for(; i >= 0; i--)
putchar(ans[i]+'0');
puts("");
}
return 0;
}
 

台長: Morris
人氣(1,993) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 11526 - H(n)
此分類上一篇:[UVA] 10879 - Code Refactoring

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文