1 条题解

  • 0
    @ 2025-4-7 21:28:46

    C :

    #include<stdio.h>
    int main(){
    int m,n,temp;
    scanf("%d %d",&m,&n);
    while(m%n!=0){
      temp=n;
      n=m%n;
      m=temp;}
    printf("%d\n",n);
    return 0;
    }
    

    C++ :

    #include <stdio.h>
    int main() {
    	int m, n, t;
    	scanf("%d %d", &m, &n);
    	/* 比较m和n的大小,通过交换保证m <= n */
    	if (m > n) {
    		t = m;
    		m = n;
    		n = t;
    	}
    	/* 进行欧几里得辗转相除法求出最大公约数,并保存在n中 */
    	while (m != 0) {
    		t = m;
    		m = n % m;
    		n = t;
    	}
    	printf("%d\n", n);
    	return 0;
    }
    
    

    Pascal :

    var
      m,n,r:integer;
    begin
      readln(m,n);
      repeat
        r:=m mod n;
        m:=n;
        n:=r;
      until r=0;
      writeln(m);
    end.
    

    Java :

    import java.util.*;
    public class Main {
    	public static void main(String args[]) {
    		Scanner cin = new Scanner(System.in);
    		int m,n;
    		int temp;
    		m=cin.nextInt();
    		n=cin.nextInt();
    		if(m<n){
    			temp=m;
    			m=n;
    			n=temp;
    		}
    		while(n!=0){
    			temp=m%n;
    			m=n;
    			n=temp;
    		}
    		System.out.println(m);
    	}
    }
    

    Python :

    def gcd(a, b):
      while b:
        a, b = b, a%b
      return a
    a,b = raw_input().split()
    print gcd(int(a),int(b))
    

    C# :

    using System;
    namespace C语言
    {
        class _2_9
        {
            static int gcd(int m, int n)
            {
                int a, b;
                if (m > n) { a = m; b = n; }
                else { a = n; b = m; }
                if (b == 0) return a;
                while (b!=0) 
                {
                    int t = a % b;
                    a = b; b = t;
                }
                return a;
            }
            static void Main()
            {
                string[] s = Console.ReadLine().Split(' ');
                int m = Convert.ToInt32(s[0]), n = Convert.ToInt32(s[1]);
                Console.WriteLine(gcd(m,n));
                
                Console.ReadLine();        
            }
         }
    }
    
    
    • 1

    信息

    ID
    1228
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者