zoukankan      html  css  js  c++  java
  • Java 7.21 游戏:豆机(C++&Java)

     

     PS:

             难点在于,随机之后的分隔,理解就很容易了

    注意:槽的奇偶情况

    C++: 

    #include<iostream>
    #include<ctime>
    #include<string>
    using namespace std;
    class Machine {
    public:
    	Machine(int,int);
    	void run();
    	void show_machine();
    	void show_road();
    private:
    	int ball;
    	int slot;
    	int *count;
    	string *road;
    };
    Machine::Machine(int b, int s) : ball(b),slot(s) {
    	count = new int[s];
    	road = new string[b];
    	for (int i = 0; i < slot; i++)
    		count[i] = 0;
    }
    void Machine::run() {
    	double mid;
    	srand((unsigned int)time(0));       //随机
    	for (int i = 0; i < ball; i++) {
    		mid = ((double)slot - 1) / 2;   //取中间值(小技巧:这里刚好使得slot奇偶都符合要求)
    		for (int j = 0; j < slot - 1; j++) { 
    			if (rand() % 2) {           //右移
    				mid += 0.5;
    				road[i] += 'R';
    			}
    			else {                      //左移
    				mid -= 0.5;
    				road[i] += 'L';
    			}
    		}
    		count[(int)mid]++;              //该列count++
    	}
    }
    void Machine::show_road() {              //显示每个球的路径
    	for (int i = 0; i < ball; i++)
    		cout << road[i] << endl;
    }
    void Machine::show_machine() {           //显示机器
    	int flag;
    	string str;
    	for (int i = ball; i > 0; i--) {     //模拟二维数组,当count[j]超过i,证明此行此列存在值 
    		flag = 0;
    		str.clear();
    		for (int j = 0; j < slot; j++) {
    			if (count[j] >= i) {
    				str += "O";
    				flag = 1;
    			}
    			else
    				str+=" ";
    		}
    		if (flag)                         //使用str以及flag为了去除多余的空行
    			cout << str << endl;
    	}
    }
    int main() {
    	int ball, slot;
    	cout << "Enter the number of balls to drop : ";
    	cin >> ball;
    	cout << "Enter the number of slots in the bean machine : ";
    	cin >> slot;
    	cout << endl;
    	Machine My(ball, slot);
    	My.run();
    	My.show_road();
    	cout << endl;
    	My.show_machine();
    	return 0;
    }

    Java:

               

    import java.util.Scanner;
    public class BeanMachine {
    	int ball,slot;
    	int[] slots;
    	String []road;
    	
        public BeanMachine(int b,int s) {
        	ball=b;slot=s;
        	slots=new int[s];
        	road=new String[b];
        }
        
        public void run(){
        	double mid;
        	for(int i=0;i<ball;i++){
        		mid=((double)slot-1)/2;
        		road[i]=new String();
        		for(int j=0;j<slot-1;j++){
        			if(Math.random()>0.5){
        				mid+=0.5;
        				road[i]+="R";
        			}
        			else{
        				mid-=0.5;
        				road[i]+="L";
        			}		
        		}
        		slots[(int)mid]++;
        	}
        }
        
        public void ShowRoad(){
        	for (int i = 0; i < ball; i++)
        		System.out.println(road[i]);
        }
        
        public void ShowMachine(){
        	String str;
        	int flag;
    		for (int i = ball; i > 0; i--) {     //模拟二维数组,当count[j]超过i,证明此行此列存在值 
    			flag = 0;
    			str="";
    			for (int j = 0; j < slot; j++) {
    				if (slots[j] >= i) {
    					str += "O";
    					flag = 1;
    				}
    				else
    					str+=" ";
    			}
    			if (flag==1)                         //使用str以及flag为了去除多余的空行
    				System.out.println(str);
    		}
        }
        
        public static void main(String[] args) {
        	int ball,solt;
        	Scanner Input=new Scanner(System.in);
        	System.out.print("Enter the number of balls to drop : ");
        	ball=Input.nextInt();
        	System.out.print("Enter the number of slots in the bean machine : ");
        	solt=Input.nextInt();
        	BeanMachine My=new BeanMachine(ball,solt);
        	My.run();
        	My.ShowRoad();
        	My.ShowMachine();
        	
        }
    }
    
  • 相关阅读:
    postman获取请求响应值
    http常用状态码说明
    postman的Testing examples(测试脚本示例)
    JMeter 如何把上一个请求的结果作为下一个请求的参数 —— 使用正则提取器
    一个绿色版的正则表达式测试工具
    让TinyXML保存文件为UTF-8格式
    TinyXml2 的使用
    Apache Thrift
    解决sqlserver的sql脚本内存不足问题
    一个表的两个字段具有相同的类型。如何仅用SQL语句交换这两列的数据?
  • 原文地址:https://www.cnblogs.com/F-itachi/p/9974346.html
Copyright © 2011-2022 走看看