看板 TransCSI 關於我們 聯絡資訊
※ 引述《antirazin (今年是日星來台年~真嗨)》之銘言: : ※ 引述《ahongyeh (小葉子)》之銘言: : : #include<stdio.h> : : #define MAX 100 : : main(){ : : int a[MAX],max,min,ch,i=0,j; : : printf("Please input numbers(split in space): "); : : do{ : : scanf("%d",&a[i++]); : : scanf("%c",&ch); : : }while(ch==' '); : ^^^^^^^^^^^^^^^^ : 這邊看不懂為什麼要這樣寫 : 是說雖然MAX等於100,可是如果這樣寫可以隨意輸入任意個數的值, : 不用輸入100個值, : 你的意思是這樣嗎? : 不過看不懂ch的型態是int為什麼可以等於空字元=> ' ' 這邊是我的疏忽~~ch的型態應該要為char比較妥當~~ (但是使用int也無不可唷~~因為char和int只是ASCII的轉換而已~~) 我會有上面的讀入資訊~~純粹只是要用' '當作分開字元罷了~~ 如果最後接收不為' '~~則代表後方無資料~~ 所以只要從這些資料做比較即可~~ 如果你還不懂我的意思的話~~你用這個code讀入看看~~ for(i=0;i<MAX;i++){ scanf("%d",&a[i]); } 那麼你會發現~~如果讀入的資料個數不到MAX~~ 則程式會一直要求持續要求你輸入資料~~ : : max=a[0]; : : min=a[0]; : : for(j=0;j<i;j++){ : : if(a[j]>max) max=a[j]; : : if(a[j]<min) min=a[j]; : : } : : printf("The largest is %d, the least is %d.\n",max,min); : : system("pause"); : : } : : 剛剛run過一次~~應該是沒錯~~ : : 不過我發現在我考的時候有些小地方寫錯了... @@ 另外~~我再提供一個不用陣列來比較~~ 因為是直接比較~~所以可以節省記憶體(不用事先宣告陣列大小)~~ #include<stdio.h> main(){ int cpr,max=0,min=0,flag=0; char ch; printf("Please input numbers(split in space): "); do{ scanf("%d",&cpr); if(flag++==0) max=min=cpr; if(cpr>max) max=cpr; if(cpr<min) min=cpr; scanf("%c",&ch); }while(ch==' '); printf("The largest is %d, the least is %d.\n",max,min); system("pause"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.164.76.211
ahongyeh:分開字元ch~~你可以自行設定~~我是使用空白分離~~ 07/12 20:35
ahongyeh:另外~~我另寫的code的資料數量最多為2^16~~(應該是夠了) 07/12 20:43
ahongyeh:那行判斷改成if(flag==0) flag=max=min=cpr;就無數量限制 07/12 20:57
antirazin:感激不盡~>< 07/12 22:34