zoukankan      html  css  js  c++  java
  • 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.

    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
    3
    579
    
    output
    024
    
    input
    4
    2014
    
    output
    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]='';
    			if(i==1 || strcmp(str1,str)>0){
    				strcpy(str1,str);
    			}
    			
    		}
    		printf("%s
    ",str1);
    	}
    	return 0;
    }
    
  • 相关阅读:
    SQL server 统计数据库表数量和列出所有表名称
    mybatis 模糊查询 like的三种方式
    jquery 实现按回车键登录功能的写法
    js 各种事件 如:点击事件、失去焦点、键盘事件等
    ssm框架中从controller传值给jsp的方式
    [GDOI2019]小说
    洛谷5113
    2020.9.26模拟总结
    [IOI2015]分组
    9.19 总结
  • 原文地址:https://www.cnblogs.com/herumw/p/9464843.html
Copyright © 2011-2022 走看看