2033.人见人爱A+B
解题思路:
A、B的时分秒分别相加,要注意时间的格式,分、秒的范围是0~59,所以相加之后判断分、秒位置上的数字是否>=60,>=60须得向前进一位。还有一点需要注意,应该先判断秒位置,因为可能会出现分、秒位置上的数字都是59的情况。
实现代码:
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();
while(n-->0) {
//接收A的时、分、秒
int h1=s.nextInt();
int m1=s.nextInt();
int s1=s.nextInt();
//接收B的时、分、秒
int h2=s.nextInt();
int m2=s.nextInt();
int s2=s.nextInt();
int H=h1+h2;
int M=m1+m2;
int S=s1+s2;
if(S>=60) {
M=M+1;
S=S-60;
}
if(M>=60) {
H=H+1;
M=M-60;
}
System.out.println(H+" "+M+" "+S);
}
}
}
}
2034.人见人爱A+B
解题思路:
集合A - 集合B = {属于集合A但不属于集合B的元素};
首先判断符合条件的元素有哪些:利用计数的方法,与集合B中的元素一一比较,如果不等,则让w+1,比较完后判断w是否等于集合B的长度,二者相等说明,该元素符合条件,输出;否则让S+1,最后判断既在集合A中又在集合B中的元素数量是否等于集合A的长度,二者相等说明集合A中的所有元素,在集合B中都能找到,此时输出“NULL”。
实现代码:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
while(s.hasNext()) {
int m = s.nextInt();
int n = s.nextInt();
if(m==0 && n==0) {
break;
}else {
int []a = new int[m]; //集合A
int []b = new int[n]; //集合B
for (int i = 0; i < m; i++) {
a[i]=s.nextInt();
}
for (int i = 0; i < n; i++) {
b[i]=s.nextInt();
}
Arrays.sort(a);
int S = 0; //记录既在集合A中,又在集合B中的元素数量
for (int i = 0; i < m; i++) {
int w = 0; //记录集合A中的元素与集合B中元素不相等的元素数量
for (int j = 0; j < n; j++) {
if(a[i] != b[j]) {
w++;
}
}
if(w == n) { //集合A中的该元素不等于集合B中的任何元素
System.out.print(a[i]+" ");
}else if(w>0 && w<n){
S++;
}
}
if(S == m) {
System.out.print("NULL");
}
System.out.println();
}
}
}
}