CodeForces 496B Secret Combination

来源:互联网 发布:mac app store更改用户 编辑:程序博客网 时间:2024/05/01 02:46
B. Secret Combination
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
3579
output
024
input
42014
output
0142
题目大意:给定一个n位整数和两种操作:1.每位上的数字加1,9加1记为0。2.每位数字右移,最右边的数字移动到最左边。通过这两种操作得到一个最小整数,可以有前导0,输出这个数。

解题思路:第一位为0时数字最小,所以要通过操作1得到带有0的整数,再通过操作2把0前置,这样的整数会有n个。所以比较n次得到答案。

解题过程:wa点基本无。细节注意避免死循环。

代码如下:
#include <cstdio>const int maxn=1005;int a[maxn],b[maxn];int n,min,t=10;char s[maxn];void B(){    int tm=a[n-1],i,p=0;for(i=0;i<n;i++) //b[i]数组记录最小的整数{        if(a[i]<b[i])        {            p=1;break;        }if(a[i]>b[i]) break;}if(p){        for(i=0;i<n;i++)            b[i]=a[i];}for(i=n-1;i>0;i--)a[i]=a[i-1];a[0]=tm;    return ;}void A(){int p;    while(t--)//10次循环,确保每位上的数字都机会加到0{p=0;for(int i=0;i<n;i++){a[i]=(a[i]+1)%10;if(a[i]==0)p=1;}if(p)   //某一位上有0时进入操作2for(int j=0;j<n;j++)B();}    return;}int main(){    int i;    scanf("%d",&n);    scanf("%s",s);    for(i=0;i<n;i++)    {        a[i]=s[i]-'0';        b[i]=a[i];    }    A();    int p=0;    for(i=0;i<n;i++)        printf("%d",b[i]);    printf("\n");    return 0;}



0 0
原创粉丝点击