The Archeologists' Dilemma

The Archeologists' Dilemma

时间: 1ms        内存:64M

描述:

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



提示:

参考答案(内存最优[752]):

#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("%d\n", 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]='\0';
      }

参考答案(时间最优[0]):

#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("%d\n", 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]='\0';
      }

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

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