zoukankan      html  css  js  c++  java
  • HDU 4814

    题目的大意就是用(1+√5)/2进制来表示十进制中的数。

    做法就是一个模拟,a[]数组表示答案,其中第50位表示个位,后面的是小数位。
    利用题目给的两个公式,进行一系列进位等操作。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define maxn 110
    
    int a[maxn];
    
    int main()
    {
        //freopen("out(2).txt", "w", stdout);
        int n;
        while(scanf("%d", &n) != EOF)
        {
            if(n == 1)
            {
                printf("1
    ");
                continue;
            }
            memset(a, 0, sizeof(a));
            a[50] = n;
            int f = 1;
            while(f)
            {
                f = 0;
                for(int i = 100; i >= 0; i--)
                {
                    if(a[i] > 1)
                    {
                        f = 1;
                        int t = a[i] / 2;
                        a[i] -= t * 2;
                        a[i-1] += t;
                        a[i+2] += t;
                    }
                }
                for(int i = 100; i >= 0; i--)
                {
                    if(a[i+1] && a[i+2])
                    {
                        f = 1;
                        int t = min(a[i+1], a[i+2]);
                        a[i+1] -= t;
                        a[i+2] -= t;
                        a[i] += t;
                    }
                }
            }
            f = 0;
            int f1 = 0;
            for(int i = 0; i <= 50; i++)
            {
                if(a[i] == 0)
                {
                    if(f)
                    printf("0");
                }
                else
                {
                    printf("1");
                    f = 1;
                }
            }
            if(f == 0)
            printf("0");
            f = 0;
            for(int i = 51; i < 100; i++)
            {
                if(a[i] == 0)
                f++;
                else
                {
                    if(!f1)
                    {
                        f1 =1;
                         printf(".");
                    }
    
                    for(int j = 0; j < f; j++)
                    printf("0");
                    printf("1");
                    f = 0;
                }
            }
            cout << endl;
        }
    }
    

      但是奇怪的是,当我调换了循环的顺序,从前向后循环就会出现一个错误的结果,很是不解。

  • 相关阅读:
    POJ3233]Matrix Power Series && [HDU1588]Gauss Fibonacci
    [codeforces 508E]Maximum Matching
    [SDOI2011]染色
    [CSU1806]Toll
    [HDU4969]Just a Joke
    [HDU1071]The area
    [HDU1724]Ellipse
    [VIJOS1889]天真的因数分解
    [BZOJ3379] Turning in Homework
    [BZOJ1572] WorkScheduling
  • 原文地址:https://www.cnblogs.com/ye8370/p/3990899.html
Copyright © 2011-2022 走看看