zoukankan      html  css  js  c++  java
  • 2018/12/05 PAT刷题 L1-019 谁先倒 Java

    这道题目非常的可以反映我的现状, 阅读的能力太差了, 将问题转化成代码的能力太差, 补救的办法就是多写题目.

    这道题目有很多值得玩味的地方, 首先使用类来保存格式严格的一系列的数据, 是一个非常实用的方法. 其次使用数组保存类的数据. 从而将多次输入的结构相同的数据以数组的形式保存下来, 调用数据也十分的方便, 直接使用数组元素.属性的方法就可以调用数组中保存的类元素的属性.

    代码如下:

     1 import java.util.Scanner;
     2 
     3 public class Main {
     4     public static void main(String[] args) {
     5         
     6         Scanner sc = new Scanner(System.in);
     7         int a = sc.nextInt();
     8         int b = sc.nextInt();
     9 
    10         int n = sc.nextInt();
    11         Drinking[] array = new Drinking[n];
    12         for (int i = 0; i < n; i++) {
    13             array[i] = new Drinking(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
    14             // 将每次喝酒的情况保存到类中, 再将类的信息保存到数组中. 
    15         }
    16         
    17         sc.close();
    18         int ah = 0; // A喝的次数
    19         int bh = 0; // B喝的次数
    20         for (int i = 0; i < n; i++) {
    21             if (array[i].sum == array[i].b2 && array[i].sum != array[i].a2) {
    22                 b--;
    23                 bh++;
    24             }
    25             if (array[i].sum == array[i].a2 && array[i].sum != array[i].b2) {
    26                 a--;
    27                 ah++;
    28             }
    29 
    30             if (a < 0 || b < 0) { // 只要有一个人和倒了, 就退出循环. 
    31                 break;
    32             }
    33         }
    34 
    35         if (a < 0) {
    36             System.out.println("A");
    37             System.out.println(bh);  // 输出没有喝倒的人和了多少酒
    38         }
    39         if (b < 0) {
    40             System.out.println("B");
    41             System.out.println(ah);  // 输出没有喝倒的人和了多少酒
    42         }
    43     }
    44 }
    45 
    46 class Drinking {
    47     int a1;
    48     int a2;
    49     int b1;
    50     int b2;
    51 
    52     int sum;
    53 
    54     Drinking(int a1, int a2, int b1, int b2) {  // 使用类的结构保存每一次的A和B划拳的情况
    55         this.a1 = a1;
    56         this.a2 = a2;
    57         this.b1 = b1;
    58         this.b2 = b2;
    59         this.sum = a1 + b1;
    60     }
    61 }
  • 相关阅读:
    使用viewpager实现QQ界面切换效果
    实例教程十一:访问通信录中的联系人和添加联系人
    实例教程十四:视频刻录(调用前置摄像头)
    实例教程十二:为应用添加多个Activity与参数传递
    【30篇突击 android】源码统计八
    GIF动画 汇总帖子+实例
    Android ProgressBar 几乎全部的用法
    实例教程十三:拍照
    android NDK 知识汇总
    【原创】oracle中大数据量更新的测试
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10070829.html
Copyright © 2011-2022 走看看