zoukankan      html  css  js  c++  java
  • sicily 6573. 奇偶分开

    Description
    将输入的一串数中的负数延迟输出,从而分离非负数和负数 例如输入的数是: P1 N1 P2 N2 那么你处理完之后的输出应该是: P1 P2 N1 N2 注意: 非负数之间的输出顺序和输入顺序相同,负数之间的输出顺序和输入顺序也是要求相同的。

    Input
    输入有多个case: 每个case的输入有1行。每一行的第一个整数是N,代表这个输入有N个数需要处理,接下来有N个整数。

    Output
    对于每个case输出一行,这1行包括N个整数,整数之间以空格隔开。 输入以EOF结束

    其实是从实验手册上改过来的题目,原题是奇数偶数分开输出,TA改成了非负数和负数分开输出,但是没改名字……于是名不对题了

    有4种做法

    ① 扫描两遍数组,分别打印出非负元素与负元素,AC用时0.01sec

    View Code
     1 #include<stdio.h> 
     2 #define MAX 10001
     3 void printBySign ( const int array[], int size );
     4 
     5 int main()
     6 {
     7     int n;
     8     int i;
     9     int array[MAX] = {0};
    10     
    11     while ( scanf( "%d", &n ) != EOF )
    12     {
    13         for ( i = 0; i < n; i++)
    14         {
    15             scanf( "%d", &array[i] );
    16         }
    17         
    18         printBySign( array, n );
    19     }
    20     
    21     return 0;
    22 }
    23 
    24 void printBySign( const int array[], int size )
    25 {
    26     int i;
    27     int negative = 0;
    28     int nonnegative = 0;
    29     
    30     int printspace = 0;
    31     
    32     for ( i = 0; i < size; i++ )
    33     {
    34         if ( array[i] >= 0 )
    35         {
    36             printf( "%d", array[i] );
    37             
    38             if ( i != size - 1 )
    39             {
    40                 printf( " " );
    41             }
    42             else
    43             {
    44                 printspace = 1;
    45             }
    46             
    47             nonnegative++;
    48         }
    49     }
    50     
    51     for( i = 0; i < size; i++ )
    52     {
    53         if ( array[i] < 0 )
    54         {
    55             if ( printspace == 1 )
    56             {
    57                 printf( " " );
    58                 printspace = 2;
    59             }
    60             
    61             printf( "%d", array[i] );
    62             negative++;
    63             
    64             if ( negative + nonnegative != size )
    65             {
    66                 printf( " " );
    67             }
    68         }
    69     }
    70     
    71     printf( "\n" );
    72     
    73     return;
    74 }

    ② 新开两个数组,分别用于存储非负元素与负元素,再打印出这两个新数组,AC用时0.02sec;

    View Code
     1 #include<stdio.h> 
     2 void moveBySign ( int array[], int size );
     3 void printArray( const int array[], int size );
     4 
     5 int main()
     6 {
     7     int n;
     8     int i;
     9     int array[10001] = {0};
    10     
    11     while ( scanf( "%d", &n ) != EOF )
    12     {
    13         for ( i = 0; i < n; i++)
    14         {
    15             scanf( "%d", &array[i] );
    16         }
    17         moveBySign( array, n );
    18     }
    19     
    20     return 0;
    21 }
    22 
    23 void moveBySign( int array[], int size )
    24 {
    25     int i = 0, j = 0, k = 0;
    26     int negative[10001] = {0};
    27     int positive[10001] = {0};
    28     
    29     for ( i = 0; i < size; i++ )
    30     {
    31         if ( array[i] < 0 )
    32         {
    33             negative[j] = array[i];
    34             j++;
    35         }
    36         else
    37         {
    38             positive[k] = array[i];
    39             k++;
    40         }
    41     }
    42     
    43     if ( k >= 1 )
    44     {
    45         printArray( positive, k );
    46     }
    47     
    48     if ( k >= 1 && j >= 1)
    49     {
    50         printf( " " );
    51     }
    52     
    53     if ( j >= 1 )
    54     {
    55         printArray( negative, j );
    56     }
    57     printf( "\n" );
    58     
    59     return;
    60 }
    61 
    62 void printArray( const int array[], int size )
    63 {
    64     int i;
    65     
    66     printf( "%d", array[0] );
    67     
    68     for ( i = 1; i < size; i++ )
    69     {
    70         printf( " %d", array[i] );
    71     } 
    72     
    73     return;
    74 }

    ③ 优化过的②,先扫描一遍,打印出非负元素,并将负元素存储在新开的数组里,然后再打印出这个负元素数组,AC用时0.01sec

    View Code
     1 #include<stdio.h> 
     2 void printBySign ( int array[], int size );
     3 void printArray( const int array[], int size );
     4 
     5 int main()
     6 {
     7     int n;
     8     int i;
     9     int array[10001] = {0};
    10     
    11     while ( scanf( "%d", &n ) != EOF )
    12     {
    13         for( i = 0; i < n; i++)
    14         {
    15             scanf( "%d", &array[i] );
    16         }
    17         
    18         printBySign( array, n );
    19     }
    20     
    21     return 0;
    22 }
    23 
    24 void printBySign( int array[], int size )
    25 {
    26     int i = 0, j = 0, k = 0;
    27     int negative[10001] = {0}; 
    28     int printspace = 0;
    29     
    30     for ( i = 0; i < size; i++ )
    31     {
    32         if( array[i] >= 0 )
    33         {
    34             printf("%d", array[i]);
    35             
    36             if( i != size - 1 )
    37             {
    38                 printf(" ");
    39             }
    40             else
    41             {
    42                 printspace = 1;
    43             }
    44         }
    45         else
    46         {
    47             negative[j] = array[i];
    48             j++;
    49         }
    50     }
    51     
    52     if ( j >= 1 )
    53     {
    54         if ( printspace == 1 )
    55         {
    56             printf(" ");
    57             printspace == 2;
    58         }
    59         
    60         printArray( negative, j );
    61     }
    62     
    63     printf("\n");
    64     
    65     return;
    66 }
    67 
    68 void printArray( const int array[], int size )
    69 {
    70     int i;
    71     
    72     printf( "%d", array[0] );
    73     
    74     for ( i = 1; i < size; i++ )
    75     {
    76         printf( " %d", array[i] );
    77     } 
    78     
    79     return;
    80 }

    ④ 使用类似冒泡排序的方法,只不过修改了元素交换条件。对原数组进行修改,再打印出来,AC用时0.12sec

    View Code
     1 #include<stdio.h> 
     2 #define MAX 10001
     3 void printBySign ( int array[], int size );
     4 void swapInArray( int array[], int first, int second );
     5 void printArray( const int array[], int size );
     6 
     7 int main()
     8 {
     9     int n;
    10     int i;
    11     int array[MAX] = {0};
    12     
    13     while ( scanf( "%d", &n ) != EOF )
    14     {
    15         for ( i = 0; i < n; i++)
    16         {
    17             scanf( "%d", &array[i] );
    18         }
    19         
    20         printBySign( array, n );
    21     }
    22     
    23     return 0;
    24 }
    25 
    26 void printBySign( int array[], int size )
    27 {
    28     int i, j;
    29     
    30     for ( i = 1; i < size; i++ )
    31     {
    32         for ( j = 0; j < size - 1; j++ )
    33         {
    34             if ( array[j] < 0 && array[j+1] >= 0 )
    35             {
    36                 swapInArray( array, j, j+1 );
    37             }
    38         }
    39     }
    40     
    41     printArray( array, size );
    42     
    43     return;
    44 }
    45 
    46 void swapInArray( int array[], int first, int second )
    47 {
    48     int temp;
    49     
    50     temp = array[first];
    51     array[first] = array[second];
    52     array[second] = temp;
    53     
    54     return;
    55 }
    56 
    57 void printArray( const int array[], int size )
    58 {
    59     int i;
    60     
    61     printf( "%d", array[0] );
    62     
    63     for ( i = 1; i < size; i++ )
    64     {
    65         printf( " %d", array[i] );
    66     } 
    67     
    68     printf("\n");
    69     
    70     return;
    71 }
  • 相关阅读:
    JAVA 字符处理
    android:visibility 隐藏 可见 不可见
    Android中 int 和 String 互相转换的多种方法
    日期月和日补零
    【程序】程序报错 error:-1610149839 等大负数
    【IE】将IE11改为64位
    【linux】CentOS网络配置
    【linux】CentOS虚拟机eth0 提示Device does not seem to be present
    【SUSE】关闭防火墙
    【走马观花】十月八日通州雾
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2813517.html
Copyright © 2011-2022 走看看