![](/img/trans.png)
[英]In c++, how do you get the input of a string, float and integer from 1 line?
[英]How do you get the largest integer from a set of numbers in c++?
如果您有一組數字,例如a = 4,b = 5,c = 8。 有沒有辦法讓程序打印出最大的價值。 例如,我的所有值都輸出為4,5,8。 如何獲得最大輸出值的程序? (我正在使用c ++)。 我使用了if語句,但是我覺得有一種更短的方法。 在Google上,我一直在尋找INT_MAX,但是那不是在int類型中顯示最大的數字嗎?
這是代碼的一部分
int a = (rand()%6)+1;
int b = (rand()%6)+1;
int c = (rand()%6)+1;
cout << int a << int b << int c << endl; //I'm trying to get it to display the largest int out of this group
我委托了if語句,試圖找到一種更好的方法來顯示組中的最大對象。
如何獲得最大輸出值的程序?
此過程分為兩個步驟。
如果允許使用標准庫中的函數,則可以使用std::max
來計算thee數的最大值。 如果不是,則必須自己寫一個。 假設您可以使用std::max
,則您的代碼需要為:
int m = std::max({a, b, c});
cout << "maximum: " << m << endl;
它也可以是單線的。
cout << "maximum: " << std::max({a, b, c}) << endl;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.