資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
編寫程序,輸入3個整數,求出最大數並輸出。
輸入格式
輸入三個整數,用空格分開。
輸出格式
輸出三個數的最大值
樣例輸入
5 3 4
樣例輸出
5
解題思路:
這道題相當簡單,可能有人認爲我在水題(好吧你說的對),但是這道題目給了我一個很好的思考和挑戰,就是如何更快求出最大值,下面我將用三種方法求出三個整數的最大值,三種方法一個比一個簡便,在做其他項目時可以使代碼更美觀,減少代碼複雜度,代碼如下:
這是普通if判斷實現:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
int max = a;
if(a < b){
max = b;
if(b < c){
max = c;
}
}else{
if(c > a){
max = c;
}
}
cout << max;
return 0;
}
三目運算符方法:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
cout << max;
return 0;
}
max函數判斷方法:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
int max = max(max(a, b), c);
cout << max;
return 0;
}
方法是不是越來越簡單了呢?喜歡的小夥伴麻煩點個贊喲,謝謝您的鼓勵!