2017-03-12 73 views
0

如何編寫C++程序,可以讓用戶輸入三個數字,那三2 higest之間找到,寫不便,如:中5,3,4總和5和4是9!C++三個數字之和之間的兩個highers不循環和數組

我想媒體鏈接,但我得到了太多,如果別人的,它看起來非常糟糕,甚至不是在所有情況下正常工作:/

#include <iostream> 
#include<math.h> 
using namespace std; 
int main() 
{ 
    float n1, n2, n3; 
cout << "Enter three numbers: "; 
    cin >> n1 >> n2 >> n3; 

    if(n1 >= n2 && n1 >= n3) 
    { 
     if(n2>=n1 && n2>=3) 
      cout << "Sum of 2 highest between this three numbers is: " << cout<<n1+n2; 
    } 

    if(n1 >= n2 && n1 >= n3) 
    { 
     if(n3>=n1 && n3>=n2) 
      cout << "Sum of 2 highest between this three numbers is: " << cout<<n1+n3; 
    } 

    if(n2 >= n1 && n2 >= n3) 
    { 
     if(n3 >= n1 && n3 >= n2) { 
      cout << "Sum of 2 highest between this three numbers is: " << cout<<n3+n2; 
     } 
    } 

    if(n3 >= n1 && n3 >= n2) 
    { 
     if(n2 >= n1) { 
      cout << "Sum of 2 highest between this three numbers is: " << cout<<n3+n2; 
     } 
    } 

    if(n3 >= n1 && n3 >= n2) 
    { 
     if(n1>= n2) { 
      cout << "Sum of 2 highest between this three numbers is: " << cout<<n3+n1; 
     } 
    } 

     return 0; 
} 
+0

我不知道它是否會有幫助,但是如果'n1> n2 && n2> n3'那麼這也意味着'n1> n3'。 –

+0

@Someprogrammerdude來吧,讓我們在這裏找到2個最高的數字:D –

+0

一些lambda與stable_sort可能會得心應手,你接受使用向量? – user

回答

0

試試這個:

max = (n1>n2) ? ((n1>n3) ? n1 : n3) : ((n2>n3) ? n2 : n3); 
second = (n1==max) ? ((n2>n3) ? n2 : n3) : ((n2==max) ? ((n1>n3) ? n1 : n3) : ((n2>n1)? n2 : n1)); 
2

找到最小第一,

double min = std::min(n1, std:min(n2, n3)); // or std::min({n1, n2, n3}) 

然後消除它

if(min == n1) 
{ 
    std::cout << n2 << ' ' << n3 << ' ' << n2 + n3; 
} 
else if(min == n2) 
{ 
    std::cout << n1 << ' ' << n3 << ' ' << n1 + n3; 
} 
else 
{ 
    std::cout << n1 << ' ' << n2 << ' ' << n1 + n2; 
} 

Live example

當然,如果你真的很挑剔,你可以通過

double min = n1; 
if(n2 < min) 
    min = n2; 
if(n3 < min) 
    min = n3; 

另一個解決方案是隻需做最小的發現和消除在一個單一的步驟,如更換呼叫std::min

if(n1 < n2 && n1 < n3) // min is n1 
{ 
    std::cout << n2 << ' ' << n3 << ' ' << n2 + n3; 
} 
else if(n2 < n1 && n2 < n3) // min is n2 
{ 
    std::cout << n1 << ' ' << n3 << ' ' << n1 + n3; 
} 
else // min is n3 
{ 
    std::cout << n1 << ' ' << n2 << ' ' << n1 + n2; 
} 

就複雜性而言,兩種解決方案都是相同的,即需要4次比較。

0

您只需要進行一些檢查即可獲得所需的結果。

int n1 = 0,n2 = 0,n3 = 0; 
cin>>n1>>n2>>n3; 

int highest = 0; 
int second_highest = 0; 

if(n1 > n2) 
{ 
    if(n1 > n3) 
    { 
     highest = n1; 
     if(n3 > n2) 
     { 
      second_highest = n3; 
     } 
     else 
     { 
      second_highest = n2; 
     } 
    } 
    else 
    { 
     highest = n3; 
     second_highest = n1; 
    } 
} 
else if(n2 > n3) // if the first if is false it means that the n2 is highest or second highest 
{ 
    highest = n2; 
    if(n1 > n3) 
    { 
     second_highest = n1; 
    } 
    else 
    { 
     second_highest = n3; 
    } 
} 
else 
{ 
    highest = n3; 
    second_highest = n2; 
} 

cout << "Highest: " << highest << "\nSecond highest: " << second_highest; 

希望有幫助。

0

一種簡單的方法來回答這個問題作爲要求將是

cout << "Sum of 2 highest between this three numbers is: "; 
    cout << (long long) n1 + n2 + n3 - std::min(n1, std::min(n2, n3)); 

或(C++ 11)

cout << "Sum of 2 highest between this three numbers is: "; 
    cout << (long long) n1 + n2 + n3 - std::min({n1, n2, n3}); 

(long long)轉換力被相加或相減的所有值之前被提升操作,以避免溢出,如果int支持的值的範圍小於long long

當然,這確實嵌入了三個正在使用的值的固定假設。

+0

這很整潔,但你也需要找到最大的2個數字(至少這是OP在他的例子中要求的);雖然在標題 – vsoftco

+0

中沒有說明這個問題沒有說明。 – Peter

+0

*找到這三個2最高和寫之間smth如:5,3,4 5和4的總和是9!*位模糊,但考慮到代碼OP寫道他可能也想要數字 – vsoftco