UVA948 Fibonaccimal Base【进制】

The well known Fibonacci sequence is obtained by starting with 0 and 1 and then adding the two last numbers to get the next one. For example the third number in the sequence is 1 (1=1+0), the forth is 2 (2=1+1), the fifth is 3 (3=2+1) and so on.

Figure 1 - The first numbers in the Fibonacci sequence
????The sequence appears on many things in our life, in nature, and has a great significance. Among other things, do you know that all positive integer numbers can be represented as a sum of numbers in the Fibonacci sequence? More than that, all positive integers can be represented as a sum of a set of Fibonacci numbers, that is, numbers from the sequence, without repetition. For example: 13 can be the sum of the sets {13}, {5,8} or {2,3,8} and 17 is represented by {1,3,13} or {1,3,5,8}. Since all numbers have this property (do you want to try to prove this for yourself?) this set could be a nice way to use as a "base" to represent the number. But, as we have seen, some numbers have more than one set whose sum is the number. How can we solve that? Simple! If we add the constraint that the sets cannot have two consecutive Fibonacci numbers, than we have a unique representation for each number! This restriction is because the sum of any two consecutive Fibonacci numbers is just the following Fibonacci number.
????Now that we know all this we can prepare a nice way to represent any positive integer. We will use a binary sequence (just zeros and ones) to do that. For example, 17 = 1 + 3 + 13 (remember that no two consecutive Fibonacci numbers can be used). Let’s write a zero for each Fibonacci number that is not used and one for each one that is used, starting at the right. Then, 17 = 100101. See figure 2 for a detailed explanation. In this representation we should not have zeros at the left, this is, we should only write starting with the first one. In order for you to understand better, note that in this scheme, not using two consecutive Fibonacci numbers means that the binary sequence will not have two consecutive ones. When we use this representation for a number we say that we are using the Fibonaccimal base, and we write it like 17 = 100101 (fib).

Figure 2 - Explaining the representation of 17 in Fibonaccimal base
????Given a set of numbers in decimal base, your task is to write them in the Fibonaccimal base.
Input
The first line of input contains a single number N, representing the quantity of numbers that follow (1 ≤ N ≤ 500).
????Than follow exactly N lines, each one containing a single positive integer smaller than 100 000 000. These numbers can come in any order.
Output
You should output a single line for each of the N integers in the input, with the format ‘DEC BASE = FIB _BASE (fib)’. DEC BASE is the original number in decimal base and FIB_BASE is its representation in Fibonaccimal base. See the sample output for an example.
Sample Input
10
1
2
3
4
5
6
7
8
9
10
Sample Output
1 = 1 (fib)
2 = 10 (fib)
3 = 100 (fib)
4 = 101 (fib)
5 = 1000 (fib)
6 = 1001 (fib)
7 = 1010 (fib)
8 = 10000 (fib)
9 = 10001 (fib)
10 = 10010 (fib)

问题链接UVA948 Fibonaccimal Base
问题简述:(略)
问题分析
????菲波那契进制问题,按照进制原理进行处理即可。
????需要从高位开始进行转换,这不同与比例进制(例如10进制)。最高用到菲波那契的哪一项需要事先进行计算,给定的数小于100000000,到37(数组下标)项是够的。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA948 Fibonaccimal Base */

#include <bits/stdc++.h>

using namespace std;

const int N = 37;
int fib[N + 1];

void setfib()
{
    fib[0] = 1;
    fib[1] = 2;
    for(int i = 2; i <= N; i++)
        fib[i] = fib[i - 2] + fib[i - 1];
}

int main()
{
    setfib();

    int n, m, k;
    scanf("%d", &n);
    while(n--) {
        scanf("%d", &m);

        for(k = N; k >= 0; k--)
            if(m >= fib[k])
                break;

        printf("%d = ", m);
        putchar('1');
        m -= fib[k];
        for(k--; k >= 0; k--)
            if(m >= fib[k]) {
                putchar('1');
                m -= fib[k];
            } else
                putchar('0');
        puts(" (fib)");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10372332.html

时间: 2024-10-15 00:51:40

UVA948 Fibonaccimal Base【进制】的相关文章

自己实现各种进制相互转换

本文自己实现了2.8.10.16进制数的相互转换.实际中很少用到或者直接用api,所以大神老鸟请绕行. 有兴趣的朋友也可以自己先写写,当做练习,仅此而已. ok, 直接进入主题.先说一下各进制转换的算法(百度一下也ok的). 算法: 一.10 进制数是平时所用到的,先从它开始.10进制转换为其它进制的数,用到的是[辗转相除取余法].简单的说,就是该数一直除以进制数(例如2进制就除以2),然后取余数,一直到结果为0.依次由下往上取余数就是结果. 例如:5(10进制),转换为2进制,进行上述过程,得

NUMBER BASE CONVERSION(进制转换)

Description Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: { 0-9,A-Z,a-z } HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when

POJ 1220 NUMBER BASE CONVERSION 高精度进制转换

      poj  50题拍照合影留念 NUMBER BASE CONVERSION Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4620   Accepted: 2115 Description Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: { 0-

[Swift]LeetCode504. 七进制数 | Base 7

Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202" Example 2: Input: -7 Output: "-10" Note: The input will be in range of [-1e7, 1e7]. 给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202&q

504 Base 7 七进制数

给定一个整数,将其转化为7进制,并以字符串形式输出.示例 1:输入: 100输出: "202" 示例 2:输入: -7输出: "-10"注意: 输入范围是 [-1e7, 1e7] .详见:https://leetcode.com/problems/base-7/description/ C++: class Solution { public: string convertToBase7(int num) { if(num==0) { return "0&

[POJ1220]NUMBER BASE CONVERSION (高精,进制转换)

题意 任意进制之间的高进的转换 思路 相模倒排,高精处理 代码 我太弱了,下面附一个讨论里发的maigo思路的代码 int i,l,k,a,b,T,t[555],A[555]; char s[555],d[555]; main(){ for(scanf("%d",&T);T--;){ scanf("%d%d%s",&a,&b,s); for(k=i=strlen(s);0<i--;)t[k-1-i]=s[i]-(s[i]<58?4

leetcode 504. 七进制数(Base 7)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个整数,将其转化为7进制,并以字符串形式输出. 示例 1: 输入: 100 输出: "202" 示例 2: 输入: -7 输出: "-10" 注意: 输入范围是 [-1e7, 1e7] . 解法: class Solution { public: string convertToBase7(int num) { string res = ""; bool neg = false;

$Poj1220/AcWing124\ Number\ Base\ Convertion$ 进制转换+高精除

$Poj$   $AcWing$ $Description$ $Sol$ 进制转化+高精度除法 $over$ $Code$ #include<bits/stdc++.h> #define il inline #define Rg register #define go(i,a,b) for(Rg int i=a;i<=b;++i) #define yes(i,a,b) for(Rg int i=a;i>=b;--i) #define mem(a,b) memset(a,b,size

进制问题的几个探究以及拓展

by MedalPluS 什么是进制这里就不赘述了= =,本文着重讨论如何转换进制 n进制转10进制 比如说(10001)2如何转换为10进制? 有一种方法叫做按权展开求和 10001可以展开为20*1+21*0+22*0+23*0+24*1=17,这样就转换为了(17)10 转换为代码如下: 1 int change_n_to_10(int c[],int k){//c为n进制数组 2 int base=k,result=0,index; 3 for(index=len_c-1;i>=0;i-