北邮java oj作业1

这篇博客包含三个Java程序示例。第一个程序用于生成特定形状的矩阵;第二个程序展示了个人所得税的计算,根据输入的收入金额按不同税率计算税款;第三个程序接收两个正整数并以逆序打印它们的每一位。此外,还有一个判断素数的程序,用于找出一定范围内的所有素数。这些代码涵盖了基础的输入输出操作、数学计算以及数据处理。

1.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class test1 {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        // TODO Auto-generated method stub

        String inputString = br.readLine();

        int len = Integer.parseInt(inputString);

        int[][] a = new int[len + 1][len + 1];

        for(int i = 1;i <= len;i ++){

        for(int j = 1;j <= i; j++){

        a[i][j] = i * i - (j - 1);

        }

        for(int k = 1;k < i; k ++){

        a[k][i] = (i - 1) * (i - 1) + k;

        }

        }

        for(int i = 1;i <= len; i ++){

        for(int j = 1; j <= len; j ++){

            if(j==len)System.out.print(a[i][j]);

//每行最后一个数字

            else System.out.print(a[i][j] + " ");

//不是每行的最后一个数字,加空格

        }

        System.out.println();

        }

   

    }

}

2.

import java.util.Scanner;

public class test2{

    public static void main(String[] args) {

        Scanner in=new Scanner(System.in);

        double money,tax=0;

        money=in.nextDouble();

        if(money<=60000) tax=0;

        else if(money<=10000) tax=(money-60000)*0.03f;

        else if(money<=180000) tax=(100000-60000)*0.03f+(money-100000)*0.1f;

        else if(money<=300000) tax=(100000-60000)*0.03f+(180000-100000)*0.1f+(money-180000)*0.18f;

        else if(money<=480000) tax=(100000-60000)*0.03f+(180000-100000)*0.1f+(300000-180000)*0.18f+(money-300000)*0.25f;

        else if(money<=700000) tax=(100000-60000)*0.03f+(180000-100000)*0.1f+(300000-180000)*0.18f+(480000-300000)*0.25f+(money-480000)*0.3f;

        else if(money<=1000000) tax=(100000-60000)*0.03f+(180000-100000)*0.1f+(300000-180000)*0.18f+(480000-300000)*0.25f+(700000-480000)*0.3f+(money-700000)*0.35f;

        else if(money>1000000) tax=(100000-60000)*0.03f+(180000-100000)*0.1f+(300000-180000)*0.18f+(480000-300000)*0.25f+(700000-480000)*0.3f+(1000000-700000)*0.35f+(money-1000000)*0.45f;

        String result=String.format("%.2f",tax);

        System.out.println(result);   

        in.close();

    }

}

3.

import java.util.Scanner;

public class test3 {

    public static void main(String[] args)

    {

            System.out.println("请输入两个正整数:");

            Scanner in=new Scanner(System.in);

            String inputstr=in.nextLine();

            String[] strArray=inputstr.split(" ");

           

            int temp1=Integer.parseInt(strArray[0]);

            int temp2=Integer.parseInt(strArray[1]);

            int a[]=new int[5];

            int b[]=new int[5];

            int i=0,j=0,n=0,max;

            int count1=0,count2=0;//记录整数有多少位

             while (temp1!=0)

             {

                 a[i]=temp1%10;

                 temp1=temp1/10;

                 i=i+1;

                 count1++;

             }

             while (temp2!=0)

             {

                 b[j]=temp2%10;

                 temp2=temp2/10;

                 j=j+1;

                 count2++;

             }

            int k=count1+count2;

            int c[]=new int[10];

            for (i=0,j=count2-1;i<k;i++){

            if(i<count1

                System.out.print(a[count1-1-i]);

                if(j>=0) 

                System.out.print(b[j--]);

            }

          

        }

    }

4

import java.util.Scanner;

public class test4{

    public static void main(String[] args) {

        Scanner in=new Scanner(System.in);

        int N;

        N=in.nextInt();

        boolean[] isPrime = new boolean[N+1];

        for(int i=2;i<isPrime.length;i++) {

            isPrime[i]=true;

        }

        for(int i=2;i<isPrime.length;i++) {

            if(isPrime[i]==true) {

                for(int j=2;j*i<isPrime.length;j++) {

                    isPrime[j*i]=false;

                }

            }

        }

        for(int i=2;i<isPrime.length;i++) {

            if(isPrime[i]==true) System.out.print(i+" ");

        }

    }

}

5.

import java.util.Scanner;

import java.util.TreeSet;

public class test2{

    public static void main(String[] args) {

    Scanner in=new Scanner(System.in);

    String str=in.nextLine();

    in.close();

    String str1=sort(str);

    System.out.println(str1);

}

public static String sort(String str) {

    String[] array_str=str.split("");//将字符串转成字符数组

    TreeSet<String> treeSet=new TreeSet<String>();

    for(String s:array_str){//遍历字符数组

        treeSet.add(s);//将字符放入treeSet中,利用treeSet集合有序不重复的特性

    }

    String st ="";

    for(String s:treeSet){//遍历treeSet集合,在拼接到str字符串中

        st+=s;

        }

    return st;

    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值