kdxf提前批20230715-第一题

给定一个长度为n的数组,数组元素为a1, a2, . . , an,每次能删除任意a的任意一位,求将所有数字变成0最少需要几步。例如103若删除第1位则变成3;若删除第2位则变成13;若删除第3位则变成10。

输入描述

第一行一个正整数n代表数组长度。接下来一行n个数第j个数代表a。

1≤n ≤10^5 0≤ai≤10^9

输出描述

输出一行一个数代表答案。


示例:

1
2
3
4
5
6
输入:
5
10 13 22 100 30

输出:
7


分析:

难度:容易

1
遍历nums中的每个数字,得到不为0的数位即可。 最大位数为10,因此时间复杂度为10 * 1e5

代码

  • C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main() {
int n;
cin >> n;

int res = 0;
for (int i = 0; i < n; i ++) {
int x;
cin >> x;
int t = x;
int tot = 0;
while (t) {
tot += (int)(t % 10 != 0);
t /= 10;
}
res += tot;
}

cout << res << endl;


return 0;
}
  • java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import javax.swing.plaf.synth.SynthTextAreaUI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int n = in.nextInt();

int res = 0;
for (int i = 0; i < n; i ++) {
int x = in.nextInt();
int t = x;
int tot = 0;
while (t != 0) {
if (t % 10 != 0) tot ++;
t /= 10;
}

res += tot;
}

System.out.println(res);
}
}
  • Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
n = int(input())
arr = map(int, input().split())

def main():
ans = 0
for x in arr:
t, tot = x, 0
while t != 0:
if t % 10 != 0: tot += 1
t /= 10
ans += tot
print(ans)

main()
  • Golang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import "fmt"

func main() {
var n int
fmt.Scan(&n)

res := 0
for i := 0; i < n; i++ {
var x int
fmt.Scan(&x)
t := x
tot := 0
for t != 0 {
tot += 1
if t % 10 == 0 {
tot -= 1
}
t /= 10
}
res += tot
}

fmt.Println(res)
}
0%