栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

刷题的狂欢-----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 

第二题

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);
    }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号