总结一下国庆放假前一周训练赛二的一些算法题:
首先第一题
上车问题,其实主要是递归的运用:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int b,a,n,m,x,s,num;
Scanner scan =new Scanner(System.in);
a=scan.nextInt();
n=scan.nextInt();
m=scan.nextInt();
x=scan.nextInt();
b = (m-(lu(n-1) * a)) / ld(n-1);
s = (lu(x)*a) + (ld(x)*b);
System.out.print(s);
}
static int upbusA(int n){
if(n==1){
return 1;
}else if(n==2){
return 0;
}else{
return upbusA(n-1) + upbusA(n-2);
}
}
static int upbusB(int n){
if(n==1){
return 0;
}else if(n==2){
return 1;
}else{
return upbusB(n-1) + upbusB(n-2);
}
}
static int downbusA(int n){
if(n==1 || n==2){
return 0;
}else{
return upbusA(n-1);
}
}
static int downbusB(int n){
if(n==1){
return 0;
}else if(n==2){
return 1;
}else {
return upbusB(n-1);
}
}
static int lu(int n){
if(n==1 || n==2){
return 1;
}else{
return (upbusA(n) - downbusA(n)) + lu(n-1);
}
}
static int ld(int n){
if(n==1 || n==2){
return 0;
}else{
return (upbusB(n) - downbusB(n)) + ld(n-1);
}
}
}
对于第二题,题目很简单,当时却研究了好久无从下手,后来看了答案才发现真的很简单,主要是用Treeset集合以及其中方法,对于Treeset而言他的存储是有序的,不重复的
```java
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Set<String> set = new TreeSet<String>();
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String[] a = sc.next().split("/");
set.add(a[2] + a[1] + a[0]);
}
for(String str : set)
System.out.printf("%s/%s/%s\n", str.substring(6), str.substring(4, 6), str.substring(0, 4));
sc.close();
}
}
这里边的两个方法很适合解这道题,split()主要是切割字符,因为题目中的时间含有特殊字符:“/”
对于for(String str : set)即为遍历数组,可以在此之后进行大小比对,然后输出即可,应当注意的是输出语句中的str.substring是对字符串进行切割的方法,String a = “abc”.substring(1,3)执行之后得到的就是a = bc
第三题当时做了,但是有出错:
import java.util.Scanner;
public class Main
{
static int [] step ;
public static int solve(int n){
step = new int[n+1] ;
step[2] = 1 ;
step[3] = 2 ;
for( int i = 4 ; i <= n ; i++){
step[i] = step[i-1] + step[i-2] ;
}
return step[n];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(solve(scanner.nextInt()));
scanner.close();
}
}
看了之后发现是自己想复杂了,其实就是一个斐波那契数列的应用,2或者3个台阶的走法个数是固定的,而后边更多的台阶只需要将之前的相加再把额外的加上即可,只能说是一道很基础的题了。
第四题:属实是不会写查了查资料发现是dfs的应用,确实是当时并没有学会,到现在还是迷迷糊糊的,现在,马上就去补!
import java.io.*;
import java.util.*;
public class Main {
static StreamTokenizer cin;
static PrintWriter out;
static int N;
static int k = 9;
static int maxN = (int)1E6;
static int count = 0;
static LinkedList<Integer> arr = new LinkedList<>();
public static void main(String[] args) throws IOException{
cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
N = nextInt();
for(int i = 1; i <= k; i++)
arr.add(i);
dfs(arr, new LinkedList<>());
out.println(count);
out.flush();
out.close();
}
static void dfs(LinkedList<Integer> list, LinkedList<Integer> his){
if(his.size() == k){
/**枚举带分数情况**/
int zheng = 0;
for(int j = 0; j < his.size(); j++){
zheng = zheng*10 + his.get(j);
if(zheng > N) // 剪枝
break;
int rest = N - zheng; // 分数需要达到的数值
int split = j+(his.size()-j)/2; // 初始间隔位置
int fz = 0; // 分子
int fm = 0; // 分母
// 初始化分子分母
for(int z = j+1; z < split+1; z++)
fz = fz*10 + his.get(z);
for(int z = split+1;z < his.size(); z++)
fm = fm*10 + his.get(z);
if(zheng == 3 && fz == 6952 && fm == 8714)
out.print("");
for(int z = split+1; z < his.size(); z++){ // 枚举分母位置split2, 可剪枝, 分子一定比分母大
if(fz%fm == 0 && fz/fm== rest){
count++;
break;
}
if(z == his.size()-1)
break;
fz = fz*10 + his.get(z);
fm = fm%((int)Math.pow(10, his.size() - z - 1));
}
}
return;
}
for(int i = 0; i < list.size(); i++){
int num = list.remove(i);
his.add(num);
dfs(list, his);
// 回溯
his.removeLast();
list.add(i, num);
}
}
static int nextInt() throws IOException{
cin.nextToken();
return (int)cin.nval;
}
}
第八题,前边几道都是蓝桥杯的题,都是点进去看了看发现比较难,时间也不是很够就没下笔,却看漏了最后一道,题目比较长,但是仔细看看还是可以做出来的
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt(),m=in.nextInt(),i;
int []a=new int[n],b=new int [m];
int la,ra,lb,rb;
for(i=0;i<n;i++)
a[i]=in.nextInt();
la=in.nextInt();
ra=in.nextInt();
for(i=0;i<m;i++)
b[i]=in.nextInt();
lb=in.nextInt();
rb=in.nextInt();
int x,y;
for(x=la-1,y=lb-1;x<ra&&y<rb;)
{
if(a[x]<b[y])
{
x++;
System.out.printf("1 ");
}
else
{
y++;
System.out.printf("2 ");
}
}
while(x<ra)
{
x++;
System.out.printf("1 ");
}
while(y<rb)
{
y++;
System.out.printf("2 ");
}
}
}