cf 496B 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的情况下进行对这个数最小化,然后更新答案即可。
#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=+;
int n;
char str[maxn][maxn];
char S[maxn];
int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(S,,sizeof(S));
scanf("%s",str[]);
int len=strlen(str[]);
for (int i= ;i<len ;i++) S[i]='';
char s2[maxn];
memset(s2,,sizeof(s2)); strcpy(s2,str[]);
int num=-(s2[]-'');
for (int j= ;j<len ;j++)
{
int k=(s2[j]-''+num)%;
s2[j]=k+'';
}
if (strcmp(S,s2)>) strcpy(S,s2); for (int i= ;i<n ;i++)
{
char c=str[i-][];
for (int j= ;j<len ;j++)
str[i][j-]=str[i-][j];
str[i][len-]=c;
strcpy(s2,str[i]);
int num=-(s2[]-'');
for (int j= ;j<len ;j++)
{
int k=(s2[j]-''+num)%;
s2[j]=k+'';
}
if (strcmp(S,s2)>) strcpy(S,s2);
}
printf("%s\n",S);
}
return ;
}
后续:感谢提出宝贵的意见。。。
cf 496B Secret Combination的更多相关文章
- CodeForces 496B Secret Combination
Secret Combination Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- codeforces 496B. Secret Combination 解题报告
题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个 ...
- B. Secret Combination
B. Secret Combination time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #283 (Div. 2) B. Secret Combination 暴力水题
B. Secret Combination time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination
题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...
- CF #301 A :Combination Lock(简单循环)
A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:
- Codeforces Round #301 (Div. 2) A. Combination Lock 暴力
A. Combination Lock Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/p ...
- A - Combination Lock
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Scroog ...
- Codeforces 540A - Combination Lock
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he ...
随机推荐
- asp.net跨页面传值
a.aspx.cs //获取a中的id HttpCookie objCookie = new HttpCookie("myCookie", id); Response.Cookie ...
- jQuery类级别插件--返回顶部,底部,去到任何部位
先引入js:<script type="text/javascript" src="jquery.js" ></script><s ...
- 如何在linux中配置PHP环境
yum -y install httpd php mysql mysql-server php-mysql//安装mysql扩展yum -y install mysql-connector-odbc( ...
- php的swoole扩展中onclose和onconnect接口不被调用的问题
在用swoole扩展写在线聊天例子的时候遇到一个问题,查了不少资料,现在记录于此. 通过看swoole_server的接口文档,回调注册接口on中倒是有明确的注释: * swoole_server-& ...
- 一个ListView中显示不同的item(分组)
MainActivity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import ...
- 一个表格说明RelativeLayout中的几个重要属性【Written By KillerLegend】
RelativeLayout中几种布局属性的说明 无 无 无 无
- S5PV2210
http://www.doc88.com/p-773451739254.html CAN转换器 CAN总线信息转换输出装置 基于车载CAN总线的倒车雷达单元设计[图] http://www.doc88 ...
- 做个伪IE浏览器?!【来自官网】
原文:docwiki.embarcadero.com/RADStudio/Seattle/en/Building_a_VCL_Forms_Web_Browser_Application 选择File ...
- Oracle 直方图实例测试
--创建表 SQL> create table tab (a number, b number); Table created. --插入数据 SQL> begin .. loop ins ...
- python解析页面上json字段
一般来说,当我们从一个网页上拿下来数据,就是一个字符串,比如: url_data = urllib2.urlopen(url).readline() 当我们这样得到页面数据,url_data是全部页面 ...