|
发表于 2019-2-22 10:46:49
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- void random(int array[] , const int n)
- {
- int c , k , m ;
- bool f ;
- srand(time(0)) ;
- for(k = 0 ; k < n ; k ++) array[k] = 0 ;
- m = 0 ;
- while(m < n) {
- c = rand() % n + 1 ;
- f = true ;
- if (m) {
- for(k = 0 ; k < m ; k ++) {
- if (array[k] == c) {
- f = false ;
- break ;
- }
- }
- }
- if (f) array[m ++] = c ;
- }
- }
- void getpass(char pass[] , const int n , const int k)
- {
- int array[26] , b , c ;
- for(c = 0 ; c <= n ; c ++) pass[c] = '\0' ;
- random(array , 26) ;
- b = 0 ;
- c = 0 ;
- while(c < n) {
- if(b == k) b = 0 ;
- pass[c ++] = 'a' + array[b ++] - 1 ;
- }
- }
- main(void)
- {
- char pass[100] ;
- int k , n ;
- for(;;) {
- printf("input n , k : ") ;
- scanf("%d %d" , & n , & k) ;
- if(k > 1 && n > 1 && k <= n) {
- getpass(pass , n , k) ;
- printf("%s\n" , pass) ;
- } else {
- printf("Error : n , k must be greater than 1 and n must be no less than k\n") ;
- }
- }
- }
复制代码 |
|