2021年寒假每日一題,2017~2019年的省賽真題,
本文內容由倪文迪(華東理工大學計算機系軟體192班)和羅勇軍老師提供,
后面的每日一題,每題發一個新博文,請大家每天看博客藍橋杯專欄: https://blog.csdn.net/weixin_43914593/category_10721247.html
每一題提供C++、Java、Python三種語言的代碼,
文章目錄
- 1、題目描述
- 2、題解
- 3、C++代碼
- 4、Java代碼
- 5、Python代碼
2018省賽A組第3題,題目鏈接:
乘積尾零 http://oj.ecustacm.cn/problem.php?id=1361
1、題目描述
給10行資料,每行有10個整數,請你求出它們的乘積的末尾有多少個零?
2、題解
??填空,送分,
??倪文迪說:“將統計0的個數轉化為統計5與2的個數,最終取二者的最小值,”100個數的乘積太大,c++處理不了,聯想到10等于
2
×
5
2\times5
2×5,那么只要統計乘積中有多少2和5的因子,特別是5的個數,它一般比2的個數多,所以有多少個5,就有多少個零,
??羅老師再次無話可說,這次連廢話也不想說,
??吐槽一句:這題目太古老了,有5000年了,
??下面的代碼,Python還有點意思,它能直接處理大數,
3、C++代碼
#include<bits/stdc++.h>
using namespace std;
int main(void){
int cnt2 = 0,cnt5 = 0;
for(int i=0;i<100;++i){
int x;
cin>>x;
while(x && x%2==0){
cnt2++;
x/=2;
}
while(x && x%5==0){
cnt5++;
x/=5;
}
}
cout<<min(cnt2,cnt5)<<endl;
return 0;
}
4、Java代碼
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = 100;
int cnt2 = 0, cnt5 = 0;
while(n > 0) {
n--;
int x = cin.nextInt();
while(x % 2 == 0) {
x /= 2;
cnt2 ++;
}
while(x % 5 == 0) {
x /=5;
cnt5 ++;
}
}
System.out.println(Math.min(cnt2, cnt5));
}
}
5、Python代碼
??太好了,是大數!
??在Python眼里,一切大數都是浮云!
??管它什么2、5,直接硬算!
??小聲說一句,其實Python的大數也不是無限大的,下面的代碼,如果一股腦先算出所有的100個數的乘積s,s實在太大了,s也是會溢位的,所以乘一個數,就看乘積s后面有沒有0,如果有0就除以10,這樣s就比較小了,
'''
輸入放在一行中,不要分10行:
5650 4542 3554 473 946 4114 3871 9073 90 4329 2758 7949 6113 5659 5245 7432 3051 4434 6704 3594 9937 1173 6866 3397 4759 7557 3070 2287 1453 9899 1486 5722 3135 1170 4014 5510 5120 729 2880 9019 2049 698 4582 4346 4427 646 9742 7340 1230 7683 5693 7015 6887 7381 4172 4341 2909 2027 7355 5649 6701 6645 1671 5978 2704 9926 295 3125 3878 6785 2066 4247 4800 1578 6652 4616 1113 6205 3264 2915 3966 5291 2904 1285 2193 1428 2265 8730 9436 7074 689 5510 8243 6114 337 4096 8199 7313 3685 211
'''
num=[int(i) for i in input().split()] #input().split()是讀一行以空格分開的元素,然后用int()轉為整數
s = 1
cnt = 0
for i in range(len(num)): #連續乘,一邊乘一邊統計0的個數
s *= num[i] #乘一個數
while s%10 == 0: #末尾是零
s /= 10 #除以10,把末尾零去掉
cnt += 1
print(cnt)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/249465.html
標籤:java
上一篇:資料庫三范式和反三范式
