若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1528: The Archeologists’ Dilemma

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,410

题目描述

An archaeologist, seeking proof of the presence of extraterrestrials in the Earth's past, has stumbled upon a partially destroyed wall containing strange chains of numbers. The left-hand part of these lines of digits is always intact, but unfortunately the right-hand one is often lost because of erosion of the stone. However, she notices that all the numbers with all its digits intact are powers of 2, so that the hypothesis that all of them are powers of 2 is obvious. To reinforce her belief, she selects a list of numbers on which it is apparent that the number of legible digits is strictly smaller than the number of lost ones, and asks you to find the smallest power of 2 (if any) whose first digits coincide with those of the list.

Thus you must write a program that, given an integer, determines the smallest exponent E (if it exists) such that the first digits of 2E coincide with the integer (remember that more than half of the digits are missing).

输入

Each line contains a positive integer N not bigger than 2,147,483,648.

输出

For every one of these integers, print a line containing the smallest positive integer E such that the first digits of 2E are precisely the digits of N, or, if there isn't one, the sentence “no power of 2".

样例输入

1
2
10

样例输出

7
8
20




参考代码

#include <stdio.h>
#include <string.h>
void mult(char c[],char t[],int m);
int main() 
{
    int i;
    int sum, t;
    int flag = 0;
    char a[20], b[20];
    char s[20];
    while( scanf("%s", b) != EOF ) 
    {
        getchar();
        memset(a, 0, sizeof(a));
        memset(s, 0, sizeof(s));
        a[0] = '1';
        flag = 0;
        sum = 1;
        while(1) 
        {
            mult(a, s, 2);
            for ( i = 0; b[i] != 0; i++ ) 
            {
                if( s[i] != b[i] ) 
                {
                    flag = 1;
                }
            }
            t = strlen(s)-strlen(b);
            if( flag == 0 && t > strlen(b) ) 
            {
                break;
            } else 
            {
                strcpy(a, s);
                flag = 0;
                sum++;
            }
        }
        printf("%dn", sum);
        memset(b, 0, sizeof(b));
    }
    return 0;
}
void mult(char c[],char t[],int m)
      {
          int i,l,k,flag,add=0;
          char s[100];
          l=strlen(c);
          for (i=0;i<l;i++)
              s[l-i-1]=c[i]-'0'; 
          for (i=0;i<l;i++)
                 {
                 k=s[i]*m+add;
                 if (k>=10) {s[i]=k%10;add=k/10;flag=1;} else 
      {s[i]=k;flag=0;add=0;}
                 }
          if (flag) {l=i+1;s[i]=add;} else l=i;
          for (i=0;i<l;i++)
              t[l-1-i]=s[i]+'0';
          t[l]='';
      }

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Skip Dreams孤独患者 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist