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 ...
随机推荐
- 转载字典地址:http://blog.csdn.net/aladdinty/article/details/3591789
相关文章: http://www.360doc.com/content/13/1003/23/14070959_318861279.shtml http://www.360doc.com/conten ...
- 在SQL中取出字符串中数字部分或在SQL中取出字符部分
在SQL中取出字符串中数字部分或在SQL中取出字符部分 编写人:CC阿爸 2013-10-18 近来在开发一个项目时,一包含数字的字符串,需要取出中间的数字部分进行排序.经过baidu搜索.并结合自己 ...
- MongoDB(1):常用操作命令大全
MongoDB常用操作命令大全(转) http://www.jb51.net/article/48217.htm 成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操 ...
- 二、MongoDB的基础知识简介
1.文档.集合和数据库 a).文档:因为MongoDB是面向文档的数据库,那么可想而知文档是它的基本单元,相当于关系型数据库中的行! Ⅰ.它是由键值对组成的一个有序集:注:键不能为空且是字符串类型的. ...
- centos彻底删除mysql
yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...
- Spark官方文档——本地编写并运行scala程序
快速开始 本文将介绍如何用scala.java.python编写一个spark单击模式的程序. 首先你只需要在一台机器上成功建造Spark:做法: 进入Spark的根目录,输入命令:$ sbt/sbt ...
- BufferedInputSream和BufferedOutputSream,,,
package cd.itcast.bufferinputstream; import java.io.BufferedInputStream; import java.io.File; import ...
- delphi 单引号在字符串中使用方法
可以看delph的帮助,里面有这个问题详细说明:A character string, also called a string literal or string constant, consist ...
- python学习应用笔记(一)
之前一直用c++写程序 所以考虑程序一般都比较容易往数据结构的方向想 而自己设计数据结构往往要费很大事 昨天看了一下python 发现脚本语言 真是厉害 用来进行模拟运算确实不错 可以先 ...
- 第一篇代码 嗨翻C语言 21点扑克
/* * 计算牌面点数的程序. * 使用“拉斯难加斯公开许可证”. * 学院21点扑克游戏小组. */#include <stdio.h>#include <stdlib.h& ...