鏈接:https://www.nowcoder.com/acm/contest/135/H
來源:牛客網
H 最大公約數
題目描述
給定兩個正整數a,b,求a,b的最小公倍數。(即[a,b])
輸入描述:
兩個整整數,a,b
輸出描述:
一個正整數,表示[a,b]
#include "bits/stdc++.h"
using namespace std;
unsigned long long gcd(unsigned long long a,unsigned long long b){
return b==0?a:gcd(b,a%b);
}
int main(){
unsigned long long a,b,t,g;
while(cin>>a>>b){
t=gcd(a,b);
g=a/t*b/t*t;
cout<<g<<endl;
}
return 0;
}
鏈接:https://www.nowcoder.com/acm/contest/135/J
來源:牛客網
j-時間
題目描述
Apojacsleam是一個喜歡特殊時刻的人。
他定義了一個時刻,若電子錶顯示ab:ba(24小時制),則該時刻爲“迴文時刻”(可以有前導零)。例如00:00就是迴文時刻。
給定一個時刻,求此時刻的上一個和下一個迴文時刻。
J題附加:00:00就是24:00,沒有24:00這一時刻
J題附加:輸入可能有前導0,輸出不含前導0,例如10:1的意思是10:01,而10:10的輸出爲10:10
輸入描述:
兩個正整數,用“:”隔開,表示小時和分鐘,保證輸入時間合法。
輸出描述:
兩行,兩個時刻(不含前導0),用“:”隔開,表示上一個時刻和下一個時刻
暴力枚舉(?!),其實我覺得函數time1可以用一個for循環解決,但是比賽的時候思路很亂,就用了暴力的方法,這題只要注意邊界問題就很容易過了
#include "bits/stdc++.h"
using namespace std;
struct node{
int x,y;
}timee[24];
void time1(){
timee[0].x=0;timee[0].y=0;
timee[1].x=1;timee[1].y=10;
timee[2].x=2;timee[2].y=20;
timee[3].x=3;timee[3].y=30;
timee[4].x=4;timee[4].y=40;
timee[5].x=5;timee[5].y=50;
timee[10].x=10;timee[10].y=1;
timee[11].x=11;timee[11].y=11;
timee[12].x=12;timee[12].y=21;
timee[13].x=13;timee[13].y=31;
timee[14].x=14;timee[14].y=41;
timee[15].x=15;timee[15].y=51;
timee[20].x=20;timee[20].y=2;
timee[21].x=21;timee[21].y=12;
timee[22].x=22;timee[22].y=22;
timee[23].x=23;timee[23].y=32;
timee[24].x=24;timee[24].y=42;
}
int main(){
int a,b;
memset(timee,-1,sizeof(timee));
time1();
while(scanf("%d:%d",&a,&b)!=EOF){
for(int i=a;i>=0;i--){
if(timee[i].x==-1)
continue;
if(a==0&&b==0){
cout<<"23:32"<<endl;
break;
}
if((a==timee[i].x&&b>timee[i].y)||a>timee[i].x){
cout<<timee[i].x<<":"<<timee[i].y<<endl;
break;
}
}
for(int i=a;i<=24;i++){
if(timee[i].x==0)
continue;
if((a==timee[i].x&&b<timee[i].y)||a<timee[i].x){
if(timee[i].x==24){
cout<<"0:0"<<endl;
break;
}
cout<<timee[i].x<<":"<<timee[i].y<<endl;
break;
}
}
}
return 0;
}
鏈接:https://www.nowcoder.com/acm/contest/135/D
來源:牛客網
D-階乘
題目描述
輸入描述:
輸入數據共一行,一個正整數n,意義如“問題描述”。
輸出描述:
輸出一行描述答案: 一個正整數k,表示S的末尾有k個0
階乘計算之大數階乘與快速取模階乘計算
引例之速求階乘末尾0的個數
https://blog.csdn.net/deaidai/article/details/79253753
我們知道末尾0至於階乘中的因子2*5有關,並且階乘n中5的數量比2的數量少很多。
如果我們要考慮階乘尾數0的個數,就只要知道有多少個2*5即可,只要知道5的個數即可。
所以我們只要算出N!中5的個數。
而N!中5的個數公式=n/5+n/25+n/125…n/(5^m)
舉個例子。n=1000。
1-1000中5有a1=200。
1-1000有25有a2=40。
1-1000有125有a3=8。
1-1000有625有a4=1。
所以5的個數有5+25+125+625。爲什麼直接加?因爲如1-1000中的數75,它有2個5。他在a1中算過一次,在a2中也算過一次。
so。實現代碼如下。
#include <bits/stdc++.h>
using namespace std;
//n![1~10]:1,2,6,24,120,720,5040,40320,362880,3628800,
int main()
{
int n;
cin>>n;
int ans=0;
while(n){
ans+=n/5;
n/=5;
}
cout<<ans<<endl;
return 0;
}
但是用上面這個方法會超時,稍微修改了一下。 / / 階乘之間相乘,0的個數直接加在後面,所以相乘的階乘之間0的個數直接相加
下面是ac代碼:
#include "bits/stdc++.h"
using namespace std;
int main(){
int n;
long long ans=0,sum=0;
while(cin>>n){
sum=0;
for(int i=5;i<=n;i++){
int t=i;
while(t%5==0){
ans++;
t/=5;
}
sum=sum+ans;
}
cout<<sum<<endl;
}
return 0;
}