最大公约数
题解
讨论
查看他人的提交
题号:NC217392
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld
题目描述
如果有一个自然数 a 能被自然数 b 整除,则称 a 为 b 的倍数, b 为 a 的约数。几个自然数公有的约数,叫做这几个自然数的公约数。公约数中最大的一个公约数,称为这几个自然数的最大公约数。
输入 a 和 b , 请返回 a 和 b 的最大公约数。
数据范围:
进阶:空间复杂度
,时间复杂度
示例1
输入
复制
3,6
3,6
返回值
复制
3
3
示例2
输入
复制
8,12
8,12
返回值
复制
4
4
备注:
a和b的范围是[1-10
9
]
最大公约数
返回全部题目
列表加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ public int gcd (int a, int b) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ int gcd(int a, int b) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 求出a、b的最大公约数。 # @param a int整型 # @param b int整型 # @return int整型 # class Solution: def gcd(self , a , b ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ public int gcd (int a, int b) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ function gcd( a , b ) { // write code here } module.exports = { gcd : gcd };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 求出a、b的最大公约数。 # @param a int整型 # @param b int整型 # @return int整型 # class Solution: def gcd(self , a: int, b: int) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ func gcd( a int , b int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ int gcd(int a, int b ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 求出a、b的最大公约数。 # @param a int整型 # @param b int整型 # @return int整型 # class Solution def gcd(a, b) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ def gcd(a: Int,b: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ fun gcd(a: Int,b: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ public int gcd (int a, int b) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ export function gcd(a: number, b: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ func gcd ( _ a: Int, _ b: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 求出a、b的最大公约数。 * @param a int整型 * @param b int整型 * @return int整型 */ pub fn gcd(&self, a: i32, b: i32) -> i32 { // write code here } }
3,6
3
8,12
4