概述
1.
public class Test2001 {
public static void main(String[] args) throws FileNotFoundException {
File file=new File("D:\1code\test.txt");
Scanner input=new Scanner(file);
Collection<String> collection=new ArrayList<>();
while(input.hasNext()) {
collection.add(input.next());
}
PriorityQueue<String> word=new PriorityQueue(collection);
String c;
while((c=word.poll())!=null) {
System.out.println(c);
}
}
}
4.
public class Test2004 {
public static void main(String[] args) {
Point[] point =new Point[100];
CompareY[] comparey=new CompareY[100];
for(int i=0;i<100;i++) {
point[i]=new Point((int)(Math.random()*100),(int)(Math.random()*100));
}
Arrays.sort(point);
for(int i=0;i<100;i++)
System.out.println(point[i].getX()+" "+point[i].getY());
Arrays.sort(point,new CompareY());
for(int i=0;i<100;i++) {
System.out.println(point[i].getX()+" "+point[i].getY());
}
}
}
class Point implements Comparable<Point>{
private int x;
private int y;
public Point() {
this.x=0;
this.y=0;
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public int compareTo(Point o) {
// TODO Auto-generated method stub
if(this.getX()>o.getX())
return 1;
else if(this.getX()<o.getX())
return -1;
else if(this.getY()>o.getY())
return 1;
else if(this.getY()<o.getY())
return -1;
else
return 0;
}
}
class CompareY extends Point implements Comparator<Point>{
@Override
public int compare(Point o1, Point o2) {
// TODO Auto-generated method stub
if(o1.getY()>o2.getY())
return 1;
else if(o1.getY()<o2.getY())
return -1;
else if(o1.getX()>o2.getX())
return 1;
else if(o1.getX()<o2.getX())
return -1;
else
return 0;
}
}
11.
public class Test2011 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String string=input.nextLine();
System.out.println(match(string));
}
public static boolean match(String string) {
Stack<Character> stack=new Stack<>();
for(int i=0;i<string.length();i++) {
if(string.charAt(i)==')') {
while(!stack.empty()) {
if(stack.peek()=='(') {
stack.pop();
break;
}
else if(stack.peek()=='['||stack.peek()=='{')
return false;
else
stack.pop();
}
}
else if(string.charAt(i)=='}') {
while(!stack.empty()) {
if(stack.peek()=='{') {
stack.pop();
break;
}
else if(stack.peek()=='['||stack.peek()=='(')
return false;
else
stack.pop();
}
}
else if(string.charAt(i)==']') {
while(!stack.empty()) {
if(stack.peek()=='[') {
stack.pop();
break;
}
else if(stack.peek()=='('||stack.peek()=='{')
return false;
else
stack.pop();
}
}
else
stack.push(string.charAt(i));
}
if(stack.empty())
return true;
else
return false;
}
}
14.
public class Test2014 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String string=input.nextLine();
System.out.println(post(string));
}
public static int post(String string) {
Stack<Integer> stack=new Stack<>();
for(int i=0;i<string.length();i++) {
if(string.charAt(i)=='+') {
int a1=stack.pop();
int a2=stack.pop();
stack.push(a1+a2);
}
else if(string.charAt(i)=='-') {
int a1=stack.pop();
int a2=stack.pop();
stack.push(a2-a1);
}
else if(string.charAt(i)=='*') {
int a1=stack.pop();
int a2=stack.pop();
stack.push(a2*a1);
}
else if(string.charAt(i)=='/') {
int a1=stack.pop();
int a2=stack.pop();
stack.push(a2/a1);
}
else
stack.push((int)(string.charAt(i)-'0'));
}
return stack.pop();
}
}
16.
public class Test2016 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
String expression =input.nextLine();
System.out.println(infixToPostfix(expression));
}
public static String infixToPostfix(String expression) {
String postfix="";
Stack<Character> stack=new Stack<>();
for(int i=0;i<expression.length();i++) {
if(expression.charAt(i)=='(')
stack.push(expression.charAt(i));
else if(expression.charAt(i)=='+'||expression.charAt(i)=='-') {
while(!stack.empty()) {
if(stack.peek()!='(')
postfix+=stack.pop();
// if(stack.peek()==’+’)
// postfix+=stack.pop();
// else if(stack.peek()==’-‘)
// postfix+=stack.pop();
// else if(stack.peek()==’*’)
// postfix+=stack.pop();
// else if(stack.peek()==’/’)
// postfix+=stack.pop();
else {
break;
}
}
stack.push(expression.charAt(i));
}
else if(expression.charAt(i)=='*'||expression.charAt(i)=='/') {
while(!stack.empty()) {
if(stack.peek()=='*'||stack.peek()=='/')
postfix+=stack.pop();
else {
break;
}
}
stack.push(expression.charAt(i));
}
else if(expression.charAt(i)==')')
while(!stack.empty()) {
if(stack.peek()!='(')
postfix+=stack.pop();
else {
stack.pop();
break;
}
}
else {
postfix+=expression.charAt(i);
}
}
while(!stack.empty())
postfix+=stack.pop();
return postfix;
}
}
最后
以上就是背后宝马为你收集整理的java语言程序设计第二十章部分答案1,4,11,14,16题的全部内容,希望文章能够帮你解决java语言程序设计第二十章部分答案1,4,11,14,16题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复