B. Secret Combination
You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have 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 is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.
You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of digits on the display.
The second line contains n digits — the initial state of the display.
Print a single line containing n digits — the desired state of the display containing the smallest possible number.
3 579
024
4 2014
0142
这道题可以用暴力过,每次把最前的一个字符移到最后,然后把第一个字符变成'0',后面依次变化,求出最小的字符串。
#include<stdio.h> #include<string.h> char s[1005],str[1005],str1[1005]; char word[40]={'0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0'}; int main() { int n,m,i,j,len,t; char c; while(scanf("%d",&n)!=EOF) { memset(s,0,sizeof(s)); scanf("%s",s); len=strlen(s); for(i=1;i<=len;i++){ if(i!=1){ c=s[0]; for(j=1;j<=len-1;j++){ s[j-1]=s[j]; } s[len-1]=c; } strcpy(str,s); t='9'-s[0]+1; str[0]='0'; for(j=1;j<len;j++){ str[j]=word[str[j]-'0'+t]; } str[len]='\0'; if(i==1 || strcmp(str1,str)>0){ strcpy(str1,str); } } printf("%s\n",str1); } return 0; }