算法入门——洛谷第一章_顺序结构(JAVA)

文章目录

B2002 Hello,World!

B2025 输出字符菱形

P1000 超级玛丽游戏

 P1001 A+B Problem

B2005 字符三角形

P5703 【深基2.例5】苹果采购

P5704 【深基2.例6】字母转换

P5705 【深基2.例7】数字反转

P5706 【深基2.例8】再分肥宅水

P5708 【深基2.习2】三角形面积

P5707 【深基2.例12】上学迟到

B2029 大象喝水

P1425 小鱼的游泳时间

P1421 小玉买文具

P3954 [NOIP2017 普及组] 成绩


B2002 Hello,World!

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello,World!");
    }
}

属于是梦开始的地方了(=^・^=)


B2025 输出字符菱形

public class Main {

    public static void main(String[] args) {
        System.out.print("  *\n" +
                " ***\n" +
                "*****\n" +
                " ***\n" +
                "  *");
    }
}

这个完全可以不用自己一行一行敲,直接把他的样例结果复制一下,然后粘贴到输出语句里就可以了,一定要是用双引号括住再复制喵~

System.out.println("复制到这里面喵");

P1000 超级玛丽游戏

public class Main {
    public static void main(String[] args) {
        System.out.println("                ********\n" +
                "               ************\n" +
                "               ####....#.\n" +
                "             #..###.....##....\n" +
                "             ###.......######              ###            ###\n" +
                "                ...........               #...#          #...#\n" +
                "               ##*#######                 #.#.#          #.#.#\n" +
                "            ####*******######             #.#.#          #.#.#\n" +
                "           ...#***.****.*###....          #...#          #...#\n" +
                "           ....**********##.....           ###            ###\n" +
                "           ....****    *****....\n" +
                "             ####        ####\n" +
                "           ######        ######\n" +
                "##############################################################\n" +
                "#...#......#.##...#......#.##...#......#.##------------------#\n" +
                "###########################################------------------#\n" +
                "#..#....#....##..#....#....##..#....#....#####################\n" +
                "##########################################    #----------#\n" +
                "#.....#......##.....#......##.....#......#    #----------#\n" +
                "##########################################    #----------#\n" +
                "#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#\n" +
                "##########################################    ############");
    }
}

 用跟上题一样的方法就可以了喵~


 P1001 A+B Problem

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = a + b;
        System.out.println(c);
    }
}

常见的警告/错误:

Scanner 无法解析为类型/无法解析符号'Scanner'

        出现这个错误是因为没有导入Scanner 包,在代码最上方写上:import java.util.Scanner;即可

Resource leak: 'sc' is never closed:

        这个警告的意思是sc未关闭,在System.in 第一次声明时会打开InputStream(输入流),不关闭就会一直占用部分内存,但不管它也可以,不会影响使用,可以使用:sc.close();来关闭这个流。(这个问题常见于eclipse,使用idea不会报这个警告)


B2005 字符三角形

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char a = sc.next().charAt(0);//无法直接获取字符,故先获取字符串,再取字符串的第一个字符
        System.out.println("  "+a);
        System.out.println(" "+a+a+a);
        System.out.println(""+a+a+a+a+a);
    }
}

可用+来拼接字符串,但是加号两边必须至少有一个是字符串喵~

不然会把字符型数据隐式转换成整形喵~


P5703 【深基2.例5】苹果采购

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = a*b;
        System.out.println(c);
    }
}

P5704 【深基2.例6】字母转换

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char a = sc.next().charAt(0);
        int b = a - 32;
        char c = (char)b;
        System.out.println(c);
    }
}

 tips:

在ASCII(美国信息交换标准代码)中,小写字母比大写字母大32喵~

a —— 97    A —— 65


P5705 【深基2.例7】数字反转

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();
        String b = "";
        for(int i = a.length() - 1;i >= 0;i--){
            b += a.charAt(i);
        }
        float c = Float.parseFloat(b);
        System.out.printf("%.3f",c);
    }
}

Float.parseFloat(String str);可将字符串转换为float型

System.out.printf("%.3f",c); ————"%.3f" 为保留三位小数的浮点型


P5706 【深基2.例8】再分肥宅水

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float a = sc.nextFloat();
        int b = sc.nextInt();
        float c = a/b;
        int d = b*2;
        System.out.printf("%.3f\n",c);
        System.out.printf("%d",d);
    }
}

P5708 【深基2.习2】三角形面积

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a = sc.nextFloat();
        double b = sc.nextFloat();
        double c = sc.nextFloat();
        double p = (a+b+c)/2;
        double s = Math.sqrt(p*(p-a)*(p-b)*(p-c));
        s = Math.round(s*10);//无法对十分位直接四舍五入,故先将其抬升为整数
        s = s/10;//四舍五入完成后再将他降回至小数
        System.out.printf("%.1f",s);
    }
}

Math.sqrt() ——用于表示开平方,也可用 Math.pow(double a ,1/2.0) 表示

Math.round() —— 四舍五入为整数


P5707 【深基2.例12】上学迟到

 

import java.util.Scanner;
/*
 24小时制下,计算时间需要考虑是否小于零,若小于,加上24,大于等于时不用改变
 此题还可以使用日期计算函数
*/
public class main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double s,v,t;
        int m,h;
        int hh,mm;
        s = sc.nextInt();
        v = sc.nextInt();
        t = Math.ceil(s/v) + 10;//多花费的十分钟
        m = (int)t%60;
        h = (int)t/60;
        hh = (m==0)?8 - h:8-h-1;//如果需要提前的时间包括分钟,就需要去借一小时
        mm = (m==0)?0:60 - m;
        hh = (hh < 0)?24+hh:hh;
        System.out.printf("%02d:%02d",hh,mm);
    }
}

tips:

三目运算符

        判断语句 ? 为真返回 :为假返回

System.out.printf("%02d:%02d",hh,mm); —— "%02d"输出的整形占两位,不足两位在左补零

第二种方法:(JDK8以后,包括JDK8,不推荐这种方法,推荐使用JDK8新的时间API)

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int s = scanner.nextInt();
        int y = scanner.nextInt();
        Calendar date = Calendar.getInstance();
        date.set(2021, Calendar.MAY, 5, 8, 0, 0);
        date.add(Calendar.MINUTE, -10);
        date.add(Calendar.MINUTE, s % y == 0 ? -(s / y) : -(s / y) - 1);
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        System.out.println(format.format(date.getTime()));
    }
}

部分API示例:

Calendar c = Calendar.getInstance();//创建日期对象

c.add(Calendar.YEAR,3)//c的日期加三年

c.add(Calendar.YEAR,-3)//c的日期减三年

c.set(int,int,int,int,int,int)//设置默认时间 -----

SimpleDateFormat //对日期格式化


B2029 大象喝水

 

import java.util.Calendar;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final double PI = 3.14;
        int h = sc.nextInt();
        int r = sc.nextInt();
        double s = PI*r*r;
        double v = s * h;
        int n = (int) Math.ceil((20*1000)/v);
        System.out.println(n);
    }
}

Math.ceil() —— 向上取整

体积计算 : 底面积 * 高


P1425 小鱼的游泳时间

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();
        int t1 = a*60+b;//将时间都转化成分钟计算
        int t2 = c*60+d;
        int t = t2 - t1;
        int h = t/60;
        int m = t%60;
        System.out.printf("%d %d",h,m);
    }
}

P1421 小玉买文具

 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c,d,n;
        c = a*10+b;//跟上题思路一样,都转化成最小单位计算
        d = 19;
        n = (int)Math.floor(c/d);
        System.out.println(n);
    }
}

P3954 [NOIP2017 普及组] 成绩

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int s = (int)(a*0.2+b*0.3+c*0.5);
        System.out.println(s);
    }
}

(int)强制类型转换,将其他类型转化成int型


欢迎大家来评论区发表自己的观点~

下次见喵~

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值