蓝桥杯-----基础训练--报时助手、芯片测试(Java)解法

来源:互联网 发布:126邮箱imap端口 编辑:程序博客网 时间:2024/06/01 10:27

一、报时助手

    问题描述:

给定当前的时间,请用英文的读法将它读出来。
  时间用时h和分m表示,在英文的读法中,读一个时间的方法是:
  如果m为0,则将时读出来,然后加上“o'clock”,如3:00读作“three o'clock”。
  如果m不为0,则将时读出来,然后将分读出来,如5:30读作“five thirty”。
  时和分的读法使用的是英文数字的读法,其中0~20读作:
  0:zero, 1: one, 2:two, 3:three, 4:four, 5:five, 6:six, 7:seven, 8:eight, 9:nine, 10:ten, 11:eleven, 12:twelve, 13:thirteen, 14:fourteen, 15:fifteen, 16:sixteen, 17:seventeen, 18:eighteen, 19:nineteen, 20:twenty。
  30读作thirty,40读作forty,50读作fifty。
  对于大于20小于60的数字,首先读整十的数,然后再加上个位数。如31首先读30再加1的读法,读作“thirty one”。
  按上面的规则21:54读作“twenty one fifty four”,9:07读作“nine seven”,0:15读作“zero fifteen”。
输入格式
  输入包含两个非负整数h和m,表示时间的时和分。非零的数字前没有前导0。h小于24,m小于60。
输出格式
  输出时间时刻的英文。
样例输入
0 15
样例输出
zero fifteen

二、解题思想:

      这道题的输入为两个元素,所以需要对这两个元素分别处理。涉及的知识无非是数字与字符串的对应关系,而swicth-case语句可以很好的解决这个问题。首先是小时元素,它的数字出现范围只能是0-23,所哟利用swicth-case语句进行数字对应字符串的判段与相关字符串的输出。最后是分,由于分的数值区间是0-59,故当其大于了19后,其对应的读法就变了,需要对十位与个位进行分别的判断与字符串的输出。

下面给出解题代码:

import java.util.Scanner;public class 报时助手 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int h = sc.nextInt();int m = sc.nextInt();sc.close();if(m==0){switch(h){case 0:System.out.print("zero");break;case 1:System.out.print("one");break;case 2:System.out.print("two");break;case 3:System.out.print("three");break;case 4:System.out.print("four");break;case 5:System.out.print("five");break;case 6:System.out.print("six");break;case 7:System.out.print("seven");break;case 8:System.out.print("eight");break;case 9:System.out.print("nine");break; case 10:System.out.print("ten");break;case 11:System.out.print("eleven");break;case 12:System.out.print("twelve");break;case 13:System.out.print("thirteen");break;case 14:System.out.print("fourteen");break;case 15:System.out.print("fifteen");break;case 16:System.out.print("sixteem");break;case 17:System.out.print("seventeen");break;case 18:System.out.print("eighteen");break;case 19:System.out.print("nineteen");break;case 20:System.out.print("twenty");break;case 21:System.out.print("twenty one");break;case 22:System.out.print("twenty two");break;case 23:System.out.print("twenty three");break;}System.out.print(" o'clock");}else{switch(h){case 0:System.out.print("zero");break;case 1:System.out.print("one");break;case 2:System.out.print("two");break;case 3:System.out.print("three");break;case 4:System.out.print("four");break;case 5:System.out.print("five");break;case 6:System.out.print("six");break;case 7:System.out.print("seven");break;case 8:System.out.print("eight");break;case 9:System.out.print("nine");break; case 10:System.out.print("ten");break;case 11:System.out.print("eleven");break;case 12:System.out.print("twelve");break;case 13:System.out.print("thirteen");break;case 14:System.out.print("fourteen");break;case 15:System.out.print("fifteen");break;case 16:System.out.print("sixteem");break;case 17:System.out.print("seventeen");break;case 18:System.out.print("eighteen");break;case 19:System.out.print("nineteen");break;case 20:System.out.print("twenty");break;case 21:System.out.print("twenty one");break;case 22:System.out.print("twenty two");break;case 23:System.out.print("twenty three");break;}if(m<=20){switch(m){case 0:System.out.print(" zero");break;case 1:System.out.print(" one");break;case 2:System.out.print(" two");break;case 3:System.out.print(" three");break;case 4:System.out.print(" four");break;case 5:System.out.print(" five");break;case 6:System.out.print(" six");break;case 7:System.out.print(" seven");break;case 8:System.out.print(" eight");break;case 9:System.out.print(" nine");break;case 10:System.out.print(" ten");break;case 11:System.out.print(" eleven");break;case 12:System.out.print(" twelve");break;case 13:System.out.print(" thirteen");break;case 14:System.out.print(" fourteen");break;case 15:System.out.print(" fifteen");break;case 16:System.out.print(" sixteem");break;case 17:System.out.print(" seventeen");break;case 18:System.out.print(" eighteen");break;case 19:System.out.print(" nineteen");break;case 20:System.out.print(" twenty");break;}}else{int temp1 = m/10;int temp2 = m%10;switch(temp1){case 2:System.out.print(" twenty");break;case 3:System.out.print(" thirty");break;case 4:System.out.print(" forty");break;case 5:System.out.print(" fifty");break;}switch(temp2){case 0:System.out.print(" zero");break;case 1:System.out.print(" one");break;case 2:System.out.print(" two");break;case 3:System.out.print(" three");break;case 4:System.out.print(" four");break;case 5:System.out.print(" five");break;case 6:System.out.print(" six");break;case 7:System.out.print(" seven");break;case 8:System.out.print(" eight");break;case 9:System.out.print(" nine");break;}}}}}



二、芯片测试

     问题描述:有n(2≤n≤20)块芯片,有好有坏,已知好芯片比坏芯片多。
每个芯片都能用来测试其他芯片。用好芯片测试其他芯片时,能正确给出被测试芯片是好还是坏。而用坏芯片测试其他芯片时,

三、解题思想

     根据题目的要求可以知道共有两个关键的问题点:1)由于好芯片的数量要多于坏芯片的数量

                                                                                    2)说真话的一定是好芯片,说假话的一定是假芯片。

所以可以总结出:一片芯片被人说是好的个数大于总数的一半则其一定是真的。

下面给出解题代码:

import java.util.Scanner;public class 芯片测试 {public static void main(String[] args) {Scanner sc = new Scanner(System.in); int n = sc.nextInt();int s[][] = new int[n][n];int sum = 0;for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){s[i][j] = sc.nextInt();}}for(int i = 0;i < n;i++){for(int j = 0;j < n;j++){sum += s[j][i];}if(sum > n/2){System.out.print((i+1)+" ");}sum = 0;}      }}

这道题逻辑思想强一点的人很好理解,弱一点的可以将数据带入尝试验证即可理解。

原创粉丝点击