C:绝地求生(pubg

绝地求生(pubg)

https://ac.nowcoder.com/acm/contest/6226/D

D:绝地求生(pubg)

请问最少要拾取多少子弹到背包,才能使得背包里的子弹数恰好是x的倍数又恰好是y的
倍数。

一句话题意:读入x,y,求x,y的最小公倍数

求出x与y的最小公约数(gcd),然后用x*y除以gcd(x,y)

需要注意的是这题要开long long……

C++虽然有个gcd函数不过我还是自己写了个(

代码如下:

#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#define int long long
using namespace std;
int T,x,y;

inline int read()
{
    int n=0,f=1; char c=getchar();
    while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
    while(c>='0' && c<='9') {n=n*10+c-'0'; c=getchar();}
    return n*f;
}

inline int gcd(int x,int y) {return y==0?x:gcd(y,x%y);}

signed main(void)
{
    T=read();
    for(int i=1; i<=T; i++)
    {
        x=read(); y=read();
        int g=gcd(x,y);
        cout<<"Case "<<i<<": "<<x/g*y<<'\n';
    }
    return 0;
}
全部评论
这个可以调用一下__gcd(x,y)这个库函数,在algorithm头文件包含,返回值就是x,y的最大公约数
点赞 回复 分享
发布于 2020-07-08 17:28

相关推荐

投递小天才等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-21 11:29
已编辑
斯卡蒂味的鱼汤:知道你不会来数马,就不捞你😂最近数马疯狂扩招,招聘要求挺低的,你能力肯定够,应该就是因为太强了,知道你不会来才不捞你
投递腾讯云智研发等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务