zoukankan      html  css  js  c++  java
  • [算法]随机数

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>              //代码功能,随机生成n个随机数,并按升序排序
    
    int main(void)
    {
        int i,j,n,t;
        srand( (unsigned)time( NULL ) );
        
        n = rand() % 100;
        int arr[n];
        
        printf("Ten random numbers from 0 to 99
    
    ");
        printf("共生成%d个随机数,分别是:",n);
        for(i=0; i<n; i++){
            arr[i]=rand() % 100;
            printf("%4d",arr[i]);
        }
        
        printf("从小到大排列为:
    ");
        
        for(i=0; i<n; i++){
            for(j=0;j<n;j++){
                if(arr[i]>arr[j]){
                    t=arr[i];
                    arr[i]=arr[j];
                    arr[j]=t;
                }
    
            }
        }
        
        for(i=0;i<n;i++)
            printf("%4d",arr[i]);
    
                return 0;
    }

    这里生成随机数的关键代码

    
    
    #include <time.h>
    srand( (unsigned)time( NULL ) );
        
    n = rand() % 100;

      上述代码中,排序代码也是一个key,这里是用二维数组来遍历每一个元素,及其与其他元素的关系,不断把符合条件的元素与原始数组的第0,1,2...个元素交换。当然,还有其他的排序代码,复杂程度各有不同,以后会更新。

  • 相关阅读:
    数组
    2017.3.20for
    PHP基础2
    php基础1
    触发器
    SQL储存过程
    范式
    时间戳
    主键和外键
    15 大图轮播
  • 原文地址:https://www.cnblogs.com/vilogy/p/6828390.html
Copyright © 2011-2022 走看看