Java语言程序设计与数据结构(基础篇)课后练习题 第七章(三)

7.35

这个题,挺好理解也挺好想出对应代码方法,就是太繁琐,需要有耐心。。

package demo;
import java.util.*;
public class diqizhang {

public static void main(String[] args){
		String[] words = {"write","that","arrays"};
		guess(words);
}

public static void guess(String[] words){
		Scanner input = new Scanner(System.in);
		char c = 'y';
		while(c=='y'){
			play(words);
			System.out.print("Do you want to guess another word? Enter y or n >: ");
			c=input.next().charAt(0);
			while(c!='y'&&c!='n'){
				System.out.print("Do you want to guess another word? ENter y or n >:");
				c=input.next().charAt(0);
			}
		}
}

public static void play(String[] words){
		Scanner input = new Scanner(System.in);
		int l = (int)(Math.random()*words.length);
		String word = words[l];
		int len = word.length();
		char[] letters = new char[len];
		for(int i=0;i<len;i++)
			letters[i]='0';
		int lettersPointer = 0;
        int missCount = 0;
        int notGuessed = len;
        while(notGuessed>0)
        {
            System.out.print("(Guess) Enter a letter in word "+alter(word,letters)+" > ");
            char guessLetter = input.next().charAt(0);
            if(belongs(guessLetter,letters))
                System.out.println(guessLetter+" is already in the word");
            else if(!belongs(guessLetter,word))
            {
                System.out.println(guessLetter+" is not in the word");
                missCount++;
            }
            else
            {
                letters[lettersPointer]=guessLetter;
                lettersPointer++;
                notGuessed-=occurTime(guessLetter,word);
            }
        }
        System.out.println("The word is "+word+". You missed "+missCount+" time(s)");
}

public static String alter(String word , char[] letters){
	String r = "";
	int len = word.length();
	for(int i=0;i<len;i++){
		if(belongs(word.charAt(i),letters))
			r += word.charAt(i);
		else
			r += '*';
	}
	return r;
}

public static boolean belongs(char key,char[] letters){
        for (char letter : letters) {
            if (letter == key)
                return true;
        }
        return false;
}

public static boolean belongs(char key,String word){
        for(int i=0;i<word.length();i++){
            if(key==word.charAt(i))
                return true;
        }
        return false;
}

public static int occurTime(char key,String word){
        int count=0;
        for(int i=0;i<word.length();i++) {
            if (key == word.charAt(i))
                count++;
        }
        return count;
}

}

7.36

package demo;
import java.util.*;
public class diqizhang {

public static void main(String[] args){
		int[] queens = {0,1,2,3,4,5,6,7};
		while(conflict(queens))
			shake(queens);
		for(int i=0;i<8;i++){
			for(int j=0;j<queens[i];j++)
				System.out.print("| ");
			System.out.print("|Q");
			for(int j=queens[i]+1;j<8;j++)
				System.out.print("| ");
			System.out.println("|");
		}
}
public static boolean conflict(int[] queens){
	int[] plus = new int[8];
	int[] minus = new int[8];
	for(int i=0;i<8;i++){
		plus[i] = i+queens[i];
		minus[i] = i-queens[i];
	}
	for(int i=0;i<8;i++){
		if(occurTime(plus[i],plus)>1)
			return true;
	}
	for(int i=0;i<8;i++){
		if(occurTime(minus[i],minus)>1)
			return true;
	}
	return false;
}
public static int occurTime(int key,int[] set){
	int count=0;
	for(int value : set){
		if(key == value)
			count++;
	}
	return count;
}
public static void shake(int[] str){
	int len = str.length;
	for(int i=0;i<len;i++){
		int d = (int)(Math.random()*len);
		int tmp = str[i];
		str[i] = str[d];
		str[d] = tmp;
	}
}

}

7.37

package demo;
import java.util.*;
public class diqizhang {

public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of balls to drop: ");
		int balls = input.nextInt();
		System.out.print("Enter the number of slots in the bean machine: ");
		int numSlot = input.nextInt();
		int[] slots = new int[numSlot];
		for(int i=0;i<balls;i++)
			drop(numSlot,slots);
		for(int i=max(slots);i>=1;i--){
			for(int j=0;j<numSlot;j++){
				if(slots[j]<i)
					System.out.print(" ");
				else
					System.out.print("0");
			}
			System.out.println();
		}
}
public static  void drop(int numSlot , int[] slots){
		double b = 0;
		for(int i=0;i<numSlot-1;i++){
			if(Math.random()>0.5){
				b += 0.5;
				System.out.print('R');
			}else{
				b -= 0.5;
				System.out.print('L');
			}
		}
		System.out.println();
		if(numSlot%2==1)
			slots[(int)(numSlot/2+b)]++;
		else{
			if(b>0)
				slots[(int)(numSlot/2-0.5+b)]++;
			else
				slots[(int)(numSlot/2+b)]++;
		}
}
public static int max(int[] v){
		int max = v[0];
		for(int value : v){
			if(value > max)
				max = value;
		}
		return max;
}

}

第七章习题 完

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值