zoukankan      html  css  js  c++  java
  • sicily 1341. 明明的随机数

    Description
    明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。

    Input
    输入包含多个测试数据。
    每个测试数据有2行,第1行为1个正整数,表示所生成的随机数的个数N,第2行有N个用空格隔开的正整数,为所产生的随机数。

    Output
    对每个测试数据输出2行。第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。

    先用选择排序进行排序,然后开一个新数组,把不重复的数字填进去,达到去重的目的,再输出。

    View Code
      1 #include<stdio.h>
      2 #define MAX 500
      3 void selectionSort( int array[], int size );
      4 void swapInArray( int array[], int first, int second );
      5 void filter( const int array[], int size );
      6 void printArray( const int array[], int size );
      7 
      8 int main()
      9 {
     10     int array[MAX] = {0};
     11     int n;
     12     int i;
     13     
     14     while ( scanf( "%d", &n ) != EOF )
     15     {
     16         
     17         for ( i = 0; i < n; i++ )
     18         {
     19             scanf( "%d", &array[i] );
     20         }
     21         
     22         selectionSort( array, n );
     23         
     24         filter( array, n );
     25         
     26     }
     27         return 0;
     28 }
     29 
     30 void selectionSort( int array[], int size )
     31 {
     32     int i, j;
     33     int min;
     34     
     35     for ( i = 0; i < size - 1; i++ )
     36     {
     37         min = i;
     38         for ( j = i + 1 ; j < size; j++ )
     39         {
     40             if ( array[j] < array[min] )
     41             {
     42                 min = j;
     43             }
     44         }
     45         
     46         if ( i != min )
     47         {
     48             swapInArray( array, i, min );
     49         }
     50     }
     51     
     52     return;
     53 }
     54 
     55 void swapInArray( int array[], int first, int second )
     56 {
     57     int temp;
     58     
     59     temp = array[first];
     60     array[first] = array[second];
     61     array[second] = temp;
     62     
     63     return;
     64 }
     65 
     66 void filter( const int array[], int size )
     67 {
     68     int unique = 0;
     69     int i;
     70     int filteredArray[MAX] = {0};
     71     
     72     filteredArray[unique] = array[0];
     73     
     74     for ( i = 1; i < size; i++ )
     75     {
     76         if ( array[i] != filteredArray[unique] )
     77         {
     78             unique++;
     79             filteredArray[unique] = array[i];
     80         }   
     81     }
     82     
     83     unique++;
     84     
     85     printf( "%d\n", unique );
     86     
     87     printArray( filteredArray, unique );
     88     
     89     return;
     90 }
     91 
     92 void printArray( const int array[], int size )
     93 {
     94     int i;
     95     
     96     printf( "%d", array[0] );
     97     
     98     for ( i = 1; i < size; i++ )
     99     {
    100         printf( " %d", array[i] );
    101     } 
    102     
    103     printf("\n");
    104     
    105     return;
    106 }
  • 相关阅读:
    springboot的@EnableAutoConfiguration起作用的原理
    springboot加载bean过程探索
    dubbo源码阅读笔记-如何引用远程服务,变成invoker
    HashMap如何实现序列化
    如果处理缓存失效从数据库加载数据
    redis设计原则
    redis相关运维命令
    spring的compentScan注解扫描类机制
    全文检索技术
    前端设计网站
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2813505.html
Copyright © 2011-2022 走看看