JAVA计算题

本文涵盖了JAVA编程中的一些基础算法题目,包括交换数据、打印乘法表、求完全数、水仙花数、质数判断、斐波那契数列、猴子吃桃问题、消除相同元素、分解质因数、计算最大公约数和最小公倍数,以及字符统计等。这些题目旨在提升JAVA程序员的算法思维和实践能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.交换数据
public class Swap {
    public static void main(String[] args) {
        int a = 3;
        int b = 4;

        //第一种
        /*int temp;
        temp = a;
        a = b;
        b = temp;*/

        //第二种
        /*a = a+b;
        b = a-b;
        a = a-b;*/

        //第三种
        a = a^b;
        b = a^b;
        a = a^b;
        System.out.println("a="+a+",b="+b);
    }
}
2.打印nn乘法表
public class nnCf {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入n的值:");
        int n = in.nextInt();
        print(n);
    }

    public static void print(int n){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i;j++){
                System.out.print(i+"x"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}
3.求完全数
public class ti3 {
    public static void main(String[] args) {
        System.out.println("完全数如下:");
        for(int i=1;i<10000;i++){
            int sum = 0;
            for(int j=1;j<i/2+1;j++){
                if(i%j == 0){
                    sum += j;
                }
            }
            if(sum==i){
                System.out.println(i);
            }
        }
    }
}
4.求水仙花数
public class shuiXH {
    public static void main(String[] args) {
        int b = 0;
        int s = 0;
        int g = 0;
        int count = 0; 
        System.out.println("水仙花如下:");
        for(int i=100;i<999;i++){
            b = i/100;
            s = i/10%10;
            g = i%10;
            if(b*b*b+s*s*s+g*g*g == i){
                System.out.println(i);
                count++;
            }
        }
        System.out.println("水仙花个数为:"+count);
    }
}
5.求100以内的质数(素数)
public class prime {
    public static void main(String[] args) {
        System.out.println("素数如下:");
        int count = 0;
        for(int i=2;i<=100;i++){
            if(isPrime(i)){
                System.out.print(i+"\t");
                count++;
                if(count%5==0){
                    System.out.println();
                }
            }
        }
        System.out.println("个数为:"+count);
    }
    public static Boolean isPrime(int num){
        int i=2;
        for(i=2;i<num;i++){
            if(num%i==0){
                break;
            }
        }
        if(i>=num){
            return true;
        }
        return false;
    }
}
6.求斐波拉契数列(1,1,2,3,5,8,13,……;生兔子问题)
public class Fibonacci2 {
    public static void main(String[] args) {
        for(int i=1;i<=50;i++){
            System.out.println("第"+i+"个月的兔子总数为"+getFibon(i));
        }
    }

    public static long getFibon(int n){
        if(n==1 || n==2){
            return 1;
        }else{
            return getFibon(n-1)+getFibon(n-2);
        }
    }
7.猴子吃桃问题
//题目:猴子第一天摘了若干个桃子,当即吃了一半,还不解馋,又多吃了一个;第二天,吃剩下的桃子的一半,还不过瘾,又多吃了一个;以后每天都吃前一天剩下的一半多一个,到第10天想再吃时,只剩下一个桃子了。问第一天共摘了多少个桃子?

public class Peach {
    public static void main(String[] args) {
        int peach = getPeach(1);
        System.out.println("第一天摘的桃子总数为:"+peach);
    }
    public static int getPeach(int day){
        if(day==10){
            return 1;
        }else{
            return (getPeach(day+1)+1)*2;
        }
    }
}
8.消除相同的数
public class Lianliankan {
    public static void main(String[] args) {
        //创建图案
        int[][] pic = create();
        //打印
        print(pic);
        int[][] pic2 = clear(pic);
        System.out.println("消除后如下");
        print(pic2);
    }
    public static void print(int[][] pic){
        for(int i=0;i<pic.length;i++){
            for(int j=0;j<pic.length;j++){
                System.out.print(pic[i][j]+"  ");
            }
            System.out.println();
        }
    }
    public static int[][] create(){
        int[][] pic = new int[6][6];
        for(int i=0;i<pic.length;i++){
            for(int j=0;j<pic.length;j++){
                pic[i][j] = (int)(Math.random()*9+1);
            }
        }
        return pic;
    }

    //
    public static int[][] clear(int pic[][]){
        int flag = 1;
        for(int i=0;i<pic.length;i++){
            for(int j=0;j<pic[0].length;j++){
                flag = 1;
                for(int m=0;m<pic.length;m++){
                    for(int n=0;n<pic[0].length;n++){
                        if(!(i==m && j==n)&&pic[i][j]==pic[m][n] && pic[i][j]!=0){
                            pic[i][j] = 0;
                            pic[m][n] = 0;
                            flag = -1;
                            break;
                        }
                    }
                    if(flag==-1){
                        break;
                    }
                }
            }
        }
        return pic;
    }
}   
9.分解质因数
public class Fenjie {
    public static void main(String[] args) {
        resolve(12);
    }

    public static void resolve(int num) {
        String res = "1";
        int temp = num;
        int k = 2;
        if (temp < 1) {
            System.out.println("输入的数小于0,不符合要求");
        } else {
            while (k <= temp) {
                if (temp % k == 0) {
                    res += "*" + k;
                    temp /= k;
                } else {
                    k++;
                }
            }
        }
        System.out.println(num+"分解质因数:"+res);
    }
}
//输出结果为:12分解质因数:1*2*2*3
10.求最大公约数和最小公倍数。
public class GongYBS {
    public static void main(String[] args) {
        getRes(8, 12);
    }
    public static void getRes(int a,int b){
        int atemp = a;
        int btemp = b;
        int k =2;
        int res = 1;
        while(k<=atemp || k<=btemp){
            if(atemp%k==0 && btemp%k==0){
                res *= k;
                atemp /= k;
                btemp /= k;
            }else{
                k++;
            }
        }
        System.out.println("最大公约数为:"+res);
        System.out.println("最小公倍数为:"+res*atemp*btemp);
    }
}
11.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
public class Tongji {
    //输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        String str = sc.nextLine();
        int letterCount = 0;
        int spaceCount = 0;
        int numCount = 0;
        int otherCount = 0;
        char[] charOfstr = str.toCharArray();
        for(int i=0;i<charOfstr.length;i++){
            if(Character.isLetter(charOfstr[i])){
                letterCount++;
            }else if(Character.isDigit(charOfstr[i])){
                spaceCount++;   
            }else if(Character.isSpaceChar(charOfstr[i])){
                numCount++;
            }else{
                otherCount++;
            }
        }
        System.out.println("英文字母个数:"+letterCount);
        System.out.println("空格个数:"+spaceCount);
        System.out.println("数字个数:"+numCount);
        System.out.println("其他个数:"+otherCount);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值