写代码之前一定要有明确的思路,最好把伪代码写一遍,千万不要一边写代码一边找思路,这样会出现很多漏洞,后期全是改bug
201803-1 跳一跳
每次只需要记录一下前面的状态,来决定本次应该加多少。
我第一次竟然把 刚开始为2的情况加了1,,被自己弱哭了。。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cur = -1;
int pre = -1;
int ans = 0;
while(in.hasNext()){
cur = in.nextInt();
if(cur == 0){
break;
}
if(cur == 1){
ans+=1;
pre = 1;
}else if(cur ==2){
if(pre == 1 || pre == -1){
ans+=2;
pre = 2;
}else if(pre > 1){
ans+=(pre+2);
pre+=2;
}
}
}
System.out.printf("%d\n", ans);
}
}
201803-2 碰撞的小球
这道题的关键是抓住小球碰撞后实际相当于交换。
有一个没有序号的球,一直在两个边界内无其他小球的阻碍来回移动,(相当于一个球自开始至结束都没有发生过任何碰撞,除与墙壁外)
使用类Ball模拟没有碰撞的小球运动(pos:位置,dir:方向),然后该球包含某一时刻小球代表的编号(id),如果发生碰撞,则交换两个 Ball 中 id 的编号,即交换两个小球。
最后按编号排序之后输出即可。
import java.util.Arrays;
import java.util.Scanner;
class Ball implements Comparable{
public int pos;
public boolean dir;
public int id;
public Ball(int pos, boolean dir, int id){
this.pos = pos;
this.dir = dir;
this.id = id;
}
@Override
public int compareTo(Object other){
Ball oth = (Ball)other;
if(this.id < oth.id){
return -1;
}else if(this.id > oth.id){
return 1;
}else{
return 0;
}
}
}
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n,L,t;
n = in.nextInt();
L = in.nextInt();
t = in.nextInt();
int[] used = new int[L+1];
Arrays.fill(used,-1);
Ball[] ball = new Ball[n];
int cur = -1;
for(int i=0; i<n; i++){
cur = in.nextInt();
ball[i] = new Ball(cur, true, i);
}
for(int i=1; i<=t; i++){
Arrays.fill(used, -1);
for(int j=0; j<n; j++){
//judge edge
if(ball[j].dir && ball[j].pos == L){
ball[j].dir = false;
}else if(!ball[j].dir && ball[j].pos == 0){
ball[j].dir = true;
}
//move to the next
if(ball[j].dir){
ball[j].pos += 1;
}else{
ball[j].pos -= 1;
}
//judge conflict
if(used[ball[j].pos] == -1){
used[ball[j].pos] = j;
}else{
int other = used[ball[j].pos];
int other_id = ball[other].id;
ball[other].id = ball[j].id;
ball[j].id = other_id;
}
}
}
Arrays.sort(ball);
for(int i=0; i<n; i++){
System.out.printf("%d ", ball[i].pos);
}
System.out.println();
}
}
201803-3 URL映射
我第一眼看到这个题,就想用树结构存储,结果写了一个dfs 过不去,,,以后有时间再搞,,,
本题就是暴力模拟。
- 首先用两个数组存储映射规则和规程名称。
- 之后每输出一个
url
直接扫描所有的规则即可。
需要注意的坑
- 如果
<int>
是 0 则不需要输出 - 结尾的匹配,只要保证规则和输入的
url
结尾一致即可。(都是/
结尾或都是标准结尾)
以下代码首先处理了结尾问题,这样方便同一判断。
import java.util.Scanner;
public class Main{
public static String[] reg = new String[110];
public static String[] name = new String[110];
public static boolean search(String cur, int index, boolean output){
int size1 = cur.length();
int size2 = reg[index].length();
int start1 = 0, start2 = 0;
String regg = reg[index];
//deal with the end
if(cur.charAt(size1-1) == '/' && regg.charAt(size2-1) != '/'){
return false;
}
if(cur.charAt(size1-1) != '/' && regg.charAt(size2-1) == '/'){
return false;
}
while(start1 < size1 && start2 < size2){
if(cur.charAt(start1) != regg.charAt(start2)){
if(regg.charAt(start2++) == '<'){
if(regg.charAt(start2) == 'i'){
start2 += 4;
int outStart1 = start1;
while(start1 < size1 && cur.charAt(start1) != '/'){
if(cur.charAt(start1)>57 || cur.charAt(start1)<48){
return false;
}
start1++;
}
//print
if(output){
int num = Integer.parseInt(cur.substring(outStart1, start1));
if(num != 0){
System.out.printf("%d ", num);
}
}
}else if(regg.charAt(start2) == 's'){
start2 += 4;
int outStart1 = start1;
while(start1 < size1 && cur.charAt(start1) != '/'){
start1++;
}
//print
if(output){
System.out.printf("%s ", cur.substring(outStart1, start1));
}
}else if(regg.charAt(start2) == 'p'){
if(cur.charAt(size1-1) != '/'){
//print
if(output){
System.out.printf("%s", cur.substring(start1, size1));
}
return true;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
start1++; start2++;
}
if(start1 < size1 || start2 < size2){
return false;
}else{
return true;
}
}
public static void main(String[] args) {
int n,m;
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
in.nextLine();
String[] input;
for(int i=0; i<n; i++){
input = in.nextLine().split(" ");
reg[i] = input[0];
name[i] = input[1];
}
String cur;
for(int i=0; i<m; i++){
cur = in.nextLine();
boolean match = false;
//match all the regex
for(int j=0; j<n; j++){
match = search(cur, j, false);
if(match){
System.out.printf("%s ", name[j]);
search(cur, j, true);
System.out.printf("\n");
break;
}
}
if(!match){
System.out.printf("404\n");
}
}
in.close();
}
}
package com.caohongchuan;
import java.util.*;
public class Main{
TreeMap
public static final int size = 3;
public static int[][] data = new int[size][size];
public static int getSorce(){
int ans = 0;
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(data[i][j]==0){
ans++;
}
}
}
return ans;
}
public static int win(int p){
boolean succ = false;
if(data[0][0]==p && data[1][1]==p && data[2][2]==p){
succ = true;
}
if(data[0][2]==p && data[1][1]==p && data[2][0]==p){
succ = true;
}
for(int i=0; i<size; i++){
boolean same = true;
for(int j=0; j<size; j++){
if(data[i][j]!=p){
same = false;
break;
}
}
if(same){
succ = true;
}
}
for(int i=0; i<size; i++){
boolean same = true;
for(int j=0; j<size; j++){
if(data[j][i]!=p){
same = false;
break;
}
}
if(same){
succ = true;
}
}
if(succ){
if(p==1) {
return getSorce();
}else if(p==2){
return -getSorce();
}else{
return 0;
}
}else{
return 0;
}
}
public static int dfs(int p){
if(getSorce()==0){
return 0;
}
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
if(data[i][j]==0){
data[i][j] = p;
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int k=0; k<n; k++){
//input
for(int i=0; i<size; i++){
for(int j=0; j<size; j++){
data[i][j] = in.nextInt();
}
}
int count1 = win(1);
int count2 = win(2);
if(count1 != 0){
System.out.printf("%d\n", count1);
}else if(count2 != 0){
System.out.printf("%d\n", count2);
}else{
System.out.printf("%d\n", dfs(1));
}
}
}
}