PTA7-3 求最大值及其下標(20 分)
阿新 • • 發佈:2019-01-01
7-3 求最大值及其下標(20 分)
本題要求編寫程式,找出給定的n個數中的最大值及其對應的最小下標(下標從0開始)。
輸入格式:
輸入在第一行中給出一個正整數n(1<n≤10)。第二行輸入n個整數,用空格分開。
輸出格式:
在一行中輸出最大值及最大值的最小下標,中間用一個空格分開。
關鍵詞:陣列迴圈讀取資料、迴圈遍歷、比較大小、最大值的修改;<textarea readonly ="readonly" name ="code" class="c++">#include <stdio.h> int main() { int n; scanf("%d",&n); int a[10]={0}; int i; int pos=0;; for(i=0;i<n;i++){ scanf("%d",&a[i]); } int max=a[0]; for(i=0;i<n;i++){ if(a[i]>max) { max=a[i];pos=i; } } printf("%d %d\n",max,pos); return 0; }</textarea>