zoukankan      html  css  js  c++  java
  • 华为软件编程题:随机数按计数输出

      1 /* array_iterate.cpp
      2  * 输入一个由随机数组成的数列(数列中每个数均是大于0的整数,长度已知),和初始计数值m。
      3  * 从数列首位置开始计数,计数到m后,将数列该位置数值替换计数值m,
      4  * 并将数列该位置数值出列,然后从下一位置从新开始计数,直到数列所有数值出列为止。
      5  * 如果计数到达数列尾段,则返回数列首位置继续计数。
      6  */
      7 
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 
     12 void array_iterate(int len, int input_array[], int m, int output_array[])
     13 {
     14     int i = 0, j = 0; 
     15     int *flag_array = (int *)malloc(len * sizeof(int)); 
     16 
     17     //判断数组中的数是否已经被取出,初始化全置零,表示未被取出
     18     //最开始写的为:memset(flag_array, 0x00, sizeof(flag_array)); 
     19     //结果出错了,原因是函数传参过程中的指针降级
     20     //导致sizeof(flag_array)返回的是一个指针类型大小的的字节数
     21     memset(flag_array, 0x00, len * sizeof(int)); 
     22     
     23     for (i = 0, j = 0; i < len; i++)
     24     {
     25         while (m)
     26         {
     27             if (!flag_array[j])
     28             {
     29                 m--; 
     30             }
     31             if (j != len - 1)
     32             {
     33                 j++; 
     34             }
     35             else
     36             {
     37                 j = 0; 
     38             } 
     39         }
     40 
     41         if (j)
     42         {
     43             output_array[i] = input_array[j - 1]; 
     44             flag_array[j - 1] = 1; 
     45         }
     46         else
     47         {
     48             output_array[i] = input_array[len - 1]; 
     49             flag_array[len - 1] = 1; 
     50         }
     51 
     52         m = output_array[i]; 
     53     }
     54 }
     55 
     56 int main()
     57 {
     58     int len = 0, m = 0; 
     59     int i = 0, input_flag = 0; 
     60     
     61     //为了提高程序的鲁棒性,写了下面一段循环
     62     //此处关键是flushall()的使用,如果不使用
     63     //由于键盘输入缓存的存在使得程序陷入死循环
     64     //关于flush()参见我的下一篇博客
     65     while (!input_flag)
     66     {
     67         printf("请输入数列的长度:"); 
     68         input_flag = scanf("%d", &len); 
     69         if (!input_flag)
     70         {
     71             printf("输入有误,仅可输入数字!
    "); 
     72             flushall(); 
     73         }
     74     }
     75     
     76     int *input_array = (int *)malloc(len * sizeof(int)); 
     77     int *output_array = (int *)malloc(len * sizeof(int)); 
     78     
     79     //input_flag使用后要重置
     80     input_flag = 0; 
     81     while (!input_flag)
     82     {
     83         printf("请输入初始数列:");
     84         for (i = 0; i < len; i++)
     85         {
     86             input_flag = scanf("%d", &input_array[i]); 
     87             if (!input_flag)
     88             {
     89                 printf("输入有误,仅可输入数字!
    "); 
     90                 flushall(); 
     91                 break; 
     92             }
     93         }
     94     }
     95 
     96     //printf("请输入初始数列:"); 
     97     //for (i = 0; i < len; i++)
     98     //{
     99     //    scanf("%d", &input_array[i]); 
    100     //}
    101     
    102     //printf("请输入初始计数值:"); 
    103     //scanf("%d", &m); 
    104     
    105     input_flag = 0; 
    106     while (!input_flag)
    107     {
    108         printf("请输入初始计数值:"); 
    109         input_flag = scanf("%d", &m); 
    110         if (!input_flag)
    111         {
    112             printf("输入有误,仅可输入数字!
    "); 
    113             flushall(); 
    114         }
    115     }
    116 
    117     array_iterate(len, input_array, m, output_array); 
    118     printf("数值出列的顺序为:"); 
    119     for (i = 0; i < len; i++)
    120     {
    121         printf("%d ", output_array[i]); 
    122     }
    123     printf("
    "); 
    124     
    125     return 0; 
    126 }
    127 
    128 
    129 
    130 //以下是网上找到的代码,空间复杂度比我的小
    131 //使用到了循环链表,但是鲁棒性不好
    132 
    133 //#include <stdio.h>
    134 //#include <stdlib.h>
    135 //#include <string.h>
    136 //
    137 //typedef struct Node
    138 //{
    139 //    int num; 
    140 //    struct Node *next; 
    141 //}node; 
    142 //
    143 //node *creat(int len, int input_array[])
    144 //{
    145 //    node *h, *s, *p; 
    146 //    int i; 
    147 //    h = (node *)malloc(sizeof(node)); 
    148 //    h->num = input_array[0]; 
    149 //    p = h; 
    150 //    for (i = 1; i < len; i++)
    151 //    {
    152 //        s = (node *)malloc(sizeof(node)); 
    153 //        s->num = input_array[i]; 
    154 //        p->next = s; 
    155 //        p = s; 
    156 //    }
    157 //    p->next = h; 
    158 //
    159 //    return (h); 
    160 //}
    161 //
    162 //void array_iterate(int len, int input_array[], int m)
    163 //{
    164 //    node *q, *p, *s; 
    165 //    int i = 0, j = 0, k; 
    166 //    int output_array[4]; 
    167 //    p = creat(len, input_array); 
    168 //    while (p->next != p)
    169 //    {
    170 //        for (i = 1; i < m; i++)
    171 //        {
    172 //            q = p; 
    173 //            p = p->next; 
    174 //        }
    175 //        m = p->num; 
    176 //        //printf("%5d", m); 
    177 //
    178 //        output_array[j++] = m; 
    179 //
    180 //        s = p; 
    181 //        q->next = p->next; 
    182 //        p = p->next; 
    183 //        free(s); 
    184 //        s = NULL; 
    185 //    }
    186 //
    187 //    m = p->num; 
    188 //    //printf("%5d
    ", m); 
    189 //    output_array[j] = p->num; 
    190 //    k = j; 
    191 //    for (j = 0; j <= k; j++)
    192 //    {
    193 //        printf("%5d", output_array[j]); 
    194 //    }
    195 //}
    196 //
    197 //int main()
    198 //{
    199 //    int input_array[] = {3, 1, 2, 4}; 
    200 //    int len = 4; 
    201 //    int m = 7; 
    202 //    int output_array[4]; 
    203 //    array_iterate(len, input_array, m); 
    204 //}
  • 相关阅读:
    漫谈怎样学习操作系统原理
    二分图的最大匹配、完美匹配和匈牙利算法
    Web报表工具FineReport中JavaScript的使用
    Java Web -- Servlet(1) 必备知识
    xxxxxxclub系统模块分类
    经典排序算法——选择排序
    github+hexo+node.js搭建个人博客基本过程及遇到的问题
    自己做小项目的流程(慢慢完善)
    二分查找
    Eclipse中遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误
  • 原文地址:https://www.cnblogs.com/andrewlee0708/p/array_iterate.html
Copyright © 2011-2022 走看看