2014-08-30 52 views
-4

我問這個問題時感覺相當「愚蠢」,但是如果有人能告訴我修改輸入結果的方法出現在命令窗口中。如何在命令窗口上修改輸入外觀

實施例:

欲5個號碼排序(1,3,4,7,5)在最小到最大的順序和命令窗口中的結果必須是:

input: 1 3 4 7 5 /* 1 line input */ 
output: 1 3 4 5 7 /* 1 line output */ 

編輯: 這裏是我的代碼

for (i = 0; i < 5; i++) 
{ 
    scanf("%d ", &array[i]); 
} 

如果我用這個代碼在命令窗口中的結果必然是:

1 
3 
4 
7 
5 

但我想只有1行中的所有輸入的號碼:

1 3 4 7 5 

那麼我有我的代碼呢?

+1

請選擇一種語言 - C或C++,但不能同時使用這兩種語言。它們是不同的,適當的答案將會有根本的不同。既然你提到你是C新手,我會假設你需要C標籤而不是C++標籤。 (請注意,第一條評論使用C++標記爲您提供了一個可行的(儘管不必要的冗長)答案。) – 2014-08-30 16:18:08

+0

感謝您使用c語言編寫的響應buti'm編程,因此沒有「cin」和「cout」。讓我解釋更多。我知道我的問題很模糊。 – MrBlue 2014-08-30 16:19:25

+0

更改爲'scanf(「%d」,&array [i]);'您可以在一行中輸入而不用更改。 – BLUEPIXY 2014-08-30 16:27:38

回答

0

關於您編輯的問題,只需將"%d "替換爲"%d"即可。

0
#include <stdio.h> 

#define N 5 

int main(void){ 
    int i, j, array[N]; 

    printf("Please enter the %d numbers.\n", N); 
    printf("input : "); 
    for(i=0;i<N;++i){ 
     scanf("%d", &array[i]); 
     if(i!=0){ 
      for(j=i;j>0 && array[j-1] > array[j];--j){ 
       //swap array[j] and array[j-1] 
       int tmp = array[j]; 
       array[j] = array[j-1]; 
       array[j-1] = tmp; 
      } 
     } 
    } 
    printf("output : "); 
    for(i=0;i<N;++i){ 
     if(i!=0) 
      putchar(' '); 
     printf("%d", array[i]); 
    } 
    putchar('\n'); 

    return 0; 
}