201903-1 小中大
简单模拟,不解释
import java.util.Scanner;
public class Main{
public static void main(String arg[]){
Scanner in = new Scanner(System.in);
int size = in.nextInt();
boolean frag = false;
int first = 0;
int last = 0;
float mid = 0;
boolean odd = true;
if( size%2 == 0 ) odd = false;
first = in.nextInt();
for(int i=2; i<size; i++){
if(odd && (i == (size + 1)/2)){
mid = in.nextInt();
}else if( (!odd) && (i == size/2 )){
mid = in.nextInt();
}else if( (!odd) && (i == size/2 + 1)){
mid += in.nextInt();
if(mid%2 != 0) frag = true;
mid /= 2;
}else{
in.nextInt();
}
}
last = in.nextInt();
in.close();
if(first > last){
int change = first;
first = last;
last = change;
}
if(frag){
System.out.printf("%d %.1f %d\n", last, mid, first);
}else{
System.out.printf("%d %d %d\n", last, (int)mid, first);
}
}
}
201903-2 二十四点
先算乘除, 后算加减 (用栈存储)
package com.caohongchuan;
import java.util.LinkedList;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
LinkedList<Integer> stack = new LinkedList<>();
Scanner in = new Scanner(System.in);
int size = in.nextInt();
for(int i=0; i<size; i++){
stack.clear();
int num = in.nextInt();
char oper = 0;
stack.offerFirst(num);
//calculate * / first
for(int j=0; j<3; j++){
String ne = in.next();
oper = ne.charAt(0);
num = in.nextInt();
if(oper == '/'){
int pre = stack.pollFirst();
pre = pre / num;
stack.offerFirst(pre);
}else if(oper == 'x'){
int pre = stack.pollFirst();
pre = pre * num;
stack.offerFirst(pre);
}else{
stack.offerFirst((int)oper);
stack.offerFirst(num);
}
}
int result = stack.pollFirst();
while(!stack.isEmpty()){
int operator = stack.pollFirst();
int next_num = stack.pollFirst();
if(operator == 43){
result += next_num;
}else{
result = next_num - result;
}
}
if(result == 24){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
in.close();
}
}
201903-4 消息传递接口
简单模拟,使用队列存储每一个进程的相应操作,依次轮巡搜索,使得匹配的操作出队列,依次模拟,判断其中是否会产生死锁。
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
private static LinkedList<String>[] list = new LinkedList[10010];
public static void main(String[] arg) {
for(int i=0; i<10010; i++) list[i] = new LinkedList<String>();
Scanner in = new Scanner(System.in);
int T = in.nextInt();
int n = in.nextInt();
in.nextLine(); //start from the next line
for(int i=0; i<T; i++) {
for(int j=0; j<n; j++) {
String lines = in.nextLine();
list[j].clear();
String[] line = lines.split(" ");
for(String oper : line) {
list[j].add(oper);
System.out.println(oper);
}
}
//judge
boolean success = true;
OUTER:
while(true) {
int num1 = countNumber(n);
for(int j=0; j<n; j++) {
String cur = list[j].peekFirst();
if(cur == null) continue;
//The first element
char Op = cur.charAt(0);
int Num = Integer.parseInt(String.valueOf(cur.charAt(1)));
String obj = list[Num].peekFirst();
//false, there is no operator
if(obj == null) {
success = false;
break OUTER;
}
//The relation element
char obj_op = obj.charAt(0);
int obj_num = Integer.parseInt(String.valueOf(obj.charAt(1)));
if(Op != obj_op && obj_num == j) {
//delete
list[j].pollFirst();
list[Num].pollFirst();
//continue for the j
j--;
}
}
int num2 = countNumber(n);
//finish
if(num2 == 0) {
break OUTER;
}
//dead lock
if(num1 == num2 && num1 != 0) {
success = false;
break OUTER;
}
}
if(success) System.out.println(0);
else System.out.println(1);
}
in.close();
}
public static int countNumber(int n) {
int ans =0;
for(int i=0; i<n; i++) {
ans += list[i].size();
}
return ans;
}
}