杭电oj2027、2028题详解(Java版)

2027.统计元音

在这里插入图片描述

实现代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        while(s.hasNext()) {
            int n = s.nextInt();
            String w = s.nextLine();
            int q = 0;
            while(n--> 0) {
                w = s.nextLine();
                int a = 0,b = 0,c = 0,d = 0,e = 0;
                for (int i = 0; i < w.length(); i++) {
                    if(w.charAt(i)=='a') {
                        a=a+1;
                    }else if(w.charAt(i)=='e') {
                        b=b+1;
                    }else if(w.charAt(i)=='i') {
                        c=c+1;
                    }else if(w.charAt(i)=='o') {
                        d=d+1;
                    }else if(w.charAt(i)=='u') {
                        e=e+1;
                    }
                }
                if(q != 0) {
                	System.out.println();
                }
                System.out.println("a:"+a);
                System.out.println("e:"+b);
                System.out.println("i:"+c);
                System.out.println("o:"+d);
                System.out.println("u:"+e);
                q++;
            }
        }
    }
}

注意:每两个输出样例之间有空行

2028.Lowest Common Multiple Plus在这里插入图片描述

实现代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();          //正整数个数
            int[] arr = new int[n];        //存放正整数
            int count = 0;                 //记录能整除数的个数
            for (int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
            }
            Arrays.sort(arr);              //sort方法:实现对数组的排序
            int max = arr[n-1];
            Star:for (int i = max;; i++) {
                count = 0;
                for (int j = 0; j < n; j++) {
                    if(i % arr[j] == 0) {
                        count++;
                    }else{
                    	break;
                    }
                }
                if(count == n) {
                    System.out.println(i);
                    break Star;
                }
            }
        }
    }
}

如果只写break,就只能跳出一层for循环;如果想跳出两层for循环的话,在外层for循环前加上一个标识,然后用“break 标识;”的写法就可以跳出外层for循环。
标识不一定要写Star,outer等其他非关键字都可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Weirdo!

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值