码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4814

时间:2014-09-24 17:57:47      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   for   div   sp   on   c   log   

题目的大意就是用(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\n");
            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;
    }
}

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

HDU 4814

标签:blog   io   os   for   div   sp   on   c   log   

原文地址:http://www.cnblogs.com/ye8370/p/3990899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!