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);
    }
}
相关推荐
程序员张31 小时前
Maven编译和打包插件
java·spring boot·maven
ybq195133454312 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339463 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8503 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
枯萎穿心攻击4 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
DKPT4 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue5 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
好奇的菜鸟5 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
m0_555762906 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
DuelCode6 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis