CF 496B Secret combination

Source: Internet
Author: User

Topic Link: B. Secret Combination

you got a box with a combination lock. The lock has a display showing  n  digits. There is and buttons on the box with each button changes digits on the display. You are quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the display one position to the right (the last digit becomes the first one). For example, if the display was currently showing Number 579, then if we push the Firs T button, the display would show 680, and if after that we push the second button, the Display would show 068.

Know that the lock would open if the display is showing the smallest possible number so can be obtained by pushing th e buttons in some order. The leading zeros is ignored while comparing numbers. Now your task was to find the desired number.

Test instructions: Give an n-digit and two operations, Operation 1: Give the number on each bit of the value plus 1 (9 will become 0), the operation 2:n the number of digits to the right one (the last one is the number of bits moved to the highest). These two operations are now required to minimize this number.

Solution: For the purpose of minimizing this number, for Operation 1, either let the high start to 0 (for example, 998923), or let the low start to 0 (such as 23789899), and then by the right shift, this time to make the current number smaller. So we can save this number of n-digits all right-shifted, and then minimize the number by guaranteeing the highest bit of 0 for each number, and then update the answer.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9typedefLong Longll;Ten Const intmaxn= ++Ten; One intN; A CharSTR[MAXN][MAXN]; - CharS[MAXN]; - intMain () the { -      while(SCANF ("%d", &n)! =EOF) -     { -memset (S),0,sizeof(S)); +scanf"%s", str[0]); -         intLen=strlen (str[0]); +          for(intI=0; i<len; i++) s[i]='9'; A         CharS2[MAXN]; atmemset (S2,0,sizeof(S2)); -  -strcpy (s2,str[0]); -         intnum=Ten-(s2[0]-'0'); -          for(intj=0; J<len; j + +) -         { in             intk= (s2[j]-'0'+num)%Ten; -s2[j]=k+'0'; to         } +         if(strcmp (S,S2) >0) strcpy (S,S2); -  the          for(intI=1; i<n; i++.) *         { $             Charc=str[i-1][0];Panax Notoginseng              for(intj=1; J<len; j + +) -str[i][j-1]=str[i-1][j]; thestr[i][len-1]=C; + strcpy (S2,str[i]); A             intnum=Ten-(s2[0]-'0'); the              for(intj=0; J<len; j + +) +             { -                 intk= (s2[j]-'0'+num)%Ten; $s2[j]=k+'0'; $             } -             if(strcmp (S,S2) >0) strcpy (S,S2); -         } theprintf"%s\n", S); -     }Wuyi     return 0; the}
View Code

Follow-up: Thank you for your valuable comments ...

CF 496B Secret combination

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.