designing efficient algorithms [examples]~c-calculator conundrum

Source: Internet
Author: User

Alice got a hold of the calculator that can display n digits. She was bored enough to come up with the following time waster.

She enters a number k then repeatedly squares it until the result overflows. When the result is overflows, only the n most significant digits is displayed on the screens and an error flag appears. Alice can clear the error and continue squaring the displayed number. She got bored by this soon enough, but wondered:

"Given N and K, what is the largest number I can get to wasting time in this manner?"

Input

The first line of the input contains an integer t (1≤t≤200), the number of test cases. Each test case contains-integers n (1≤n≤9) and K (0≤k < 10n) where n is the number of digits this calculator Can display k is the starting number.

Output

For each test case, print the maximum number, which Alice can get by repeatedly squaring the starting number as described.

Sample Input

2

1 6

2 99

Sample Output

9

99

problem-solving ideas: The title means: There is an old computer, can only display n digits. One day, you are bored, so enter an integer k, and then repeat the square until the overflow. Each time the overflow occurs, the computer displays the highest n bits of the result and an error mark. Then clear the mark and continue the square. If you keep doing this, what is the maximum number you can get? For example, when n=1,k=6, the computer will show 6, 3 (the highest bit of 36), 9, 8 (the highest bit of 81), 6 (the highest bit of 64), 3, ...

Program code:

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<set>UsingNamespace Std;IntNext(int n,int k){LongLong Res=(LongLong) K*k;While(Res>= N) Res/=10;return res;}IntMain(){int T;scanf("%d",&t);For(int I=0; I< (T); I++){int nK;scanf("%d%d",&n,&k); N=(Int)Pow(10.0N);Computes the N power assignment of 10 to Nint ans=1<<31;Set<int>Ss;While(1){ans=Max(ansK);If(SS.Countbreakelse ss. Insert= next (n );  printf ( "% D\n ") ;  return 0;< Span class= "Sh-cbracket" >             

designing efficient algorithms [examples]~c-calculator conundrum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.