《算法》第四版(习题1.3.1,1.3.2,1.3.3,1.3.4,1.3.5)

本博客详细解析了五道算法练习题,包括添加方法至FixedCapacityStackOfStrings、判断出栈顺序、使用Stack验证括号匹配、打印二进制表示及处理输入流。通过代码示例和运行结果,深入探讨了数据结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

习题1.3.1

题目描述

为FixedCapacityStackOfStrings添加一个方法isFull()

代码

package chapter1;

/**   
*    
* 项目名称:Algorithms4   
* 类名称:P1_3_01   
* 类描述:练习1.3.1,为FixedCapacityStackOfStrings添加isFull(方法)   
* 创建人:bbbdbbb   
* 创建时间:2020年1月31日 下午8:36:10   
* @version        
*/
public class P1_3_01 {
	/**
	 * 添加isFull()方法后的代码
	 */
//	public class FixedCapacityStackOfStrings<Item> {
//		private Item[] a;
//		private int N;
//		
//		public FixedCapacityStackOfStrings(int cap){
//			a = (Item[]) new Object[cap];
//		}
//		public boolean isEmpty(){
//			return N==0;
//		}
//		public int size(){
//			return N;
//		}
//		public void push(Item item){
//			a[N++] = item;
//		}
//		public Item pop(){
//			return a[--N];
//		}
//		public boolean isFull(){
//			return N==a.length;
//		}
//	}
	

}

习题1.3.2

题目描述

给定以下输入,java Stack的输出是什么?
it was - the best - of times - - - it was - the - -

代码

package chapter1;

import collection.Stack;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

/**   
*    
* 项目名称:Algorithms4   
* 类名称:P1_3_02   
* 类描述:习题1.3.2,给一组新的输入,调试P84的main方法   
* 创建人:bbbdbbb   
* 创建时间:2020年1月31日 下午8:51:02   
* @version        
*/
public class P1_3_02 {
	public static void main(String[] args) {
		Stack<String> s = new Stack<>();
		while(!StdIn.isEmpty()){
			String item = StdIn.readString();
			if(!item.equals("-"))
				s.push(item);
			else if(!s.isEmpty()){
				StdOut.print(s.pop()+"  ");
			}
			
		}
		StdOut.println("\n剩下的元素数量:"+s.size());
	}
}

运行结果截图

在这里插入图片描述
  习题1.3.2应该是说将P84的测试用例中的类FixedCapacityStack换成P94的类Stack,用题目中给的数据作为新的测试数据。如果不会P84使用方法中的设置标准输入文件,
请看我的这篇blog:设置标准输入,输出  这里需要注意一定,程序在读取标准输入中的数据后,不会自己结束,需要在console面板中按快捷键Ctrl+z,就可以继续执行后面的代码了。

习题1.3.3

题目描述

假设某个用例程序会进行一系列入栈和出栈的混合操作。入栈操作会将整数0到9按顺序压入栈;出栈操作会打印出返回值。下面哪种序列是不可能产生的?
a. 4 3 2 1 0 9 8 7 6 5
b. 4 6 8 7 5 3 2 9 0 1
c. 2 5 6 7 4 8 9 3 1 0
d. 4 3 2 1 0 5 6 7 8 9
e. 1 2 3 4 5 6 9 8 7 0
f. 0 4 6 5 3 8 1 7 2 9
g. 1 4 7 9 8 6 5 3 0 2
h. 2 1 4 3 6 5 8 7 9 0

代码

package chapter1;

import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdIn;

/**
 * 
 * 项目名称:Algorithms4 
 * 类名称:P1_3_03 
 * 类描述:练习1.3.03,判断出栈顺序是否正确 入栈操作会将整数0到9按顺序压入栈;
 * 		     出栈操作会打印出返回值。 下面哪种序列是不可能产生的? 
 * 创建人:bbbdbbb 
 * 创建时间:2020年1月31日 下午9:16:15
 * 
 * @version
 */
public class P1_3_03 {
	/**
	 * 这到题可以人为直接判断,但是想到我要参加蓝桥杯比赛(我是一个新手), 就打算使用代码来判断整数的出栈顺序是否正确
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		int N = 8;// 输入:行数
		// 建立一个整数栈的数组
		Queue<Integer>[] a = (Queue<Integer>[]) new Queue[N];
		int i = 0;
		for(int j=0;j<N;j++){
			a[j] = new Queue<Integer>();
		}

		//获取数据
		for (;i<N; i++) {
			for(int j=0;j<10;j++){
				if (!StdIn.isEmpty()) {
					int item = StdIn.readInt();
					
					a[i].enqueue(item);
				}else{
					System.out.println("输入数据错误!!!");
					System.exit(0);
				}	
			}					
		}
		
		//判断
		for(int j=0;j<N;j++){
			//先打印 题目中整数的出栈序列
			for(int num : a[j]){
				System.out.print(num+"  ");
			}

			boolean bigFlag = true;		
			//判断
			while(!a[j].isEmpty()){
				Queue<Integer> temp = new Queue<Integer>();
				int number = a[j].dequeue();//从栈中拿出来一个数number
				for(int num : a[j]){//把栈中所有比number小的数压入(复制)到temp栈中
					if(number > num){
						temp.enqueue(num);
					}
				}
				//判断比number小的数是否逆序
				boolean flag = true;
				while(!temp.isEmpty()){
					int t = temp.dequeue();
					if(number<t){
						flag = false;
						break;
					}
					number = t;
				}
				if(flag==false){
					bigFlag = false;
					System.out.println(bigFlag);
					break;
				}
			}
			
			if(bigFlag==true){
				System.out.println(bigFlag);
			}
		}

	}
}

运行结果截图

在这里插入图片描述
  判断出栈顺序是否正确的方法:从左到到右依次取一个数number,然后判断number右边所有比它小的整数是否降序排列,如果不是降序排列,就说明出栈顺序不对。

习题1.3.4

题目描述

编写一个Stack的用例Parentheses,从标准输入中读取一个文本流并使用栈判定其中的括号是否配对完整.例如[()]{}{[()]}为true,对于[(])程序则打印false。

代码

package chapter1;

import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

/**   
*    
* 项目名称:Algorithms4   
* 类名称:P1_3_04_Parenthses   
* 类描述:练习1.3.04,编写一个Stack用例Parenthses,
* 			使用栈判断括号是否配对完整   
* 创建人:bbbdbbb   
* 创建时间:2020年2月1日 下午4:46:49   
* @version        
*/
public class P1_3_04_Parenthses {

	public static void main(String[] args) {
		Stack<String> s = new Stack<>();
		System.out.println("输入:");
		while(!StdIn.isEmpty()){
			String item = StdIn.readString();
			System.out.print(item+" ");
			if(item.equals("(") || item.equals("[") || item.equals("{")){
				s.push(item);
			}else if(!s.isEmpty() && item.equals(")")){
				if(s.isEmpty()){
					System.out.println("\n结果:"+false);
					System.exit(0);
				}
				String str = s.pop();
				if(!"(".equals(str)){
					System.out.println("\n结果:"+false);
					System.exit(0);
				}
			}else if(!s.isEmpty() && item.equals("]")){
				String str = s.pop();
				if(!"[".equals(str)){//]
					System.out.println("\n结果:"+false);
					System.exit(0);
				}
			}else if(!s.isEmpty() && item.equals("}")){
				String str = s.pop();
				if(!"{".equals(str)){
					System.out.println("\n结果:"+false);
					System.exit(0);
				}
			}else{//标准输入中含有其他字符
				System.out.println("\n结果:"+false);
				System.exit(0);
			}
			
		}
		//程序执行到这里,说明全部括号配对完整  
		System.out.println("\n结果:"+true);
	}

}

运行结果截图

在这里插入图片描述
在这里插入图片描述

习题1.3.5

题目描述

当N为50时下面这段代码会打印什么?从较高的抽象层次描述给定正整数N时这段代码的行为。

代码

package chapter1;

import edu.princeton.cs.algs4.Stack;

/**   
*    
* 项目名称:Algorithms4   
* 类名称:P1_3_05   
* 类描述:练习1.3.05,输入整数N,打印N的二进制表示
* 创建人:bbbdbbb   
* 创建时间:2020年2月1日 下午9:23:34   
* @version        
*/
public class P1_3_05 {
	public static void main(String[] args) {
		info(50);
		info(127);
		info(521);
		info(2020);
		info(2019);
	}
	
	public static void info(int N){
		System.out.print("N = "+N+" = ");
		Stack<Integer> stack = new Stack<>();
		while(N>0){
			stack.push(N%2);
			N=N/2;
		}
		for(int d : stack){
			System.out.print(d);
		}
		System.out.println();
	}

}

运行结果截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值