刷题的狂欢-----JAVA每日三练-----第十五天

努力刷题,每日三题,题目来源于 “牛客网”。

第一题
在这里插入图片描述

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int i,j,k;
            while(scanner.hasNext()){
            int Line = scanner.nextInt();    
            System.out.println("*");
            System.out.println("* *"); //因为从3开始,前两行直接打印
            for(i=3; i<Line; i++){
                System.out.print("* ");//先打印最左边的*,后面有空格
                for(j=1; j<=i-2; j++){
                    System.out.print("  ");//打印中间的空格
                }
                System.out.println("*");
            }
            for(k=1; k<=Line; k++){
                System.out.print("* ");//最后一行单独打印
            }
                System.out.println();
        }
    }
}

第二题
在这里插入图片描述

import java.util.*;

public class Main{

    static class Test{ 
//本题唯一注意的地方是使用Long类型,因为使用int ,会超过最大范围,当执行程序里的x*y的时候,使用int会越界。
        public long work(long x, long y){
            long c;
            c = x%y;
            while(c!=0){
                x = y;
                y = c;
                c = x%y;
            }
            return y;
        }
    }

    static class Test_1{
        Test test = new Test();
        public long work_1(long x, long y){
            long temp;
            temp = (x*y)/(test.work(x,y));
            return temp;
        }
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        long x = scanner.nextLong();
        long y = scanner.nextLong();
        Test test = new Test();
        long a = test.work(x,y);
        Test_1 test_1 = new Test_1();
        long b = test_1.work_1(x,y);
        long sum = a + b;
        System.out.println(sum);
    }
}

第三题
在这里插入图片描述

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int number;
        int i, j=0, sum=0;
        int[] array =new int[9];
        number = scanner.nextInt();
        while(number>0){//判定条件number>0,改成 >=会导致数组越界
            i = number%10;
            number = number/10;
            if(i%2==0) {
                array[j] = 0;
            } else{
                array[j] = 1;
            }
            sum = (int) (sum + array[j]*Math.pow(10,j));
            j++;
        }
        System.out.println(sum);
    }
}

代码并非最优解,该兴趣的话可以去官网寻找最优解,有问题欢迎评论区留言。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的xiaolang

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值