(1)【数据结构1-1】线性表
这是一道很基础的题目。就是单纯的考察数组的存储和使用,但是因为数据量会超过java的极限。所以用输入挂才可以过。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
int n=sc.nextInt(),m=sc.nextInt();
int[] arr=new int[n+1];
for(int i=1;i<=n;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<m;i++) {
System.out.println(arr[sc.nextInt()]);
}
}
}
class InputReader{
StreamTokenizer tokenizer;
public InputReader(InputStream stream){
tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}
(2)P3613 【深基15.例2】寄包柜
这题不能开二维数组,内存肯定不够。应该使用多个两个的一维数组来模拟。同样,也要用输入挂
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt(),q=sc.nextInt();
node[] desk=new node[100005];
for(int i=0;i<n;i++) {
desk[i]=new node();
}
while(q-->0)
{
int x,a,b,c;
x=sc.nextInt();
if(x == 1)
{
a=sc.nextInt();b=sc.nextInt();c=sc.nextInt();
desk[a].s++;
desk[a].num.add(b);
desk[a].w.add(c);
}
else
{
a=sc.nextInt();b=sc.nextInt();
for(int i = desk[a].s - 1;i >= 0;i--)
{
if(desk[a].num.get(i) == b)
{
out.println(desk[a].w.get(i));
break;
}
}
}
}
out.close();
}
}
class node{
ArrayList<Integer> num=new ArrayList<Integer>(),w=new ArrayList<Integer>();
int s = 0;
}
class InputReader{
StreamTokenizer tokenizer;
public InputReader(InputStream stream){
tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}
(3)P1449 后缀表达式
考察栈的使用,后缀表达式本质就是栈的使用,如果是数字就入栈,如果是操作符就把出栈两个数字再入栈,循环这个操作直到结束
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
Stack<Integer> stack=new Stack<Integer>();
int num=0;
for(int i=0;i<s.length();i++) {
char x=s.charAt(i);
if(Character.isDigit(x)) {
num=num*10+(int)(x-'0');
}
if(x=='.') {
stack.push(num);
num=0;
}
if(x=='+') {
int a=stack.pop();
int b=stack.pop();
stack.push(a+b);
}
if(x=='-') {
int a=stack.pop();
int b=stack.pop();
stack.push(b-a);
}
if(x=='*') {
int a=stack.pop();
int b=stack.pop();
stack.push(a*b);
}
if(x=='/') {
int a=stack.pop();
int b=stack.pop();
stack.push(b/a);
}
}
System.out.println(stack.pop());
}
}
(4)P1996 约瑟夫问题
就是一直对一段数字进行循环遍历,到某个特定的数字时,出列某个位置,用一个布尔数组记录,下次到这个位置时,就跳过,直到只有一个位置没被标记,结束循环。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
boolean[] arr=new boolean[n+1];
int count=0;
int k=0;
while(count!=n) {
int mm=m;
while(mm>0) {
k++;
if(k>n) {
k-=n;
}
if(!arr[k]) {
mm--;
}
}
System.out.print(k+" ");
count++;
arr[k]=true;
}
}
}
(5)P1160 队列安排
又是一道java不能过的题目,这题利用集合类linkedlist可以很好的完成这个不断插入删除的工作,但是因为数据量过大,java还是不能全部ac.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
LinkedList<Integer> al=new LinkedList<Integer>();
al.add(1);
for(int i=2;i<=n;i++) {
al.add(al.indexOf(sc.nextInt())+sc.nextInt(),i);
}
int m=sc.nextInt();
for(int i=0;i<m;i++) {
int x=sc.nextInt();
if(al.indexOf(x)!=-1)
al.remove(al.indexOf(x));
}
for(int x:al) {
out.print(x+" ");
}
out.close();
}
}
class InputReader{
StreamTokenizer tokenizer;
public InputReader(InputStream stream){
tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}
(6)P1540 机器翻译
一道模拟题目。用一个数组来模拟储存的内存,每看到一个单词就加入内存,如果插满了就从头开始更新数组。然后每次查询的时候利用一个辅助的布尔数组来记录这个单词是否在内存中
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int M=sc.nextInt();
int N=sc.nextInt();
int[] help=new int[1001];
int[] arr=new int[M];
Arrays.fill(arr, -1);
int k=0;
int count=0;
for(int i=0;i<N;i++) {
int x=sc.nextInt();
if(help[x]==1) {
continue;
}
count++;
if(arr[k]!=-1) {
help[arr[k]]=0;
}
help[x]=1;
if(k==M-1) {
arr[k]=x;
k=0;
}
else {
arr[k++]=x;
}
}
System.out.println(count);
}
}
(7)P2058 海港
这道题关键点在于需要存的是每个人的信息。用2个数组,分别存每个人到达的时间和属于的国籍,然后再用一个数组,来存每个国籍目前的人数。然后进行模拟,每当有一个船进来时,往数组里面填数据,如果出现了新国籍的人,那么计数加1。当到达的人的时间减去最开始到达的时间超过24小时,那么要最开始到达的人他那个国家的人数要减1。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
int n=sc.nextInt();
int[] time=new int[300005]; //记录第i个人他到达的时间
int[] peo=new int[300005]; //记录第i个人他的国籍
int[] arr=new int[100005]; //记录每个国籍的人数
int z=0,num=0,r=0;
while(n-->0) {
int t=sc.nextInt(),k=sc.nextInt();
while(k-->0) {
peo[r]=sc.nextInt();
time[r]=t;
if(arr[peo[r]]==0) {
num++;
}
arr[peo[r]]++;
r++;
}
while(t-time[z]>=86400) {
arr[peo[z]]--;
if(arr[peo[z++]]==0) {
num--;
}
}
System.out.println(num);
}
}
}
class InputReader{
StreamTokenizer tokenizer;
public InputReader(InputStream stream){
tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}
(8)P1241 括号序列
这道题考察栈的使用,如果是左括号入栈,如果是右括号就与栈首匹配,看是否匹配,匹配就出栈,然后给这两个匹配成功的字符用一个布尔数组标记。最后再扫描一遍字符串,如果匹配成功了就直接输出字符,没有匹配成功的帮这个字符进行补全再输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
int n=s.length();
Stack<Character> st=new Stack<Character>();
Stack<Integer> st1=new Stack<Integer>();
char[] arr=new char[n];
boolean[] vis=new boolean[n];
for(int i=0;i<n;i++) {
arr[i]=s.charAt(i);
if(arr[i]=='('||arr[i]=='[') {
st.add(arr[i]);
st1.add(i);
}
else {
if(!st.isEmpty()) {
if(arr[i]==')'&&st.peek()=='(') {
st.pop();
vis[st1.pop()]=true;
vis[i]=true;
}
if(arr[i]==']'&&st.peek()=='[') {
st.pop();
vis[st1.pop()]=true;
vis[i]=true;
}
}
}
}
for(int i=0;i<n;i++) {
if((arr[i]=='('||arr[i]=='[')&&vis[i]==true) {
System.out.print(arr[i]);
}
if((arr[i]=='('||arr[i]==')')&&vis[i]==false){
System.out.print("()");
}
if((arr[i]=='['||arr[i]==']')&&vis[i]==false){
System.out.print("[]");
}
if((arr[i]==')'||arr[i]==']')&&vis[i]==true) {
System.out.print(arr[i]);
}
}
}
}
(9)P4387 【深基15.习9】验证栈序列
直接用java自带的栈模拟这个序列是否正确。如果栈不空并且出栈序列的当前位置等于栈首,那么出栈,如果栈空或者出栈序列的当前位置不等于栈首,那么入栈。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int q=sc.nextInt();
int[] a=new int[100000];
int[] b=new int[100000];
while(q-->0) {
Stack<Integer> st=new Stack<Integer>();
int n=sc.nextInt();
boolean flag=true;
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
b[i]=sc.nextInt();
}
int k=0;
for(int i=0;i<n;i++) {
st.push(a[i]);
while(!st.isEmpty()&&st.peek()==b[k]) {
k++;
st.pop();
}
}
if(st.isEmpty()) {
out.println("Yes");
}
else {
out.println("No");
}
}
out.close();
}
}
class InputReader{
StreamTokenizer tokenizer;
public InputReader(InputStream stream){
tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}
(10)P2234 [HNOI2002]营业额统计
省选难度的题目,定义一个结构体,存天数和营业额,然后按营业额排序。然后遍历数组,从两边寻找天数在当前位置天数前面的营业额。因为排序的关系,所以第一个找到的营业额肯定是绝对值差最小的。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
no[] arr=new no[n];
for(int i=0;i<n;i++) {
arr[i]=new no(i,sc.nextInt());
}
Arrays.sort(arr);
long sum=0;
for(int i=0;i<n;i++) {
int temp=Integer.MAX_VALUE;
for(int j=i-1;j>=0;j--) {
if(arr[i].day>arr[j].day) {
temp=Math.min(temp, Math.abs(arr[i].num-arr[j].num));
break;
}
}
for(int j=i+1;j<n;j++) {
if(arr[i].day>arr[j].day) {
temp=Math.min(temp, Math.abs(arr[i].num-arr[j].num));
break;
}
}
if(arr[i].day==0) {
sum+=arr[i].num;
}
else {
sum+=temp;
}
}
out.println(sum);
out.close();
}
}
class no implements Comparable<no>{
int day,num;
no(int day,int num){
this.day=day;this.num=num;
}
@Override
public int compareTo(no o) {
return num-o.num;
}
}
class InputReader{
StreamTokenizer tokenizer;
public InputReader(InputStream stream){
tokenizer=new StreamTokenizer(new BufferedReader(new InputStreamReader(stream)));
tokenizer.ordinaryChars(33,126);
tokenizer.wordChars(33,126);
}
public String next() throws IOException {
tokenizer.nextToken();
return tokenizer.sval;
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean hasNext() throws IOException {
int res=tokenizer.nextToken();
tokenizer.pushBack();
return res!=tokenizer.TT_EOF;
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
}