Randomize Arrays and Joseph Rings

Source: Internet
Author: User

1. Random array problems

is to order the existing arrays in random order, so that there is no regularity;

(1), Code implementation

#include <stdio.h> #include <time.h> #include <stdlib.h>void showarray (int *a, int  count); void random_1 (Int *a, int count); Void random_1 (Int *a, int  count) {    int i;    int tmp;     Int index;    srand (Time (NULL));     for (i = count;  i > 0; i--) {        index = rand ()%i ;        tmp = a[index];         a[index] = a[i-1];        a[i-1] = tmp ;     }}void showarray (int *a, int count) {    int  i;    for (i = 0; i < count; i++) {         printf ("%d ",  a[i]);     }    printf ("\ n");} Int main (void) {    int a[] = {4, 6, 8, 2, 0,  7, 1,};    int count = sizeof (a)/sizeof (int);     Showarray (A, count);     random_1 (A, count);     showarray (A,  count);     return 0;}

(2), results

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/1F/wKiom1jUABDwG_omAABd9nOXOoQ672.png-wh_500x0-wm_ 3-wmp_4-s_3084060741.png "title=" Qq20170324010346.png "alt=" Wkiom1juabdwg_omaabd9noxooq672.png-wh_50 "/>


2. Joseph Ring Question

M elements, the nth element out of the loop, starting from the start of the first number can be;

(1), Code implementation

#include <stdio.h> #include <malloc.h>void yusf (char **str, int count, int  doom, int start) {    int *person;    int i;     int pre = start-2;    int cur =  Start-1;    int alive = count;    int doomnumber  = 0;    if (start == 1) {         pre = count-1;    }    person =  (int *) malloc (sizeof (int)  * count);     for (I = 0; i < count ;  i++) {        person[i] =  (i+1)%count;  // Loop array     }    for (; alive > 0; cur =  Person[cur]) {  &Nbsp;     if (++doomnumber >= doom) {             printf ("%s-> out ring \ n",  str[cur]);             alive--;             doomnumber = 0;            person[ pre] = person[cur]; //of the pre when the ring is out         }else{             pre = cur;         }    }}int main (void) {    char *str[ ] = {"Li",  "Umaji",  "Zhang San",  "John Doe",  "Harry",  "Liu Liu",  "Hang Seven",  "Zhu Eight",  "Yang Jiu"};     int count = sizeof (str)/sizeof (char *);     int  doom;&nbsp //    int start;  //from the first few people     scanf ("%d%d",  &doom, &start);     if (doom > count | |  doom <= 0 | |  start > count| |  start <= 0) {        return;     }&NBSP;&NBSP;&NBSP;&NBSP;YUSF (str, count, doom, start);  //count elements, Doom the number of doom, starting from the beginning of the first;     return 0;}

(2), results

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/1F/wKiom1jUAbHAK8NNAABOK-vbv9Q866.png-wh_500x0-wm_ 3-wmp_4-s_3807994968.png "title=" Qq20170324011040.png "alt=" Wkiom1juabhak8nnaabok-vbv9q866.png-wh_50 "/>



This article is from the "wait0804" blog, make sure to keep this source http://wait0804.blog.51cto.com/11586096/1909852

Randomize Arrays and Joseph Rings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.