POJ2166 Death to Binary?

  1 /*
  2  POJ2166 Death to Binary?
  3  http://poj.org/problem?id=2116
  4  齐肯多夫定理
  5
  6  */
  7 #include <cstdio>
  8 #include <algorithm>
  9 #include <cstring>
 10 #include <cmath>
 11 #include <vector>
 12 #include <queue>
 13 #include <iostream>
 14 #include <map>
 15 #include <set>
 16 //#define test
 17 using namespace std;
 18 const int Nmax=1e6+7;
 19 char s[50],a[50],b[50],c[50],tmp[50],t[50];
 20 long long f[50];
 21 vector<long long> v;
 22 vector<long long>::iterator it;
 23 long long scan()
 24 {
 25     char c=getchar();
 26     while(c==‘ ‘||c==‘\n‘)
 27         c=getchar();
 28     if(c==-1)
 29         return -1;
 30     int sz=0;
 31     while(c>=‘0‘ && c<=‘1‘)
 32     {
 33         s[++sz]=c;
 34         c=getchar();
 35     }
 36     long long x=0LL;
 37     for(int i=1;i<=sz;i++)
 38     {
 39         x+=1LL*(s[i]-‘0‘)*f[sz-i];
 40     }
 41     return x;
 42 }
 43 int id(long long x)
 44 {
 45     int now=upper_bound(v.begin(),v.end(),x)-v.begin();
 46     if(now!=0)
 47         now--;
 48     return now;
 49 }
 50 char* get(long long x)
 51 {
 52    int sz=0;
 53    for(int i=0;i<=40;i++)
 54        tmp[i]=‘0‘;
 55    //printf("\n");
 56    while(x>0)
 57    {
 58        //printf("x:%lld\n",x);
 59        tmp[id(x)]=‘1‘;
 60        sz=max(sz,id(x));
 61        //printf("%d ",id(x));
 62        //printf("%lld",f[id(x)]);
 63        x-=f[id(x)];
 64    }
 65    //printf("\n");
 66    //for(int i=0;i<=40;i++)
 67        //printf("%c",tmp[i]);
 68    //printf("\n");
 69
 70    //printf("%s\n",tmp);
 71
 72    for(int i=sz;i>=0;i--)
 73        t[sz-i]=tmp[i];
 74    t[sz+1]=‘\0‘;
 75    return t;
 76 }
 77 void print()
 78 {
 79     int n=strlen(c);
 80     int sum=n+2;
 81     int an=strlen(a);
 82     int bn=strlen(b);
 83     for(int i=1;i<=sum-an;i++)
 84         putchar(‘ ‘);
 85     printf("%s\n",a);
 86     putchar(‘+‘);
 87     for(int i=1;i<=sum-bn-1;i++)
 88         putchar(‘ ‘);
 89     printf("%s\n",b);
 90     putchar(‘ ‘);
 91     putchar(‘ ‘);
 92     for(int i=1;i<=n;i++)
 93         putchar(‘-‘);
 94     putchar(‘\n‘);
 95     putchar(‘ ‘);
 96     putchar(‘ ‘);
 97     printf("%s\n\n",c);
 98 }
 99 int main()
100 {
101     #ifdef test
102     #endif
103     //freopen("poj2166.in","r",stdin);
104     f[0]=1LL;
105     f[1]=2LL;
106     for(int i=2;i<41;i++)
107         f[i]=f[i-1]+f[i-2];
108     for(int i=0;i<=40;i++)
109         v.push_back(f[i]);
110     while(1)
111     {
112         long long x=scan();
113         //printf("%lld\n",x);
114         if(x==-1)
115             break;
116         long long y=scan();
117         if(y==-1)
118             break;
119         long long ans=x+y;
120         //long long ttt=40LL;
121         //while(ttt>0)
122         //{
123             //printf("%d\n",id(ttt));
124             //printf("%lld\n",f[id(ttt)]);
125             //ttt-=f[id(ttt)];
126         //}
127         //strcpy(a,get(40LL));
128         //printf("%s\n",a);
129         strcpy(a,get(x));
130         strcpy(b,get(y));
131         strcpy(c,get(ans));
132         print();
133     }
134     return 0;
135 }
时间: 2024-10-14 16:46:26

POJ2166 Death to Binary?的相关文章

poj 2116 Death to Binary? 模拟

Death to Binary? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1707   Accepted: 529 Description The group of Absurd Calculation Maniacs has discovered a great new way how to count. Instead of using the ordinary decadic numbers, they us

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

L304 What Is Death?

How should we define the death of a person? Philosophers and physicians have long pondered this question, yet we still don’t have a satisfactory answer. For much of human history, death was synonymous with the cessation of the heartbeat. However, the

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[leetcode] 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De