文章

Practice1

2020.9.29 ・ 共 1221 字,您可能需要 3 分钟阅读

Tags: 代码

2020/10/4 待交 QQ图片20200929164953

//9
import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] arr = new int[10];
        for (int i = 0; i < 10; ++i)
        {
            arr[i] = input.nextInt();
        }
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < 10; ++i)
        {
            if (arr[i] > max)
                max = arr[i];
            if (arr[i] < min)
                min = arr[i];
        }
        System.out.printf("最大值为%d,最小值为%d", max, min);
    }
}
//10
import java.util.Scanner;

public class Test2
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        char[] c = str.toCharArray();
        char[] n = new char[c.length - 1];
        int i = 0;
        for (char s: c)
        {
            if (s == '#')
                break;

            n[i] = s;
            i++;
        }
        int min = n[0];
        for (int k = 1; k < n.length; ++k)
        {
            if (n[k] < min)
                min = n[k];
        }
        System.out.println(min);

    }
}
//12
import java.util.Scanner;

public class Test3
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        if (num < 1 || num > 12)
        {
            System.out.println("输入错误,重新输入");
            System.exit(-1);
        }
        switch (num)
        {
            case 1:
                System.out.println("一月");
                break;
            case 2:
                System.out.println("二月");
                break;
            case 3:
                System.out.println("三月");
                break;
            case 4:
                System.out.println("四月");
                break;
            case 5:
                System.out.println("五月");
                break;
            case 6:
                System.out.println("六月");
                break;
            case 7:
                System.out.println("七月");
                break;
            case 8:
                System.out.println("八月");
                break;
            case 9:
                System.out.println("九月");
                break;
            case 10:
                System.out.println("十月");
                break;
            case 11:
                System.out.println("十一月");
                break;
            case 12:
                System.out.println("十二月");
                break;
        }
    }
}
//18
public class Test4
{
    public static void main(String[] args)
    {
        int[] a = {1, 2, 4, 5, 1};
        int max = a[0];
        int min = a[0];
        int plus = 0;
        for (int i = 0; i < a.length; ++i)
        {
            if (a[i] < min)
                min = a[i];
            if (a[i] > max)
                max = a[i];
            plus += a[i];
        }
        double avg = plus / a.length;
        System.out.printf("最大值%d 最小值%d 平均值%f 总和%d", max, min, avg, plus);
    }
}

//19
import java.util.ArrayList;
import java.util.List;

public class Test5
{
    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 12; ++i)
        {
            list.add(i);
        }
        int n = -1;
        while (list.size() > 1)
        {
            n = (n + 5) % list.size();
            list.remove(n);
            --n;
        }
        System.out.println(list.get(0));
    }
}

//20
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test6
{
    public static void main(String[] args)
    {
        int[] a = new int[99];
        for (int i = 2; i < 101; ++i)
        {
            a[i - 2] = i;
        }
        for (int i = 0; i < 99; ++i)
        {
            if (a[i] % 2 == 0 && a[i] != 2)
                a[i] = 0;
            if (a[i] % 3 == 0 && a[i] != 3)
                a[i] = 0;
            if (a[i] % 5 == 0 && a[i] != 5)
                a[i] = 0;
            if (a[i] % 7 == 0 && a[i] != 7)
                a[i] = 0;
        }
        for (int i: a)
        {
            if (i != 0)
                System.out.println(i);
        }
    }
}

QQ图片20200929165330

// 第一题
public class Test7
{
    public static void main(String[] args)
    {
        int tick = 0;
        for (int i = 101; i < 201; ++i)
        {
            boolean a = true;
            for (int j = 2; j < i; ++j)
            {
                if (i % j == 0)
                {
                    a = false;
                }
            }
            if (a)
            {
                tick += 1;
                System.out.println(i);
            }
        }
        System.out.println("素数个数:"+tick);
    }
}

// 第二题
import java.util.Scanner;

public class Test8
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String str = input.next();
        char[] c =str.toCharArray();
        for (char s: c)
        {
            if (s == '#')
                break;
            System.out.print(s);
        }
    }
}
//第三题 ->输出所有三位水仙花数
public class Test9
{
    public static void main(String[] args)
    {
        for (int i = 100; i < 1000; i++)
        {
            int a = i / 100;
            int b = (i / 10) % 10;
            int c = i % 10;
            if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i)
                System.out.println(i);
        }
    }
}