Secret Combination

Problem:Secret Combination

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 1:
3
579

Sample Output 1:
024

Sample Input 2:
4
2014

Sample Output 2:
0142

Language:C

#include <stdio.h>
#include <string.h>

char number[1010];
char answer[1010];

void button1(int length)
{
  int i;
  for(i=0;i<length;i++)
  {
    number[i]++;
    if(number[i]>'9') number[i]='0';
  }
}

void button2(int length)
{
  int i;
  char temp=number[length-1];
  for(i=length-1;i>0;i--)
  {
    number[i]=number[i-1];
  }
  number[0]=temp;
}

int main()
{
  int length;
  scanf("%d\n",&length);
  gets(number);
  strcpy(answer,number);

  int i,j;
  for(i=0;i<10;i++)
  {
    button1(length);

    if(strcmp(answer,number)>0)
    {
      strcpy(answer,number);
    }

    for(j=0;j<length;j++)
    {
      button2(length);

      if(strcmp(answer,number)>0)
      {
        strcpy(answer,number);
      }
    }
  }

  printf("%s\n",answer);

  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值