--- title: 23-5:重複的倒序 lang: zh-tw tags: DICE Python --- 23-5:重複的倒序 === > [name=Chinglin-K] --- 目錄:[Dice 程式教學-Python完整版](https://hackmd.io/@Chinglin-K/Dice-menu) 上一篇:[23-4:倒序輸出](https://hackmd.io/@Chinglin-K/Dice-23-4) 下一篇:[24-4:窮舉法-百錢百雞問題](https://hackmd.io/@Chinglin-K/Dice-24-4) --- ## 題目 寫一個程式,從標準輸入取得一系列整數,直到輸入負數停止,輸入的資料最多不超過10000個,並反向輸出,如輸出範例的反向輸出範例說明。 以上的工作可以重複執行,直到文件結束位止。此種類型的結束條件是直接手動輸入結束符Ctrl+z。 輸入範例: 1 2 3 4 5 -1 9 8 7 6 5 -1 輸出範例: 5 4 3 2 1(反向輸出範例) 5 6 7 8 9 --- ## Python程式碼 ```Python= try: while True: array=[] while True: array=list(map(int,input().split())) if(array[len(array)-1]<0): del array[-1] break array.reverse() for i in range(len(array)): print(array[i],end=" ") print("") except: pass ``` --- ## C++程式碼 筆者在作答時Python測資有誤,需使用C++才能拿滿分。 ```C++= #include <iostream> using namespace std; int main() { while(!cin.eof()){ int i=0,n=0; int num[10000]={0}; while(cin>>num[n]){ if(num[n]<0){ break; } n++; } for(i=n-1;i>=0;i--){ cout << num[i] << " "; } cout<<endl; } return 0; } ``` --- ## 輸出 ```Python= ``` --- 目錄:[Dice 程式教學-Python完整版](https://hackmd.io/@Chinglin-K/Dice-menu) 上一篇:[23-4:倒序輸出](https://hackmd.io/@Chinglin-K/Dice-23-4) 下一篇:[24-4:窮舉法-百錢百雞問題](https://hackmd.io/@Chinglin-K/Dice-24-4) --- :::info 「盡多少本分,得多少本事」😊 ::: --- {%hackmd i1nMRrZcTFmTvoF897K9zg %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up