section1.2主要包括5道题和1个编程知识介绍。下面对这6部分内容进行学习。

Complete Search

这个翻译成枚举搜索或者穷举搜索。主要用于当写代码时间不够用而且不用考虑程序的效率问题的时候。

这个方法简单易行,一般是做题目的首选,如果满足时间和空间的要求,那就这么做,把时间多出来去解决更难的问题。

需要注意的是,很多人经常不知不觉用了这个方法

Milking Cows

本题难度并不大,对输入数据进行排序是关键。后面的需要细心了。多测几次基本上能过。

/*
LANG: JAVA
TASK: milk2
*/ import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; public class milk2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("milk2.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milk2.out")));
ArrayList<Schedule> schedules = new ArrayList<Schedule>();
int length = Integer.parseInt(br.readLine());
for (int i = 0; i < length; i++) {
String[] timeInfo = br.readLine().split(" ");
Schedule oneSchedule = new Schedule(Integer.parseInt(timeInfo[0]), Integer.parseInt(timeInfo[1]));
schedules.add(oneSchedule);
}
Collections.sort(schedules, new MyCompare());
int start = schedules.get(0).start;
int end = schedules.get(0).end;
int intervalMilked = end - start;
int intervalNotMilked = 0; for (int i = 1; i < schedules.size(); i++) {
if (schedules.get(i).start > end) {
int newIntervalNotMilked = schedules.get(i).start - end;
intervalNotMilked = Math.max(intervalNotMilked, newIntervalNotMilked);
start = schedules.get(i).start;
end = schedules.get(i).end; } else if (schedules.get(i).start <= end) {
int newIntervalMilked = schedules.get(i).end - start;
intervalMilked = Math.max(newIntervalMilked, intervalMilked);
end = Math.max(end, schedules.get(i).end);
}
} pw.println(intervalMilked + " " + intervalNotMilked);
pw.close();
br.close();
} } class Schedule {
public int start;
public int end;
public Schedule (int start, int end) {
this.start = start;
this.end = end;
}
} class MyCompare implements Comparator<Schedule> {
public int compare(Schedule a, Schedule b) {
if (a.start > b.start) return 1;
else if (a.start < b.start) return -1;
else return 0;
}
}

Transformations

这小节果然是穷举法啊。这题也没什么好说的,把所有情况都检查一下,考察代码能力多于算法能力。

/*
LANG: JAVA
TASK: transform
*/
import java.io.*;
public class transform {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("transform.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("transform.out")));
//initialization
int n = Integer.parseInt(br.readLine());
int[][] before = new int[n][n];
int[][] after = new int[n][n];
for (int i = 0; i < n; i++) {
String line = br.readLine();
for (int j = 0; j < line.length(); j++) {
before[i][j] = line.charAt(j);
}
}
for (int i = 0; i < n; i++) {
String line = br.readLine();
for (int j = 0; j < line.length(); j++) {
after[i][j] = line.charAt(j);
}
} if (clockwise90(before, after)) pw.println("1");
else if (clockwise180(before, after)) pw.println("2");
else if (clockwise270(before, after)) pw.println("3");
else if (reflection(before, after)) pw.println("4");
else if (combination(before, after)) pw.println("5");
else if (noChange(before, after)) pw.println("6");
else pw.println("7"); pw.close();
br.close();
} public static boolean clockwise90(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[j][n - 1 - i]) return false;
}
}
return flag;
} public static boolean clockwise180(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[n - 1 - i][n - 1 - j]) return false;
}
}
return flag;
} public static boolean clockwise270(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[n- 1- j][i]) return false;
}
}
return flag;
} public static boolean reflection(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[i][n - 1 - j]) return false;
}
}
return flag;
} public static boolean combination(int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
int[][] newBefore = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newBefore[i][j] = before[i][n - 1 - j];
}
}
if (clockwise90(newBefore, after) || clockwise180(newBefore, after) || clockwise270(newBefore, after)) {
return true;
}
else {
flag = false;
}
return flag;
} public static boolean noChange (int[][] before, int[][] after) {
boolean flag = true;
int n = before.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (before[i][j] != after[i][j]) return false;
}
}
return flag;
}
}

Name That Number

这个题目要是直接枚举空间复杂度就大了,一个小技巧就是先对dict.txt处理成哈希表,用一个数字映射多个字符串,通过目标数字,得到这一列字符串。

有一个需要注意的问题是,哈希表里面的key要设为Long类型,因为有些字符串太长了无法解析成Integer。

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects; /*
ID: jameszh1
LANG: JAVA
TASK: namenum
*/
public class namenum {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("namenum.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("namenum.out")));
BufferedReader brDict = new BufferedReader(new FileReader("dict.txt")); //initialization
int[] letterValue = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9,0};
long num = Long.parseLong(br.readLine());
String line = null;
HashMap<Long, ArrayList<String>> dict = new HashMap<>();
while ((line = brDict.readLine()) != null) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < line.length(); i++) {
sb.append(String.valueOf(letterValue[(line.charAt(i) - 'A')]));
}
if (dict.containsKey(Long.parseLong(sb.toString()))) {
dict.get(Long.parseLong(sb.toString())).add(line);
} else {
ArrayList al = new ArrayList();
al.add(line);
dict.put(Long.parseLong(sb.toString()), al);
}
} if (dict.containsKey(num)) {
ArrayList al = dict.get(num);
for (Object s: al) {
pw.println(s);
}
}
else {
pw.println("NONE");
} pw.close();
br.close(); }
}

Palindromic Squares

看上去是一到很简单的题目,难度其实在于进制的任意转换。不过也可以用java内置的函数BigInteger.toString()。

import java.io.*;
import java.math.BigInteger; /*
LANG: JAVA
TASK: palsquare
*/
public class palsquare {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("palsquare.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("palsquare.out"))); int base = Integer.parseInt(br.readLine());
for (int i = 1; i <= 300; i++) {
BigInteger b = new BigInteger(String.valueOf(i), 10);
BigInteger bSquare = new BigInteger(String.valueOf(i*i), 10);
if (isPalindromic(bSquare.toString(base))) {
pw.println(b.toString(base).toUpperCase() + " " + bSquare.toString(base).toUpperCase());
}
} pw.close();
br.close();
} public static boolean isPalindromic(String s) {
StringBuffer sb = new StringBuffer(s);
if (sb.toString().equals(sb.reverse().toString())) return true;
else return false;
}
}

Dual Palindromes

这个题做的郁闷。题目要求里有S的范围,看了测试用例才知道是起始点的范围,并不是所有数的搜寻范围。本来以为时间复杂度不会通过的,结果直接穷举也通过了。

import java.io.*;
import java.math.BigInteger; /*
LANG: JAVA
TASK: dualpal
*/
public class dualpal {
public static void main (String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("dualpal.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dualpal.out"))); String[] input = br.readLine().split(" ");
int number = Integer.parseInt(input[0]);
int start = Integer.parseInt(input[1]);
int i = start + 1;
while (number > 0) {
BigInteger b = new BigInteger(String.valueOf(i), 10);
int count = 0;
for (int base = 2; base <= 10; base++) {
String s = b.toString(base);
if (isPalindromic(s) && number > 0) {
count++;
if (count >= 2) {
pw.println(b);
number--;
break;
}
}
}
i++;
} pw.close();
br.close();
} public static boolean isPalindromic(String s) {
StringBuffer sb = new StringBuffer(s);
if (sb.toString().equals(sb.reverse().toString())) return true;
else return false;
}
}

USACO Section1.3的更多相关文章

  1. USACO Section1.2

    section1.1主要包括四道题和两个编程知识介绍.下面将对这6个部分内容进行学习. Your Ride Is Here 这道题没什么难度,读懂题目意思就行:把两个字符串按照题目要求转换成数字,然后 ...

  2. USACO Section1.1

    本系列博客主要学习和记录USACO的相关代码和总结,附上我的github地址. 什么是USACO USACO全称是The USA Computing Olympiad,主要目的是从美国高中生中选出代码 ...

  3. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  4. USACO Section1.4 Mother's Milk 解题报告

    milk3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  5. USACO Section1.3 Wormholes 解题报告

    wormhole解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------- ...

  6. USACO Section1.2 Name That Number 解题报告

    namenum解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

  7. USACO Section1.1 Friday the Thirteenth 解题报告

    friday解题报告 —— icedream61 博客园(转载请注明出处) -------------------------------------------------------------- ...

  8. USACO section1.1 Broken Necklace

    /* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...

  9. USACO section1.2 Miking cows

    /* ID: vincent63 LANG: C TASK: milk2 */ #include <stdio.h> #include<stdlib.h> #include&l ...

随机推荐

  1. 转载:python的编码处理(一)

    以下内容转载自: http://in355hz.iteye.com/blog/1860787 最近业务中需要用 Python 写一些脚本.尽管脚本的交互只是命令行 + 日志输出,但是为了让界面友好些, ...

  2. 连接Mysql错误 error 1042 can't get hostname for your address

    背景: 1.etc下的hosts文件有:     127.0.0.1   localhost 2.MySQL的my.ini配置文件:     [mysqld] 节点下已经加入以下两行代码 skip-n ...

  3. C# 算法题系列(一) 两数之和、无重复字符的最长子串

    题目一 原题链接 https://leetcode-cn.com/problems/two-sum/ 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整 ...

  4. ubuntu常见问题排查

    1. ubuntu recovery mode read-only 获取写的权限 1.1. 选择fsck check all file systems 进去直接选择YES就可以了 1.2. 选择roo ...

  5. python第十九课——random模块中的常用函数

    1.random():返回一个[0,1)的随机浮点数(双精度浮点数) 2.uniform(a,b): 返回[a,b]之间的一个随机浮点数(双精度浮点数) [注意]a和b接受的数据大小随意 例如:3.r ...

  6. 8年前,令我窒息的Java socket体验学习

    本来已经放弃编程了,那时我发誓再也不去IT培训班了,如果找不到工作,我就去工地上打工.可心有不甘,老是惦记着,我不想天天面对生产线,做一个丧失思考能力的操作工,可后来呀,还是走上了程序员之路...这么 ...

  7. chrome://命令

    一些常用的命令: chrome://version 显示当前版本 chrome://flags 实验项目,加“#项目名称”锚点可以直接定位到项目 chrome://settings 设置,下图是设置定 ...

  8. webpack.config.js配置遇到Error: Cannot find module '@babel/core'问题

    在webpack配置,将ES6转成ES5的时候,,出现Error: Cannot find module '@babel/core'错误最初以为是babel-core没有安装上.重装了好几遍babel ...

  9. Jython的应用

    今天本文围绕主要内容是jython是什么.安装.简单实用. 另外说说我为什么研究jython,研究它是有一个目的的,目的是将python代码转化为jar包以供安卓方面那边人脸识别,虽说目前人脸识别像阿 ...

  10. @SpringBootApplication无法被解析引入

    问题描述:@SpringBootApplication无法被解析引入,导致SpringBoot启动类报错 原因分析:springboot的包冲突了所致 解决方案: 需要删掉 repository\or ...