Codeforces 540A - 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 lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of disks on the combination lock.
The second line contains a string of n digits — the original state of the disks.
The third line contains a string of n digits — Scrooge McDuck's combination that opens the lock.
Print a single integer — the minimum number of moves Scrooge McDuck needs to open the lock.
5
82195
64723
13
In the sample he needs 13 moves:
- 1 disk:

- 2 disk:

- 3 disk:

- 4 disk:

- 5 disk:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
char a[],b[];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%s%s",&a,&b);
int s=;
for(int i=;i<n;i++){
int x=(int)a[i]-'';
int y=(int)b[i]-'';
if(x<y) swap(x,y);
s+=min(x-y,+y-x);
}
printf("%d\n",s);
}
return ;
}
Codeforces 540A - Combination Lock的更多相关文章
- CodeForces 540A Combination Lock (水题)
题意:给定一个串数,表示一种密码锁,再给定一串密码,问你滑动最少的次数,把第一行变成第二行. 析:很简单么,反正只有0-9这个10个数字,那么就是把每一个数从正着滑和倒着滑中找出一个最小的即可,正着滑 ...
- 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock
题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...
- 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 ...
- B.Petr and a Combination Lock
https://codeforces.com/contest/1097/problem/A Petr and a Combination Lock time limit per test 1 seco ...
- Combination Lock
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a Micr ...
- hihocoder #1058 Combination Lock
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...
- Hiho----微软笔试题《Combination Lock》
Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You ...
- CF #301 A :Combination Lock(简单循环)
A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:
- hihocoder-第六十一周 Combination Lock
题目1 : Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview roo ...
随机推荐
- C#设计模式学习笔记:(12)代理模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7814004.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲结构型设计模式的第七个模式,也是 ...
- java8 常用代码
1. 使用java8 提取出 list 中 bean 的某一属性 public static void main(String[] args) { List<Student> stuLis ...
- 多线程笔记 - provider-consumer
通过多线程实现一个简单的生产者-消费者案例(笔记). 首先定义一个要生产消费的数据类 : public class Data { private String id; private String n ...
- Android中实现一个简单的逐帧动画(附代码下载)
场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...
- .net 用反射获取当前运行的程序集中的所有Controller与Action
public string InitPermission() { // 通过反射加载程序集 // var baseType = Assembly.LoadFile(@"G:\testproj ...
- Lucene之查询索引
Query子类 TermQuery:根据域和关键词进行搜索 /** * termQuery根据域和关键词进行搜索 */ @Test public void termQuery() throws IOE ...
- 微服务SpringCloud(一)
Spring Cloud微服务(一) 什么是Spring Cloud 简单来说,Spring Cloud是一个微服务框架的规范,注意,只是规范,他不是任何具体的框架.Spring Cloud 为最常见 ...
- openc —— Canny 边缘检测
边缘检测的一般步骤 [第一步]滤波 边缘检测的算法主要是基于图像强度的一阶和二阶导数,但导数通常对噪声很敏感,因此必须采用滤波器来改善与噪声有关的边缘检测器的性能.常见的滤波方法主要有高斯滤波,即采用 ...
- 原创: idea 的maven 方式配置 开发javaWeb(idea+tomcat +maven
通过idea 编辑器来配置基于maven创建站点,在tomcat上配置 在采用idea 配置之前,首先要确保maven和 tomcat 正确运行. maven 配置链接 tomcat 配置链接 实际你 ...
- Jenkins 插件安装问题
插件安装问题 尝试修改更新站点为可用的镜像站点 打开 Jenkins > Manage Jenkins > Manage Plugins > Advanced,将 Update Si ...