倒序--逆序=3 rwkj 1271

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   io   

 

C語言:遞迴函式3(數組倒序) 時間限制(普通/Java):1000MS/3000MS          運行記憶體限制:65536KByte
總提交:786            測試通過:347

描述

 

輸入多個整數,以0結束,將這些整數逆序後輸出。

要求:使用遞迴函式將數組倒序,在main中調用遞迴函式。

 

輸入

多個整數,最後為0。

輸出

逆序後的這些整數。

範例輸入

1 2 5 4 0
1 2 3 4 5 6 7 8 9 0

範例輸出

4 5 2 1
9 8 7 6 5 4 3 2 1

提示

 

注意:每個數後面有一個空格。

 

 

 

 

 

#include <stdio.h>void f(int a[],int n){    int i,t;    for ( i=0; i<n/2; i++)     {   t=a[i]; a[i]=a[n-1-i]; a[n-1-i]=t;    }}int main(){    int a[100],n,i;    while (scanf("%d",&a[0])!=EOF)    {        n=1;        while(  scanf("%d",&a[n]) )        {    if (a[n]==0) break;            n++;        }           f(a,n);        for (i=0; i<n; i++) printf("%d ",a[i]);        printf("\n");    }} 
View Code

#include <stdio.h>
void f(int a[],int n)
{ int i,t;
for ( i=0; i<n/2; i++)
{ t=a[i]; a[i]=a[n-1-i]; a[n-1-i]=t; }
}
int main()
{ int a[100],n,i;
while (scanf("%d",&a[0])!=EOF)
{
n=1;
while( scanf("%d",&a[n]) )
{ if (a[n]==0) break;
n++;
}
f(a,n);
for (i=0; i<n; i++) printf("%d ",a[i]);
printf("\n");
}
}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.