• 2017.10.13


      

    公约数和公倍数

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:1
     
    描述
    小明被一个问题给难住了,现在需要你帮帮忙。问题是:给出两个正整数,求出它们的最大公约数和最小公倍数。
     
    输入
    第一行输入一个整数n(0<n<=10000),表示有n组测试数据;
    随后的n行输入两个整数i,j(0<i,j<=32767)。
    输出
    输出每组测试数据的最大公约数和最小公倍数
    样例输入
    3
    6 6
    12 11
    33 22
    
    样例输出
    6 6
    1 132
    11 66


    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main(int argc, char *argv[])
    {
    int n,k;
    int i,j;
    int t,m;
    int temp=0;
    int count1=0;
    int count2=0;
    int arr1[100]={0};
    int arr2[100];
    scanf("%d",&n);
    for(k=0;k<n;k++)
    {
    scanf("%d",&i);
    scanf("%d",&j);
    if(i<j)
    {
    temp=i;
    i=j;
    j=temp;
    }
    for(t=1;t<j+1;t++)
    {
    if(i%t==0&&j%t==0) //最大公约数
    {
    arr1[count1]=t;
    count1++;
    }
    }
    for(m=i;m<i*j+1;m++) //最小公倍数
    {
    if(m%i==0&&m%j==0)
    {
    arr2[count2]=m;
    count2++;
    }
    }
    printf("%d ",arr1[count1-1]);
    printf("%d ",arr2[0]);
    count1=count2=0; //一定要记得清零
    printf(" "); //转换
    }
    return 0;
    }

    
    
  • 相关阅读:
    Java vs Python
    Compiled Language vs Scripting Language
    445. Add Two Numbers II
    213. House Robber II
    198. House Robber
    276. Paint Fence
    77. Combinations
    54. Spiral Matrix
    82. Remove Duplicates from Sorted List II
    80. Remove Duplicates from Sorted Array II
  • 原文地址:https://www.cnblogs.com/panlangen/p/7704877.html
Copyright © 2020-2023  润新知