题目链接:http://codeforces.com/problemset/problem/540/A

题意:

输入的两个长度一样的数,求对应位置的某位数到下一个数需要最小的步长,每次只能先前或先后走以步,求所有的步长的的和。(如下例)

Sample test(s)
input
5
82195
64723
output
13

解题:

这个很简单,暴力破解

可以把数字0-9是个长度是10的循环,对于任意两个数之间的最小距离就等于:min{|a-b|,|10-|a-b||}

对于两个数之间的距离很容易想到是 :|a-b|,但是这个要求的最小距离,a绕一圈回来的距离是10,若按照一定方向走:a->b  ,b->a 的距离也是10,则就有了 |10-|a-b||这个b->a的距离;我们要求的是最小距离,所以要在这个两个距离中取最小值

Java程序:

import java.util.Scanner;

public class A540 {
static void run0(){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String before = in.next();
String end = in.next();
int sum = 0;
for(int i =0;i<n;i++){
int a = (int)before.charAt(i);
int b= (int)end.charAt(i);
sum = sum + Math.min(Math.abs(a-b), Math.abs(10-Math.abs(a-b)));
}
System.out.println(sum);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
run0();
} }

540A: Combination Lock的更多相关文章

  1. Codeforces 540A - Combination Lock

    Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he ...

  2. hihocoder #1058 Combination Lock

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...

  3. 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock

    题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...

  4. 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 ...

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

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

  6. hihocoder-第六十一周 Combination Lock

    题目1 : Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview roo ...

  7. A - Combination Lock

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Scroog ...

  8. HDU 3104 Combination Lock(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3104 Problem Description A combination lock consists ...

  9. 洛谷 P2693 [USACO1.3]号码锁 Combination Lock

    P2693 [USACO1.3]号码锁 Combination Lock 题目描述 农夫约翰的奶牛不停地从他的农场中逃出来,导致了很多损害.为了防止它们再逃出来,他买了一只很大的号码锁以防止奶牛们打开 ...

随机推荐

  1. 使用 PHP cURL 提交 JSON 数据

    http://www.oschina.net/code/snippet_54100_7351 http://www.lornajane.net/posts/2011/posting-json-data ...

  2. rm -rf删除过多文件提示参数过长

    cd /var/tmp/ find . -name "*.log"|xargs rm -rf "*.log"

  3. JSP基本知识

    JSP基本原理: JSP本质是Servlet(一个特殊的Java类),当用户向指定Servlet发送请求时,Servlet利用输出流动态生成HTML页面.JSP通过在标准的HTML页面中嵌入Java代 ...

  4. 数据类型 swift

    1整形 Int,Int8,Int16,Int32,Int64 UInt,UInt8,UInt16,UInt32,UInt64 其中Int,UInt始终和当前平台的原生字长相同(32位机,64位机) 查 ...

  5. cocos2dx中如何从一张图片中切割一部分显示成小图片

    1.通常我们拿到的资源中,通常都是许多张小图片压缩到一张图片里了,我们如何在使用的时候把它切割出来呢? 2.例如我们要把上面这张图片按组分隔开来 CCSprite* newGameNormal = C ...

  6. Z_blog博客尝试 http://www.uuxin.com/

    原来的博客由于没有备份所有数据全部丢失,很是郁闷. 又用Z-BLOG新建了一个博客.http://www.uuxin.com

  7. Java实现Tire

    Trie,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比 ...

  8. Jquery $.getJSON()设置同步

    如下: $.ajaxSettings.async = false; $.getJSON('/AjaxSwitchDynamicInfo/GetPortUsedCount.cspx', { switch ...

  9. shell编程之重定向

    标准输入,输出和错误---------------------------------文件文件                描述符---------------------------------输 ...

  10. 【BZOJ】【3473】字符串

    后缀数组 Orz zyf 神题不会做啊,先坑着吧……sigh //BZOJ 3473 #include<vector> #include<cstdio> #include< ...