1

我试图用 C++ 编写一个程序,它应该订购 3 个随机数(用户将编写它们),然后打印较高和较低的数字,但如果两个或更多数字相等,则应打印它。

新代码:

#include <iostream>

using namespace std;

int main()
{
    int num1;
    int num2;
    int num3;

    cout << "num1" << endl;
    cin >> num1;
    cout << "num2" << endl;
    cin >> num2;
    cout << "num3" << endl;
    cin >> num3;

    if(num1 == num2 && num3==num2 && num1==num3){
        cout << "all numbers are equal";
    }
    else if (num1 == num2){
        cout << "num1 and num2 are equal";
    }

    else if (num2 == num3){
        cout << "num2 and num3 are equal";
    }

    else if(num3 == num1){
        cout << "num1 and num3 are equal";
    }

    else{
    if (num1 != num2 && num2 != num3 && num3 != num1){
        if (num1 > num2 && num1 > num3){
            cout << "higher is num1";
        }

        else if(num2 > num1 && num2 > num3){
            cout << "higher is num2";
        }
        else if(num3 > num1 && num3 > num2){
            cout << "higher is num3";
        }
    }

    }
    return 0;
}

新问题: 程序也需要知道哪个是最低的,那我该怎么做呢?

旧代码:

#include <iostream>

using namespace std;

int main()
{
    int num1;
    int num2;
    int num3;

    cout << "num1" << endl;
    cin >> num1;
    cout << "num2" << endl;
    cin >> num2;
    cout << "num3" << endl;
    cin >> num3;

    if(num1 == num2 && num3==num2 && num1==num3){
        cout << "all your numbers are equal";
    }

    if (num1 != num2 && num2 != num3 && num3 != num1){
        if (num1 > num2 && num1 > num3){
            cout << "num1";
        }

        else if(num2 > num1 && num2 > num3){
            cout << "num2";
        }
        else /*(num3 > num1 && num3 > num2)*/{//Here I tried to use and else if
            cout << "num3";
        }
    }


    return 0;
}

老问题 这段代码都是错的,但不知道自己做错了什么,请帮帮我。我有最后一个问题,我对 if 语句的 if 有限制吗?或者我不能写另外两个 if or..?谢谢。

4

2 回答 2

5
#include <iostream>
#include <set>

int main()
{
    std::set<int> numbers;
    int input;

    for (int i=1; i<=3; ++i) {
        std::cout << "Enter number " << i << ": ";
        std::cin >> input;
        numbers.insert(input);
    }

    if (numbers.size() < 3) {
        std::cout << "You entered the same number more than once, silly!" << std::endl;
        std::cout << "Nevertheless, ";
    }

    std::cout << "the maximum number is " << *numbers.rbegin() << std::endl;
}

此代码的重要特征是它利用了以下属性std::set

  1. 它保持所有元素的排序顺序
  2. 它不允许重复元素

std::set::rbegin用于获取最后一个元素,也就是最大的数字(因为数字是自动排序的)。

此代码不显示最大和最小的数字,但可以轻松添加。例如,std::set::insert返回可让您确定集合中的哪个插入失败的信息。std::set 只能包含其包含对象的单个副本。因此,如果用户多次输入一个数字,insert 调用就会失败。

于 2013-09-25T04:32:41.777 回答
0

以下代码解决了您的目的。它具有三个功能findLargestfindSmallestcheckEqual。这是一个非常基本的程序,可以根据您的需要修改如何显示和返回什么等。

#include <iostream>
using namespace std;

void findLargest(int n1,int n2,int n3)
{
    if(n1>n2 && n1>n3)
    {
        cout<<"Largest number is :"<<n1;
        cout<<"\n";
    }
    else if((n2>n1) && (n2>n3))
    {
        cout<<"Largest number is :"<<n2;
        cout<<"\n";
    }

    else
    {
        cout<<"Largest number is :"<<n3;
        cout<<"\n";
    }
}

void findSmallest(int n1,int n2,int n3)
{
    if(n1<=n2 && n1<=n3)
    {
        cout<<"Smallest number is :"<<n1;
        cout<<"\n";
    }
    else if((n2<=n1) && (n2<=n3))
    {
        cout<<"Smallest number is :"<<n2;
        cout<<"\n";
    }
    else
    {
        cout<<"Smallest number is :"<<n3;
        cout<<"\n";
    }
}
int checkEqual(int n1,int n2,int n3)
{
    if(n1==n2 && n2==n3 && n3==n1)
    {
        cout<<"All three are equal";
        cout<<"\n";
    }
    else if(n1==n2||n2==n3||n3==n1)
    {
        cout<<"Two numbers are equal";
        cout<<"\n";
    }
    else
    {
        cout<<"None are equal.. Finding Largest and Smallest....!!";
        cout<<"\n";
    }
}
int main() {

    int num1,num2,num3;
    cout<<"Enter the numbers";
    cin>>num1>>num2>>num3;
    cout<<"\n";
    checkEqual(num1,num2,num3);
    findSmallest(num1,num2,num3);
    findLargest(num1,num2,num3);
    return 0;
}

ideone链接:http: //ideone.com/hz4keQ

希望能帮助到你。:)

于 2013-09-25T05:16:58.480 回答