Java.函数-acwing

题目一: n的阶乘

804. n的阶乘 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    private static int fact(int n) {
        int res = 1;
        for(int i = 1; i <= n; i ++) 
            res *= i;
        
        return res;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        System.out.println(fact(n));
    }
}

题目二:x和y的最大值

805. x和y的最大值 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    private static int max(int x, int y) {
        if(x > y) return x;
        else return y;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int x = sc.nextInt(), y = sc.nextInt();
        System.out.println(max(x,y));
    }
}

题目三:最大公约数

808. 最大公约数 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    private static int gcd(int a, int b) {
        while(b != 0) {
            int c = a%b;
            a = b; b = c;
        }
        return a;
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b = sc.nextInt();
        
        System.out.println(gcd(a,b));
    }
}

题目四:交换数值

811. 交换数值 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int[] a = {sc.nextInt(), sc.nextInt()};
        swap(a);
        System.out.printf("%d %d\n",a[0],a[1]);
    }
    
    private static void swap(int[] a) {
        int t = a[0]; a[0] = a[1]; a[1] = t;
    }
}

题目五:打印数字

812. 打印数字 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt(), size = sc.nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; i ++) 
            a[i] = sc.nextInt();
        
        print1D(a,size);
    }
    
    private static void print1D(int[] a, int size) {
        for(int i = 0; i < size; i ++) 
            System.out.printf("%d ",a[i]);
    }
}

题目六:打印矩阵

813. 打印矩阵 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int r = sc.nextInt(), c = sc.nextInt();
        int[][] a = new int[r][c];
        for(int i = 0; i < r; i ++) 
            for(int j = 0; j < c; j ++) 
                a[i][j] = sc.nextInt();
        
        print2D(a,r,c);
    }
    
    private static void print2D(int[][] a, int r, int c) {
        for(int i = 0; i < r; i ++) {
            for(int j = 0; j < c; j ++) {
                System.out.printf("%d ",a[i][j]);
            }
            System.out.println("");
        }
    }
}

题目七:递归求阶乘

819. 递归求阶乘 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        System.out.println(fact(n));
    }
    
    private static int fact(int n) {
        if(n == 1) return 1;
        return fact(n-1)*n;
    }
}

题目八:递归求斐波那契数列

820. 递归求斐波那契数列 - AcWing题库

代码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        System.out.println(solve(n));
    }
    
    private static int solve(int n) {
        if(n == 1 || n == 2) return 1;
        return solve(n-1) + solve(n-2);
    }
}
相关推荐
都叫我大帅哥25 分钟前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮2 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
古月-一个C++方向的小白5 小时前
C++11之lambda表达式与包装器
开发语言·c++
沐知全栈开发5 小时前
Eclipse 生成 jar 包
开发语言
Dcs5 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java
杭州杭州杭州6 小时前
Python笔记
开发语言·笔记·python
tanyongxi666 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
fouryears_234177 小时前
Spring,Spring Boot 和 Spring MVC 的关系以及区别
java·spring boot·spring·mvc
阿葱(聪)7 小时前
java 在k8s中的部署流程
java·开发语言·docker·kubernetes
浮生带你学Java8 小时前
2025Java面试题及答案整理( 2025年 7 月最新版,持续更新)
java·开发语言·数据库·面试·职场和发展