zoukankan      html  css  js  c++  java
  • 随机排座位(模板) 20.10.17

    思路

    		先给每个人定一个编号,然后用random_shuffle()这个函数随机生成一个序列。
    		不过此时每次生成的排位还是有一定规律的,但是如果一次运行多次随机生成排列函数的话,是不会重复的;
    		所以再用rand()%100随机生成一个次数,按照这个次数循环就很难会出现有重复的情况了!
    
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e6 + 10;
    int n;
    
    vector<int> temp;
    int a[50];
    int myrandom (int i) { return rand()%i;}
    void out(int x){
    	if(x == 1) printf("张三  	");
    	else if (x == 2 ) printf("唐三 	"); 
    	else if (x == 3 ) printf("睿总   	"); 
    	……………………
    	else if (x == 49 ) printf("李小明 	" ); 
    }//先给名字编号
    
    void print(vector<int> &temp){
    	    for (int i = 0; i < 7; i++)
    	    {
    	    	for(int j = 0; j < 7; j ++)
    	        	out(temp[i * 7 + j]);
    				cout << endl;
    	    }
    	    cout << endl << endl;
    }
    void randperm(vector<int> &temp, int Num) 
    {
        for (int i = 1; i <= Num; i++)	temp.push_back(i);
        random_shuffle(temp.begin(), temp.end());
        random_shuffle ( temp.begin(), temp.end(), myrandom);//这里是模板
    }
    int main(){
    	freopen("ttt.in", "r", stdin);
    	freopen("ttt.out", "w", stdout);
    	
      	srand (time(NULL));
     	int n = rand()%100;  	
     	
    	while(n){
    		vector<int> temp;
    		randperm(temp, 49);//随机生成数列
    //		print(temp);
    //	    for (vector<int>::iterator it=temp.begin(); it!=temp.end(); ++it)//这里会自动搜寻到是从第几个开始 
    //    		cout << *it<< ' ' ; //验证temp数组没有重复! 
    //	    cout << endl;
    //    	for(int i = 0; i < 49; i ++) cout << temp[i]<< " " ;//temp排好序之后下标是从0开始的! 
    //	    cout << endl;
    //	    for(int i = 0; i < 7; i ++){
    //	    	for(int j = 0; j < 7; j ++)
    //	    		cout << temp[i * 7 + j] << ' ';
    //	    	cout << endl;
    //	    }
    //	    cout << endl;		//方格状验证是否所有1-49的数都生成了
    
    //		for(int i = 1; i <= 49; i ++) cout << a[i] << " " ; 
    
    //	    cout << endl << endl;
    		n --;
    		if(!n)	print(temp); 
    	}
    	
    	return 0;
    }
    
    //
    //std::srand ( unsigned ( std::time(0) ) );
    //  std::vector<int> myvector;
    //
    //  // set some values:
    //  for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    //
    //  // using built-in random generator:
    //  std::random_shuffle ( myvector.begin(), myvector.end() );
    //
    //  // using myrandom:
    //  std::random_shuffle ( myvector.begin(), myvector.end(), myrandom);
    //
    //  // print out content:
    //  std::cout << "myvector contains:";
    //  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    //    std::cout << ' ' << *it;
    //
    //  std::cout << '
    ';
    
    

    总结:
    学到了很多关于和随机生成有关的函数,比如rand()%n,srand (time(NULL));,
    还有 random_shuffle ( temp.begin(), temp.end(), myrandom); 这个给数组元素随机排序的函数

    以下是本函数用到的函数模板:

    srand()

    /* srand example */
    #include <stdio.h>      /* printf, NULL */
    #include <stdlib.h>     /* srand, rand */
    #include <time.h>       /* time */
    
    int main ()
    {
      printf ("First number: %d
    ", rand()%100);
      srand (time(NULL));
      printf ("Random number: %d
    ", rand()%100);
      srand (1);
      printf ("Again the first number: %d
    ", rand()%100);
    
      return 0;
    }
    

    random_shuffle example

    // random_shuffle example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::random_shuffle
    #include <vector>       // std::vector
    #include <ctime>        // std::time
    #include <cstdlib>      // std::rand, std::srand
    
    // random generator function:
    int myrandom (int i) { return std::rand()%i;}
    
    int main () {
      std::srand ( unsigned ( std::time(0) ) );
      std::vector<int> myvector;
    
      // set some values:
      for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
    
      // using built-in random generator:
      std::random_shuffle ( myvector.begin(), myvector.end() );
    
      // using myrandom:
      std::random_shuffle ( myvector.begin(), myvector.end(), myrandom);
    
      // print out content:
      std::cout << "myvector contains:";
      for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
    
      std::cout << '
    ';
    
      return 0;
    }
    

    rand()

    /* rand example: guess the number */
    #include <stdio.h>      /* printf, scanf, puts, NULL */
    #include <stdlib.h>     /* srand, rand */
    #include <time.h>       /* time */
    
    int main ()
    {
      int iSecret, iGuess;
    
      /* initialize random seed: */
      srand (time(NULL));
    
      /* generate secret number between 1 and 10: */
      iSecret = rand() % 10 + 1;
    
      do {
        printf ("Guess the number (1 to 10): ");
        scanf ("%d",&iGuess);
        if (iSecret<iGuess) puts ("The secret number is lower");
        else if (iSecret>iGuess) puts ("The secret number is higher");
      } while (iSecret!=iGuess);
    
      puts ("Congratulations!");
      return 0;
    }
    
  • 相关阅读:
    HDU 2460 Network 傻逼Tarjan
    HTTP状态码
    Spring Tool Suite(STS)安装
    Manajro17配置
    VsCode搭建Java开发环境
    《从零开始搭建游戏服务器》项目管理工具Maven
    Debian9安装MariaDB
    Intellij从无到有创建项目
    PostgreSQL 二进制安装
    Intellij IDEA创建项目
  • 原文地址:https://www.cnblogs.com/yuanyulin/p/14026739.html
Copyright © 2011-2022 走看看