#301 (div.2) A. Combination Lock

1.题目描述:点击打开链接

2.解题思路:本题要求当前的密码经过最少的转动后和最终的密码一样。可以利用贪心法解决。考虑第i位,假设当前位a,目标为b,那么有两种转动方式,所需的步数分别为abs(a-b)和10-abs(a,b),取较小者即可。累加后即为最小的步数。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef long long ll;
#define me(s) memset((s),0,sizeof(s))
#define For(i,n) for(int i=0;i<(n);i++)

int main()
{
    //freopen("t.txt", "r", stdin);
    int n;
    while (~scanf("%d", &n))
    {
        string str1, str2;
        cin >> str1 >> str2;
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
            int m = abs(str1[i] - str2[i]);
            m = min(m, 10 - m);
            sum += m;
        }
        cout << sum << endl;
    }
    return 0;
}
时间: 2024-10-10 07:19:42

#301 (div.2) A. Combination Lock的相关文章

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

题目传送门 1 /* 2 贪心水题:累加到目标数字的距离,两头找取最小值 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f; 12 char s[MAXN],

Codeforces Round #301 (Div. 2) -- (A,B,C,D)

题目传送:Codeforces Round #301 (Div. 2) A. Combination Lock 水题,求最小移动次数,简单贪心一下即可 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #i

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

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

HDU 3104 Combination Lock(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3104 Problem Description A combination lock consists of a circular dial, which can be turned (clockwise or counterclockwise) and is embedded into the "fixed" part of the lock. The dial has N evenly

CodeForces Round #301 Div.2

今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1000 + 10; 5 6 char s1[maxn], s2[maxn]; 7 8 int main() 9 { 10 int n; cin >>

Combination Lock

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a Microsoft interviewer is in the room though the door is locked. There is a combination lock on the door. There are N rotators on the lock, each consists o

1.3.4 Combination Lock

Farmer John's cows keep escaping from his farm and causing mischief. To try and prevent them from leaving, he purchases a fancy combination lock to keep his cows from opening the pasture gate. Knowing that his cows are quite clever, Farmer John wants

hihocoder-第六十一周 Combination Lock

题目1 : Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a Microsoft interviewer is in the room though the door is locked. There is a combination lock on the door. There are N rotators on th

Hiho----微软笔试题《Combination Lock》

Combination Lock 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a Microsoft interviewer is in the room though the door is locked. There is a combination lock on the door. There are N rotators on the lock