练习cf1883A. Morning

题目如下
A. Morning
time limit per test2 seconds
memory limit per test256 megabytes
You are given a four-digit pin code consisting of digits from 0 to 9 that needs to be entered. Initially, the cursor points to the digit 1. In one second, you can perform exactly one of the following two actions:

Press the cursor to display the current digit,
Move the cursor to any adjacent digit.

c4411622f09de0cbd488d8ce5e86c51e2223ee17

The image above shows the device you are using to enter the pin code. For example, for the digit 5, the adjacent digits are 4 and 6, and for the digit 0, there is only one adjacent digit, 9.

Determine the minimum number of seconds required to enter the given four-digit pin code.

Input
Each test consists of multiple test cases. The first line contains a single integer 𝑡 (1≤𝑡≤104) - the number of the test cases. This is followed by their description.

The single line of each test case describes the pin code as a string of length 4, consisting of digits from 0 to 9.

Output
For each test case, output the minimum number of seconds required to enter the given pin code.

题目大意
现有四位数的密码,从前往后输入,初始时刻光标停留在数字1处,光标只能一个一个移动并且每次按下也计入操作数,那么完整的打出这四位密码需要的操作数是多少。

题目分析
因为光标是一个一个数移动,那么只要计算两个数字之间的差的绝对值即可。
注意“0”的位置在9后那么只要把0当成10进行操作即可。

点击查看代码
include <iostream>
#include <cmath>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        int now = 1, sum = 0;
        for (int i = 0; i < 4; i++){
            if(s[i] == '0'){
                sum += abs(now - 10) + 1;
                now = 10;
            }else{
                sum += abs(now - (s[i] - '0')) + 1;
                now = s[i] - '0';
            }

        }
        cout << sum << endl;
    }
    return 0;
}
posted @ 2025-07-19 21:30  sirro1uta  阅读(8)  评论(0)    收藏  举报