竞赛讨论区 > 牛客小白月赛5中H,J题题解
头像
sorrow8
发布于 2018-07-23 09:10
+ 关注

牛客小白月赛5中H,J题题解

H题

只要使用时间复杂度较低的求最小公倍数办法即可,我使用的是辗转相除法
时间复杂度O(logn)
代码

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main(){
    unsigned long long a,b,t,num,a1,b1;
    scanf("%llu %llu",&a,&b);
    if(a==b){
        num = a;
    }else{
        if(a<b){
            t = a;
            a = b;
            b = t;
        }
        a1 = a;
        b1 = b;
        while(a%b){
            t = a%b;
            a = b;
            b = t;
        }
        num=a1/b;
        num*=b1;
    }
    printf("%llu\n",num);
}

J题

只要依照顺序将其比对即可
时间复杂度O(1)
代码

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h> 
int a[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
int b[24] = {0,10,20,30,40,50,-1,-1,-1,-1, 1,11,21,31,41,51,-1,-1,-1,-1, 2,12,22,32};
int main(){
    int m,n,i;
    scanf("%d:%d",&m,&n);
    i = m;
    while(1){
        if(i==-1){
            i = 23;
        }
        if(i == m){
            if(b[i]<n&&b[i]!=-1){
                break;
            }
        }else{
            if(b[i]!=-1){
                break;
            }
        }
        i--;
    }
    printf("%d:%d\n",a[i],b[i]);
    i  = m;
    while(1){
        i%=24;
        if(i == m){
            if(b[i]>n){
                break;
            }
        }else{
            if(b[i]!=-1){
                break;
            }
        }
        i++;
    }
    printf("%d:%d\n",a[i],b[i]);
}

全部评论

(1) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐