[数据结构和算法笔记]用vim写Java不是找罪受么

Posted 外面下着雪的小木屋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[数据结构和算法笔记]用vim写Java不是找罪受么相关的知识,希望对你有一定的参考价值。

        被要求用unix加vim写Java了,你说学生又不是手跟不上脑子,为啥要用vim来浪费时间呢...


        因为不熟悉语法,所以昨天写了一下午才大概把一题作业写出来,嘛,算是学了不少东西吧。


[数据结构和算法笔记]用vim写Java不是找罪受么

[数据结构和算法笔记]用vim写Java不是找罪受么

[数据结构和算法笔记]用vim写Java不是找罪受么

        上面的是题目和输入输出的例子,其实是很傻的一个问题,没啥难度,就是稍微复杂一点,但是作为一个Java新手(特别是基本不记得C和CPP的新手),用vim写还是经常会遇到奇怪的问题的,下面贴一下代码(第一次写这么长的代码):


如果想做这一题的话就先别看(真的很无聊的一个题)



import java.io.*;

import java.util.*;

import java.lang.*;

public class Storage {

public void run() {

Scanner scan = new Scanner(System.in);

int N = scan.nextInt();

int S = scan.nextInt();

int K = scan.nextInt();

int Q = scan.nextInt();

//System.out.println(N+" "+S+" "+K+" "+Q);

Item[] handy = new Item[S];

for (int i = 0; i < S; i += 1) {

handy[i] = new Item(" ", 0);

}


//initialize status of box


Box[] instore = new Box[N];

for (int i = 0; i < N; i += 1) {

instore[i] = new Box(K, (i + 1));

instore[i].ini();

//System.out.println(instore[i].onebox.length);


}

//System.out.println(instore.length);


//Process every line of query

for (int i = 0; i <= Q; i += 1) {

String query = scan.nextLine();

String[] queries = query.split(" ");

if (queries[0].equals("purchase")) {

purchase P = new purchase(S, N, K, handy, queries, instore);

P.process();

}


if (queries[0].equals("deposit")) {

deposit D = new deposit(S, N, K, handy, queries, instore);

D.process();

}


if (queries[0].equals("withdraw")) {

withdraw W = new withdraw(S, N, K, handy, queries, instore);

W.process();

}


if (queries[0].equals("location")) {

location L = new location(S, N, K, handy, queries, instore);

L.process();

}


if (queries[0].equals("valuable")){

valuable V= new valuable(S, N, K, handy, queries, instore);

V.process();

}


}

}


public static void main(String[] args) {

Storage myStorageSystem = new Storage();

myStorageSystem.run();

}


class Box {

int K;

Item[] onebox;

int boxnumber;


public Box(int a, int b) {

K = a;

onebox = new Item[K];

boxnumber = b;

}


public void ini() {

for (int i = 0; i < K; i += 1) {

onebox[i] = new Item(" ", 0);

}

}


}


class Item {

String itemname;

int value;


public Item(String ite, int v) {

itemname = ite;

value = v;

}

}


class purchase {

int S;

int N;

int K;

Item[] handy;

String[] queries;

Box[] instore;


public purchase(int s, int n, int k, Item[] hand, String[] query, Box[] is) {

S = s;

N = n;

K = k;

handy = hand;

queries = query;

instore = is;

}


public void process() {

int flag = 0;

for (int i = 0; i < S; i += 1) {

if (handy[i].itemname.equals(" ")) {

handy[i].itemname = queries[1];

try {

handy[i].value = Integer.parseInt(queries[2]);

} catch (NumberFormatException NFE) {

System.out.println(NFE);

}

flag = 1;

break;

}

}

if (flag == 1) {

System.out.println("item " + queries[1] + " is now being held");

} else {

int red_flag = 0;

for (int i = 0; i < N; i += 1) {

for (int j = 0; j < K; j += 1) {

if (instore[i].onebox[j].itemname.equals(" ")) {

instore[i].onebox[j].itemname = queries[1];

try {

instore[i].onebox[j].value = Integer.parseInt(queries[2]);

} catch (NumberFormatException NFE) {

System.out.println(NFE);

}

System.out.println("item " + queries[1] + " has been deposited to box " + instore[i].boxnumber);

red_flag = 1;

break;

}

}

if (red_flag == 1) {

break;

}

}

}

}

}


class deposit {

int S;

int N;

int K;

int pin = -1;

Item[] handy;

String[] queries;

Box[] instore;


public deposit(int s, int n, int k, Item[] hand, String[] query, Box[] is) {

S = s;

N = n;

K = k;

handy = hand;

queries = query;

instore = is;

}


public void process() {

int redflag = 0;

for (int i = 0; i < N; i += 1) {

for (int j = 0; j < K; j += 1) {

if (instore[i].onebox[j].itemname.equals(queries[1])) {

redflag = 1;

System.out.println("item " + queries[1] + " is already in storage");

break;

}

}

if (redflag == 1) {

break;

}

}

if (redflag == 0) {

int flag = 0;

for (int i = 0; i < S; i += 1) {

if (handy[i].itemname.equals(queries[1])) {

pin = i;

flag = 1;

break;

}

}

if (flag == 0) {

System.out.println("item " + queries[1] + " does not exist");

}

else{

int greenflag = 0;

for (int i = 0; i < N; i += 1) {

for (int j = 0; j < K; j += 1) {

if (instore[i].onebox[j].itemname.equals(" ")) {

instore[i].onebox[j].itemname = queries[1];

instore[i].onebox[j].value = handy[pin].value;

handy[pin].itemname = " ";

handy[pin].value = 0;

System.out.println("item " + queries[1] + " has been deposited to box " + (i + 1));

greenflag = 1;

break;

}

}

if (greenflag == 1) {

break;

}

}

}

}



}

}


class withdraw {

int S;

int N;

int K;

Item[] handy;

String[] queries;

Box[] instore;


public withdraw(int s, int n, int k, Item[] hand, String[] query, Box[] is) {

S = s;

N = n;

K = k;

handy = hand;

queries = query;

instore = is;

}


public void process() {

int redflag = 0;

for (int ln = 0; ln < S; ln += 1) {

if (handy[ln].itemname.equals(queries[1])) {

redflag = 1;

System.out.println("item " + queries[1] + " is already being held");

break;

}

}

if (redflag == 0) {

int flag = 0;

for (int i = 0; i < N; i += 1) {

for (int j = 0; j < K; j += 1) {

if (instore[i].onebox[j].itemname.equals(queries[1])) {

flag = 1;

int greenflag = 0;

for (int ll = 0; ll < S; ll += 1) {

if (handy[ll].itemname.equals(" ")) {

handy[ll].itemname = queries[1];

handy[ll].value = instore[i].onebox[j].value;

instore[i].onebox[j].itemname = " ";

instore[i].onebox[j].value = 0;

greenflag = 1;

break;

}

}

if (greenflag == 1) {

System.out.println("item " + queries[1] + " has been withdrawn");

break;

} else {

System.out.println("cannot hold any more items");

break;

}

}

}

if (flag == 1) {

break;

}

}

if (flag == 0) {

System.out.println("item " + queries[1] + " does not exist");

}

}

}

}


class location {

int S;

int N;

int K;

Item[] handy;

String[] queries;

Box[] instore;


public location(int s, int n, int k, Item[] hand, String[] query, Box[] is) {

S = s;

N = n;

K = k;

handy = hand;

queries = query;

instore = is;

}


public void process() {

int flag = 0;

for (int i = 0; i < S; i += 1) {

if (handy[i].itemname.equals(queries[1])) {

flag = 1;

System.out.println("item " + queries[1] + " is being held");

break;

}

}

int redflag = 0;

if (flag == 0) {

for (int i = 0; i < N; i += 1) {

for (int j = 0; j < K; j += 1) {

if (instore[i].onebox[j].itemname.equals(queries[1])) {

redflag = 1;

System.out.println("item " + queries[1] + " is in box " + (i + 1));

break;

}

}

if (redflag == 1) {

break;

}

}

}

if ((flag == 0) && (redflag == 0)) {

System.out.println("item " + queries[1] + " does not exist");

}

}

}


class valuable {

int S;

int N;

int K;

Item[] handy;

String[] queries;

Box[] instore;

ArrayList<String> alitem = new ArrayList<String>();

ArrayList alvalue = new ArrayList();


public valuable(int s, int n, int k, Item[] hand, String[] query, Box[] is) {

S = s;

N = n;

K = k;

handy = hand;

queries = query;

instore = is;

}


public void process() {

for (int i = 0; i < S; i += 1) {

if (handy[i].itemname.equals(" ")) {

continue;

} else {

alitem.add(handy[i].itemname);

alvalue.add(handy[i].value);

}

}

for (int i = 0; i < N; i += 1) {

for (int j = 0; j < K; j += 1) {

if (instore[i].onebox[j].itemname.equals(" ")) {

continue;

} else {

alitem.add(instore[i].onebox[j].itemname);

alvalue.add(instore[i].onebox[j].value);

}

}

}

int size = alitem.size();

String[] items = (String[]) alitem.toArray(new String[size]);

Object[] svalues = (Object[]) alvalue.toArray();

int[] values= new int[size];

for (int i=0;i<size;i+=1){

values[i]=Integer.parseInt(String.valueOf(svalues[i]));

}

Item[] owns = new Item[size];

for (int ll = 0; ll < size; ll += 1) {

owns[ll] = new Item(items[ll], values[ll]);

}

Arrays.sort(values);

int max= values[size-1];

int maxvalueitems=0;

for (int ll = 0; ll < size; ll += 1) {

if (owns[ll].value==max){

maxvalueitems+=1;

}

}

String[] maxitems= new String [maxvalueitems];

int counter=0;

for (int ll = 0; ll < size; ll += 1) {

if (owns[ll].value == max) {

maxitems[counter] = owns[ll].itemname;

counter += 1;

}

}

Arrays.sort(maxitems);

System.out.println(maxitems[0]);

}



}

}


        最后一个class写的不是很规范,但是通过了10个测试,所以准确性应该没问题。在做这个题的时候遇到了一些问题,先贴一个,后面的等做完剩下两题再写了。


空指针问题

class Item{

        String itemname;

        int value;

}

 Item[] handy = new Item[S];

这样初始化之后的问题是,定义了一个数组,数组的每一个位置都储存了一个Null的位置,其实就是一个空指针,就会报错:

Exception in thread "main" java.lang.NullPointerException

比如我们想初始化一下这个handy

for (int i=0;i<S;i+=1){

handy[i].itemname=" ";

handy[i].value=0;

}

就会出现上面的报错,在初始化的时候要每次都new一个item对象,就可以访问了:

for (int i=0;i<S;i+=1){

handy[i] = new Item();

handy[i].itemname=" ";

handy[i].value=0;

}


        今天就是这样,如果对这题有兴趣的可以留言,我可以给你测试的数据。



以上是关于[数据结构和算法笔记]用vim写Java不是找罪受么的主要内容,如果未能解决你的问题,请参考以下文章

PHP的设计模式之工厂模式

算法学习笔记

VIM入门

[FreeCodeCamp笔记] Python 数据结构和算法1 二分搜索 Binary Search

[FreeCodeCamp笔记] Python 数据结构和算法1 二分搜索 Binary Search

[FreeCodeCamp笔记] Python 数据结构和算法1 二分搜索 Binary Search