java刷题(1-5)

第一题:在一个数组中找出三个数相加之和为0,且不重复的集合

 1 import java.lang.reflect.Array;
 2 import java.security.PublicKey;
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Arrays;
 6 public class AddThreenumber {
 7     public  static List> threenumber(int[] nums){
 8         Arrays.sort(nums);
 9         List> ls = new ArrayList>();
10         for (int i = 0; i < nums.length - 2; i++) {
11             if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {  // 跳过可能重复的答案
12 
13                 int l = i + 1, r = nums.length - 1, sum = 0 - nums[i];
14                 while (l < r) {
15                     if (nums[l] + nums[r] == sum) {
16                         ls.add(Arrays.asList(nums[i], nums[l], nums[r]));
17                         while (l < r && nums[l] == nums[l + 1]) l++;
18                         while (l < r && nums[r] == nums[r - 1]) r--;
19                         l++;
20                         r--;
21                     } else if (nums[l] + nums[r] < sum) {
22                         while (l < r && nums[l] == nums[l + 1]) l++;   // 跳过重复值
23                         l++;
24                     } else {
25                         while (l < r && nums[r] == nums[r - 1]) r--;
26                         r--;
27                     }
28                 }
29             }
30         }
31         System.out.println(ls);
32         return ls;
33     }
34     public static void main(String[] args){
35         int[] nums={-1, 0, 1, 2, -1, -4};
36         threenumber(nums);
37     }
38 }

第二题:输入一个字符串,计算其中的大小写字母及数字的个数

import java.util.*;
public class Count {

    public static void main(String[] args){
        System.out.println("请输入一串字符");
        Scanner scan= new Scanner(System.in);
        if(scan.hasNext()){
            String i =scan.next();
            int capital_numbercount=0,lower_numbercount=0,number=0;
            for(int j=0;j<=i.length()-1;j++){
                char s=i.charAt(j);
                if('a'<=s&&'z'>=s){
                    lower_numbercount=lower_numbercount+1;
                }
                else if('A'<=s&&'Z'>=s){
                    capital_numbercount=capital_numbercount+1;

                }
                else if('0'<=s&&'9'>=s){
                    number=number+1;
                }

            }
            System.out.printf("您所输入的字符串是:%s\n",i);
            System.out.printf("其中大写字母的个数为:%d个\n",capital_numbercount);
            System.out.printf("其中小写字母的个数为:%d个\n",lower_numbercount);
            System.out.printf("其中整数的个数为:%d个\n",number);

        }
        else{
            System.out.println("请重新输入");
        }

    }



}

第三题:使用类及方法实现计算器加减乘除基本功能

import java.util.*;
public class Calc {
    public  float add(float a,float b){
        return(a+b);
    }
    public  float subtraction(float a,float b){
        return(a-b);
    }
    public  float multiplication(float a,float b){
        return(a*b);
    }
    public  float division(float a,float b){
        return(a/b);
    }


    public static void main(String[] args){
        Calc calc=new Calc();
        System.out.println("请输入两个数字");
        Scanner scan=new Scanner(System.in);
        float a = scan.nextFloat();
        float b = scan.nextFloat();
        float add=calc.add(a,b);
        float Subtraction=calc.subtraction(a,b);
        float Multiplication=calc.multiplication(a,b);
        float Division = calc.division(a,b);
        System.out.printf("%.2f+%.2f=%.2f",a,b,add);
        System.out.printf("%.2f-%.2f=%.2f",a,b,Subtraction);
        System.out.printf("%.2f*%.2f=%.2f",a,b,Multiplication);
        System.out.printf("%.2f/%.2f=%.2f",a,b,Division);
    }
}

第四题:输入数字n,计算小于等于该数字n的所有阶乘之和

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int number = 0;
        int jc=1;
        int result=0;
        if (scanner.hasNextInt()) {
            number = scanner.nextInt();
            System.out.println("您输入的数字是:" + number);
            for(int i=1;i<=number;i++){
                for(int j=1;j<=i;j++){
                    jc *=j;
                }

                result=result+jc;
                jc=1;

            }
    System.out.printf("小于整数%d的所有正整数阶乘之和是:%d",number,result);
        }
        else{
                System.out.println("您输入的字符不合法");
            }
            scanner.close();
        }
    }

第五题:打印100-10000之间的回文数,并计算个数:

public class HuiWenshu {
    public static void main(String[] args){
        int count=0,number_1=0,number_2=0;
        for(int i =100;i<=10000;i++){
            if(i>=100&&i<=1000){
                String s=Integer.toString(i);
                char c=s.charAt(0);
                char b=s.charAt(2);
                if(c==b){
                    System.out.println(i);
                    number_1=number_1+1;
                }
            }
            else if(i>1000&&i<=10000){
                String s=Integer.toString(i);
                char first=s.charAt(0);
                char second=s.charAt(1);
                char third=s.charAt(2);
                char fourth=s.charAt(3);
                if(first==fourth){
                    if(second==third){
                        System.out.println(i);
                        number_2=number_2+1;
                    }

                }

            }

        }

    count=number_1+number_2;
        System.out.printf("100-10000之间的回文数一共有%d个",count);
    }



}

 

你可能感兴趣的:(java刷题(1-5))