问题 4868 --虎哥的数字钟

4868: 虎哥的数字钟★★

时间限制: 1 Sec  内存限制: 128 MB
提交: 112  解决: 86
[提交][状态][命题人:]

题目描述

虎哥有一个n位的数字钟,每位允许出现0~9的数字,现在虎哥想要采用尽可少的操作让这个数字钟显示为全0。数字钟允许的如下两种操作:
(1)将它的最后一位减一。
(2)交换任意两位。

你能帮他计算出数字钟显示为全0需要的最少操作次数吗?

You are given a digital clock with nn digits. Each digit shows an integer from 0 to 9, so the whole clock shows an integer from 0 to 10^n−1. The clock will show leading zeroes if the number is smaller than 10^n−1.

You want the clock to show 0 with as few operations as possible. In an operation, you can do one of the following:

  • decrease the number on the clock by 1, or
  • swap two digits (you can choose which digits to swap, and they don't have to be adjacent).

Your task is to determine the minimum number of operations needed to make the clock show 0.

输入

第一行为T(1<=T<=1000),表示有T组测试数据。

每组测试数据包括两行,第一行为一个整数n(1<=n<=100);第二行为一个n位整数。

输出

每组测试数据输出一个整数,表示使数字钟显示为全0需要的最少操作次数。

For each test case, print one integer: the minimum number of operations needed to make the clock show 0.

样例输入
Copy
7
3
007
4
1000
5
00000
3
103
4
2020
9
123456789
30
001678294039710047203946100020
样例输出
Copy
7
2
0
5
6
53
115

提示

第一个样例,将最后一位执行7次减一操作
第二个样例,先将第一位与最后一位交换,然后再将最后一位执行1次减一操作

第三个样例,数字本身已经为0了,不需要操作

In the first example, it's optimal to just decrease the number 7 times.

In the second example, we can first swap the first and last position and then decrease the number by 1.

In the third example, the clock already shows 0, so we don't have to perform any operations.

来源

[提交][状态]