java数学简单实例

本文探讨了如何通过Java实现找出一组正整数中的最大值及其最大位,同时介绍了如何对数字进行重新排序和判断是否为'相邻位不重复数'。算法涉及输入处理、数值比较和字符数组操作,适合学习者理解基本的编程技巧。
摘要由CSDN通过智能技术生成

找出一组正整数中的最大值及其最大位

import java.util.Scanner;
public class Main { 
   public static void main (String[] args) { 
      Scanner sc = new Scanner(System.in);
        int max=0;
        while(sc.hasNextInt()){ 
           int num=sc.nextInt();
            if(max<num){
              max=num; 
           }
    }
    System.out.println( "最大值 "+ max);
    int max_n=max%10;
    while(max!=0){ 
       if(max_n<max%10){
             max_n=max%10;
        }max/=10;
    }    System.out.print("最大位 "+max_n);    
}
}

将一整个正整数的所有位重新排序,组成一个最大数

import java.util.Scanner;
public class Main{
    public static void main(String[] args ){
        Scanner sc = new Scanner(System.in);
        String imput =sc.next();
        String arr[] = imput.split("");
        int[] a = new int[arr.length];
        sc.close();
        for(int i = 0; i<a.length; i++) {
        	a[i] = Integer.parseInt(arr[i]);
        }
        int temp;
        for(int i = 0; i<a.length-1; i++){
            for(int j = i+1; j<a.length; j++){
            if(a[i]<a[j]){
                temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
            }
        }
        for(int i = 0; i<a.length; i++){
            System.out.print(a[i]);
        }
        }
    }

判断一个数是否是“相邻位不重复数”

import java.util.Scanner;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
   int num = sc.nextInt();
   int count=0;
   int n = num;
   while(n !=0){
       count ++;
       n /= 10;
   }
   int abc[] = new int[count];
   for(int i=0; i<count; i++) {
   	abc[i] = num%10;
   	num /= 10;
   }
   boolean flag = false;
   for(int j=0; j<abc.length-1; j++) {
   	if(abc[j] == abc[j+1]) {
   	flag = true;
   	break;
   	}	  
   }
   if(!flag) {
   	System.out.println(true);
   }
   else {
   	System.out.println(false);
   }
  }
}

学生成绩类排序

import java.util.TreeSet;
import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        TreeSet<Student> stus = new TreeSet<>();
        while(sc.hasNext()){
            String name = sc.next();
            String id = sc.next();
            int chinese = sc.nextInt();
            int math = sc.nextInt();
            int english = sc.nextInt();            
            
            
           stus.add(new Student(name,id,chinese,math,english));
        }
        sc.close();
        for(Student stu :stus) {
        	System.out.println(stu);
        }
        
    }
}
class Student implements Comparable<Student>{
    private String name;
    private String id;
    private int chinese;
    private int math;
    private int english;
    
    public Student() {
}
    public Student(String name, String id,int chinese ,int math, int english) {
        this.name = name;
        this.id = id;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
}
    public int getSum(){
        return chinese+math+english;
    }
    public int getChinese() {
    	return chinese;
    }
    public int getMath() {
    	return math;
    }
    public int getEnglish() {
    	return english;
    }
    public String getId() {
    	return id;
    }
    
    public String toString(){
        return name+ ", "+id+", "+getSum()+", "+chinese+", "
            +math+", "+english;
    }
    
    public int compareTo(Student o) {
    	Student stu = (Student) o;
        int num1 = stu.getSum() - this.getSum();
        int num2 = (num1==0? stu.getChinese() - this.getChinese()   : num1);
        int num3 = (num2==0? stu.getMath() - this.getMath()         : num2);
        int num4 = (num3==0? stu.getEnglish() - this.getEnglish()   : num3);
        int num5 = (num4==0? this.id.compareTo(stu.getId())        : num4);	
return num5;
}
}

使用集合存储和遍历

import java.util.ArrayList;
import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while(sc.hasNext()){
            String name = sc.next();
            String id = sc.next();
            int chinese = sc.nextInt();
            int math = sc.nextInt();
            int english = sc.nextInt();
            
            Student stu = new Student(name,id,chinese,math,english); 
            list.add(stu);
        }
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
}
class Student {
    private String name;
    private String id;
    private int chinese;
    private int math;
    private int english;
    
    public Student() {
}
    public Student(String name, String id,int chinese ,int math, int english) {
        this.name = name;
        this.id = id;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
}
    public int getSum(){
        return chinese+math+english;
    }
    public String toString(){
        return name+ ", "+id+", "+getSum()+", "+chinese+", "
            +math+", "+english;
    }
}

找一组数据中最大的"相邻位不重复数"

import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        int max = 0;
        lable:while(sc.hasNextInt()){
            int num = sc.nextInt();
            char [] ch = String.valueOf(num).toCharArray();
            for(int i=0;i<ch.length-1;i++)
                if(ch[i] == ch[i+1]){
                    continue lable;
                }
                if(max<num){
                    max = num;
                }
        }
        System.out.println(max);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值