如何将输出从 1,2,3,4,5 更改为第一、第二等

How do I change the output from being 1,2,3,4,5 to being first, second, etc.?

本文关键字:输出      更新时间:2023-10-16

如何让我的输出从说 1,2,3,4,5 更改为说第一、第二、第三、第四、第五?另外,最后当它声明哪个输入是最小值时,我如何让它说它是否是条目 1-5,而不是实际的输入编号?

#include <iostream>
using namespace std;
int main()
{
int num[5];
int i = 0;
int small=0;
    cout <<"Enter the first number: ";
    cin >> num[0];
    small = num[0];
    {
        for(i = 1; i < 5; i++) 
            {
             cout << "Enter the number "<< i + 1 <<" number: ";
             cin >> num[i];
                if (num[i] < small)         
                    small = num[i];
            }
    }
cout<<endl<<endl;   
cout<<"Entry No. "<<small<<" is the minimum number"<<endl<<endl;
return 0;
}

您不仅要找到最小的num[i],还要找到相应的i。 这很简单。 关键是要更换循环的内脏

if (num[i] < small)         
    small = num[i];

if (num[i] < small) {    // when num[i] is the smallest so far
    small = num[i];      //     save it
    smalli = i;          //     and save the matching i
}

我怀疑你可以弄清楚变量声明和初始值。

要打印基数单词,您可以使用

const char* cardinals[] = { "first", "second", "third", "fourth", "fifth" };

,然后在提示中显示cardinals[i]。 和/或最后cardinals[smalli]

std::cout << "nnThe " << cardinals[smalli] " number was the minimum, " << small << "nn";

尝试以下操作

#include <iostream>
int main()
{
    const size_t N = 5;
    const char *serial_num[N] = { 'first", "second", "third", "fourth", "fifth" };
    int num[N];
    size_t small;
    for ( size_t i = 0; i < N; i++ ) 
    {
        std::cout << "Enter the " << serial_num[i] << " number: ";
        std::cin >> num[i];
        if ( i == 0 || num[i] < num[small] ) small = i;
    }   
    std::cout << "nnEntry No. " << serial_num[small] << " is the minimum numbern" << std::endl;
    return 0;
}