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.

Examples
Input
3
579
Output
024
Input
4
2014
Output
0142
C语言版本一

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char num[1010];
char ans[1010];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void buttom1(int n){
	int i;
	for(i=0;i<n;i++){
		num[i]++;
		if(num[i]>'9')num[i]='0';
	}
}
void buttom2(int n){
	int i;
	char temp;
	temp=num[n-1];
	for(i=n-1;i>0;i--){
		num[i]=num[i-1];
		
	}
	num[0]=temp;
}

int main(int argc, char *argv[]) {
	int n;
	scanf("%d",&n);
	
	scanf("%s",&num);
	strcpy(ans, num);
		int i,j;
	for(i=0;i<10;i++){
			buttom1(n);
		//	printf("%s\n",num);
		//if (strcmp(ans, num) > 0)		strcpy(ans, num);
			for(j=0;j<n;j++){
				
					buttom2(n);
		//	printf("%s\n",num);
				if (strcmp(ans, num) > 0)		strcpy(ans, num);
				
			}
	}
	printf("%s\n",ans);

}

C++版本一

/*题目大意:给一整数,有两种方式、1、每位加1,其中9变为0,但不进位。2、整体向后移动一位、最后一位移动到首位
 *			问经过若干操作1与2后得到的最小数是多少
 *算法分析:可将环问题转换为链进行解决。然后每位累加,加10次依次进行比较即可,暴力完成 
*/
 
#include <bits/stdc++.h>
using namespace std;
 
char a[2200];
char b[2200], ans[2200];
 
int main() {
	int n;
	scanf("%d ",&n);
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(ans, 0, sizeof(ans));
	for (int i = 0; i<n; i++)	scanf("%c",&a[i]);
	strcpy(ans, a);
	//cout << ans << endl;
	for (int i = 0; i<n; i++)	a[n+i] = a[i];
	
	int t = 0;
	while (t <= 9) {
		for (int i = 0; i<2*n; i++)	{
			int tmp = a[i] - 48;
			tmp = (tmp+1) % 10;
			a[i] ='0' + tmp;
		}
		//cout << a << endl;
		t ++ ;
		for (int i = 0; i<n; i++) {
			int flag = 0;
			for (int j = i; j<i+n; j++) 	b[flag++] = a[j];
			if (strcmp(ans, b) > 0)		strcpy(ans, b);
			//cout << b << endl;
		}
		//cout << ans << endl;
	}
	cout << ans << endl;
	return 0;
}

C++版本二

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long ll;
const int maxn=1000+10;
int n;
char str[maxn][maxn];
char S[maxn];
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        memset(S,0,sizeof(S));
        scanf("%s",str[0]);
        int len=strlen(str[0]);
        for (int i=0 ;i<len ;i++) S[i]='9';
        char s2[maxn];
        memset(s2,0,sizeof(s2));

        strcpy(s2,str[0]);
        int num=10-(s2[0]-'0');
        for (int j=0 ;j<len ;j++)
        {
            int k=(s2[j]-'0'+num)%10;
            s2[j]=k+'0';
        }
        if (strcmp(S,s2)>0) strcpy(S,s2);

        for (int i=1 ;i<n ;i++)
        {
            char c=str[i-1][0];
            for (int j=1 ;j<len ;j++)
            str[i][j-1]=str[i-1][j];
            str[i][len-1]=c;
            strcpy(s2,str[i]);
            int num=10-(s2[0]-'0');
            for (int j=0 ;j<len ;j++)
            {
                int k=(s2[j]-'0'+num)%10;
                s2[j]=k+'0';
            }
            if (strcmp(S,s2)>0) strcpy(S,s2);
        }
        printf("%s\n",S);
    }
    return 0;
}