文章

算法第一章习题记录

2020.9.28 ・ 共 761 字,您可能需要 2 分钟阅读

Tags: 代码


1 2 3

//1.1.3
import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        if (a == b && b == c)
        {
            System.out.println("equal");
        }
        else System.out.println("not equal");
    }
}
//1.1.5
import java.util.Scanner;
public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();
        if ((x >= 0 && x <= 1) && (y >= 0 && y <= 1))
        {
            System.out.println(true);
        }
        else
            System.out.println(false);
    }
}
//1.1.9
import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        String string = "";
        while (N > 0)
        {
            int r = N % 2;
            string = r + string;
            N /= 2;
        }
        System.out.println(string);

    }
}
//1.1.11
public class Test
{
    public static void main(String[] args)
    {
        boolean[][] a = {{true, true, false},{false, true, false}};
        for (int i = 0; i < a.length; ++i)
        {
            for (int j = 0; j < a[i].length; ++j)
            {
                if (a[i][j] == true)
                    System.out.println("第"+i+"行,"+"第"+j+"列:"+"*");
                else
                    System.out.println("第"+i+"行,"+"第"+j+"列:"+" ");
            }
        }
    }
}
//1.1.13
import java.util.Arrays;

public class Test
{
    public static void main(String[] args)
    {
        int M = 3;
        int N = 2;
        int[][] o = {{2,3}, {1,2}, {4,5}};
        int[][] n = new int[N][M];
        for (int i = 0; i < M; ++i)
        {
            for (int j = 0; j < N; ++j)
            {
                n[j][i] = o[i][j];
            }
        }
        for (int a = 0; a < 2; ++a)
        {
            System.out.println(Arrays.toString(n[a]));
        }
    }
}
//1.1.14
public class Test
{
    public static void main(String[] args)
    {
        int a = lg(7);
        System.out.println(a);
    }
    public static int lg(int N)
    {
        if (N >= 1)
        {
            int exp = 1;
            int x = 0;
            while (exp <= N)
            {
                exp *= 2;
                x++;
            }
            return x - 1;
        }
        return -1;

    }
}
//1.1.15
import java.util.Arrays;

public class Test
{
    public static void main(String[] args)
    {
        int[] a = {1, 2, 1, 3, 1};
        System.out.println(Arrays.toString(Test.histogram(a, 3)));
    }

    public static int[] histogram(int[] a, int m)
    {
        int[] b = new int[m];

        for (int i = 0; i < m; ++i)
        {
            int tick = 0;
            for (int num: a)
            {
                if (num == i + 1)
                {
                    ++tick;
                }
                b[i] = tick;
            }
        }
        return b;
    }
}
//1.1.20
public class Test
{
    public static void main(String[] args)
    {
        System.out.println(cal(3));
        System.out.println(Math.log(cal(3)));

    }

    public static int cal(int N)
    {
        if (N <= 1)
        {
            return 1;
        }
        return cal(N - 1) * N;
    }
}
//1.1.21
import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {
        String [] array = new String[100];
        int i = 0;
        Scanner input = new Scanner(System.in);
        while (input.hasNext())
        {
            array[i] = input.nextLine();
            if(array[i].equals("end")){
                break;
            }
            i++;
        }
        System.out.println();
        for (int j = 0; j < i; ++j)
        {
            String [] strings = array[j].split(" ");
            String string1 = strings[0];
            int x = Integer.parseInt(strings[1]);
            int y = Integer.parseInt(strings[2]);
            System.out.printf("%s  %d  %d  %.3f \n", string1, x, y, (double) x / y);
        }
    }
}
//1.1.24
import java.util.Scanner;

public class Test
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        System.out.println(fun(a, b));
    }
    public static int fun(int a, int b)
    {
        if (a % b == 0)
        {
            return b;
        }
        System.out.print(a+" ");
        System.out.println(b);
        return fun(b, a % b);
    }
}