Ref: ITSA 53 - [Problem 3] 號碼鎖最少轉動幾次
/*******************************************************/
/* ITSA 53 - [Problem 3] 號碼鎖最少轉動幾次 */
/* Author: awei0905 [at] awei0905.blogspot.tw */
/* Version: 2017/04/09 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int N, count, temp;
char init[11], target[11];
scanf("%d", &N);
while (N-- && scanf("%s%s", init, target)) {
count = 0;
for (int i = 0; i < strlen(init); i++) {
if (init[i] > target[i]){
temp = init[i]; init[i] = target[i]; target[i] = temp;
}
temp = target[i] - init[i];
if (temp > 10 - temp) temp = 10 - temp;
count += temp;
}
printf("%d\n", count);
}
}
Debug: I/O7
000 217
1234 5678
12389 21354
0 9
9 0
0123911173 9999999999
9897965201 0000000000
6
16
10
1
1
22
20
沒有留言:
張貼留言