如何将输出从 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;
}
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词