E - 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 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?

Input

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.

Output

Print a single integer — the minimum number of moves Scrooge McDuck needs to open the lock.

Sample Input

Input
5
82195
64723
Output
13

Hint

In the sample he needs 13 moves:

  • 1 disk: 
  • 2 disk: 
  • 3 disk: 
  • 4 disk: 
  • 5 disk: 


#include <iostream>
#include <cmath>
using namespace std; int main()
{
int n;
char a[1100];
char b[1100];
int flag= 0 ;
cin >> n;
for (int i=0; i<n; i++)
cin >> a[i];
for (int j=0; j<n; j++)
cin >> b[j];
int num = 0;
for (int i=0; i<n; i++)
{
int p1 = (int)a[i]-48;
int p2 = (int)b[i]-48;
int p = p1-p2;
flag = abs(p);
if (flag>5)
flag = 10-flag;
num = num +flag;
}
cout << num << endl;
return 0;
}

hbmy周赛1--E的更多相关文章

  1. hbmy周赛1--D

    D - Toy Cars Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  2. hbmy周赛1--C

    C - Exam Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. hbmy周赛1--B

    B - 改革春风吹满地 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  4. hbmy周赛1--A

    Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...

  5. 周赛-KIDx's Pagination 分类: 比赛 2015-08-02 08:23 7人阅读 评论(0) 收藏

    KIDx's Pagination Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...

  6. 2015浙江财经大学ACM有奖周赛(一) 题解报告

    2015浙江财经大学ACM有奖周赛(一) 题解报告 命题:丽丽&&黑鸡 这是命题者原话. 题目涉及的知识面比较广泛,有深度优先搜索.广度优先搜索.数学题.几何题.贪心算法.枚举.二进制 ...

  7. Leetcode 第133场周赛解题报告

    今天参加了leetcode的周赛,算法比赛,要求速度比较快.有思路就立马启动,不会纠结是否有更好的方法或代码可读性.只要在算法复杂度数量级内,基本上是怎么实现快速就怎么来了. 比赛时先看的第二题,一看 ...

  8. 牛客OI周赛9-提高组题目记录

    牛客OI周赛9-提高组题目记录 昨天晚上做了这一套比赛,觉得题目质量挺高,而且有一些非常有趣而且非常清奇的脑回路在里边,于是记录在此. T1: 扫雷 题目链接 设 \(f_i\) 表示扫到第 \(i\ ...

  9. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

随机推荐

  1. openstack ocata版本简化安装

    Network Time Protocol (NTP) Controller Node apt install chrony Edit the /etc/chrony/chrony.conf 添加如下 ...

  2. 在海航云中部署 keepalived

    **本文属自我学习,不适合转载** 1. 准备工作 1.1 网络方面的准备工作 1.1.1 创建安全组 注意这里要添加 vrrp 协议支持,否则 keepalived 将无法正常工作. 1.1.2 创 ...

  3. 由linux命令谈学习操作系统的重要性

    linux命令妙趣横生,喜欢敲命令行的人会深有体会,但是没有系统学习过操作系统的话,很多命令还是难以理解的.讲实在话,大多数linux爱好者常敲的都是这些方面的: 文件系统 磁盘 网络 系统状态 账户 ...

  4. 解决CUICatalog: Invalid asset name supplied问题

    这个问题其实是老问题,产生原因就是因为在使用的时候 [UIImage imageNamed:]时,图片不存在或者传入的图片名为nil.

  5. Odwiedziny[POI 2015]

    题目描述 给定一棵n个点的树,树上每条边的长度都为1,第i个点的权值为a[i]. Byteasar想要走遍这整棵树,他会按照某个1到n的全排列b走n-1次,第i次他会从b[i]点走到b[i+1]点,并 ...

  6. Head First设计模式之观察者模式

    一.定义 观察者设计模式定义了对象间的一种一对多的依赖关系,以便一个对象的状态发生变化时,所有依赖于它的对象都得到通知并自动刷新. 有时被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多 ...

  7. MVC框架实例构建

    转自:http://www.cnblogs.com/levenyes/p/3290885.html MVC全名是Model View Controller,是模型(model)-视图(view)-控制 ...

  8. Web服务器(Apache)与Servlet容器(Tomcat)

    之前一直比较迷惑Apache与Tomcat的关系,通过查询资料,有所了解,现记录于此. Apache与Tomcat 两者定位:Apache是HTTP Web服务器,Tomcat是Web容器. 有一个非 ...

  9. iOS获取各种数据方法整理以及IDFA与IDFV使用环境

    iOS获取APP版本号: NSString *AppVersion  =  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBun ...

  10. Spark源码分析 之 Driver和Excutor是怎么跑起来的?(2.2.0版本)

    今天抽空回顾了一下Spark相关的源码,本来想要了解一下Block的管理机制,但是看着看着就回到了SparkContext的创建与使用.正好之前没有正式的整理过这部分的内容,这次就顺带着回顾一下. S ...