cf 496B Secret Combination

题目链接: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.

题意:给出一个n位数和两种操作,操作1:给这个数的每个位上的数值加1(9会变成0),操作2:n位数右移一位(最后一位也就是个位上的数移到最高位)。现在要求运用这两种操作来使这个数最小化。

解法:因题目要求最小化这个数,对于操作1而言,要么让高位开始变为0(比如998923),要么让低位开始变为0(比如23789899),然后通过右移,此时才能使当前数更小化。所以我们可以先保存这个n位数所有右移后的数,然后对每个数保证最高位为0的情况下进行对这个数最小化,然后更新答案即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long ll;
10 const int maxn=1000+10;
11 int n;
12 char str[maxn][maxn];
13 char S[maxn];
14 int main()
15 {
16     while (scanf("%d",&n)!=EOF)
17     {
18         memset(S,0,sizeof(S));
19         scanf("%s",str[0]);
20         int len=strlen(str[0]);
21         for (int i=0 ;i<len ;i++) S[i]=‘9‘;
22         char s2[maxn];
23         memset(s2,0,sizeof(s2));
24
25         strcpy(s2,str[0]);
26         int num=10-(s2[0]-‘0‘);
27         for (int j=0 ;j<len ;j++)
28         {
29             int k=(s2[j]-‘0‘+num)%10;
30             s2[j]=k+‘0‘;
31         }
32         if (strcmp(S,s2)>0) strcpy(S,s2);
33
34         for (int i=1 ;i<n ;i++)
35         {
36             char c=str[i-1][0];
37             for (int j=1 ;j<len ;j++)
38             str[i][j-1]=str[i-1][j];
39             str[i][len-1]=c;
40             strcpy(s2,str[i]);
41             int num=10-(s2[0]-‘0‘);
42             for (int j=0 ;j<len ;j++)
43             {
44                 int k=(s2[j]-‘0‘+num)%10;
45                 s2[j]=k+‘0‘;
46             }
47             if (strcmp(S,s2)>0) strcpy(S,s2);
48         }
49         printf("%s\n",S);
50     }
51     return 0;
52 }

后续:感谢提出宝贵的意见。。。

时间: 2024-10-18 21:42:11

cf 496B Secret Combination的相关文章

CodeForces 496B Secret Combination

Secret Combination Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 496B Description You got a box with a combination lock. The lock has a display showing n digits. There are two buttons

codeforces 496B. Secret Combination 解题报告

题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个位数)   2.循环右移(最右边那个数字去到第一位上).问经过若个两种操作的组合后,得到的最小数值为多少. 我一开始用了vector来做= =,没有考虑到循环右移的情况.以为每一位从1加到9之后,找出最小的那个就可以..... 留个纪念(错误代码,别学,如果没有循环右移的限制,这个是对的) 1 #incl

构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

题目传送门 1 /* 2 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 8:43:02 7 File Name :A.cpp 8 *************************************************/ 9 1

CF #301 A :Combination Lock(简单循环)

A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:

A - Combination Lock

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and

Codeforces Round #301 解题报告

感觉这次的题目顺序很不合理啊... A. Combination Lock Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock. The combination loc

Topcoder SRM 146

Div1 300 RectangularGrid 题意:给定一个长方形,问包含有多少不是正方形的小长方形 题解:枚举小长方形的长宽即可 #line 2 "RectangularGrid.cpp" #include<bits/stdc++.h> using namespace std; typedef long long LL; class RectangularGrid { public: long long countRectangles(int width, int h

Topcoder 144-150(未完待续)

SRM 144 Div2 1100 PowerOutage 题意:给定一个有根树,求遍历完整棵树的最小路程 题解:DFS一下,求出叶子节点中到根节点的最远距离,然后把树的所有边相加,乘以二,减去最远距离就是答案 #line 2 "PowerOutage.cpp" #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=50; struct Edge { int to;

CodeForces Round #283 Div.2

A. Minimum Difficulty 题意: 有一个递增序列a,现在要去掉除了第一项和最后一项元素外的某一项,使新数列中相邻元素之差的最大值最小. 分析: 先求出原序列a中,相邻两项元素之差的最大值m. 枚举去掉ai,则新序列相邻元素差的最大值为max{ m, ai+1 - ai },然后记录最小值即可. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn =