码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 496B Secret Combination

时间:2015-08-30 21:08:54      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

Secret Combination
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Input

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.

Output

Print a single line containing n digits — the desired state of the display containing the smallest possible number.

Sample Input

Input
3
579
Output
024
Input
4
2014
Output
0142
技术分享
 1 #include <stdio.h>
 2 #include <string.h>
 3 int main()
 4 {
 5     int n;
 6     int i,j,k;
 7     char a[1005];
 8     int b[1005];
 9     while(scanf("%d",&n)!=EOF)
10     {
11         b[0]=0;
12         for(i=1;i<1000;i++)
13             b[i]=9;
14         scanf("%s",a);
15         for(i=0;i<n;i++)
16         {
17             int x=9-a[i]+1;
18             for(j=1;j<n;j++)
19             {
20                 int y=(a[(i+j)%n]-0+x)%10;
21                 if(y<b[j])
22                 {
23                     for(k=1;k<n;k++)
24                     {
25                         b[k]=(a[(i+k)%n]-0+x)%10;
26                     }
27                     break;
28                 }
29                 else if(y>b[j])
30                 {
31                     break;
32                 }
33             }
34         }
35         for(i=0;i<n;i++)
36             printf("%d",b[i]);
37         printf("\n");
38     }
39     return 0;
40 }
View Code

 

CodeForces 496B Secret Combination

标签:

原文地址:http://www.cnblogs.com/cyd308/p/4771544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!