C++ 查找序列中的两个最大和最小整数
我试图在课堂上制作一个程序,在给定的序列中找出2个最大和最小的整数,然后将它们打印给用户。 示例运行如下所示:C++ 查找序列中的两个最大和最小整数,c++,if-statement,for-loop,integer,sequence,C++,If Statement,For Loop,Integer,Sequence,我试图在课堂上制作一个程序,在给定的序列中找出2个最大和最小的整数,然后将它们打印给用户。 示例运行如下所示: Enter the sequence size: 8 Enter the sequence: 5 8 9 12 -6 4 -8 10 The two smallest values are: -8 -6 The two largest values are: 12 10 我不允许使用排序或数组。我整晚都在做这件事,但我似乎不明白,有人能给我指出正确的方向吗?这就是我目前所处的位置—
Enter the sequence size: 8
Enter the sequence: 5 8 9 12 -6 4 -8 10
The two smallest values are: -8 -6
The two largest values are: 12 10
我不允许使用排序或数组。我整晚都在做这件事,但我似乎不明白,有人能给我指出正确的方向吗?这就是我目前所处的位置——它不会编译,因为'small1'和'large1'变量没有初始化,但是如果我将它们设置为零,它们在示例运行中仍保持为零
int small1, small2, large1, large2, loopcount, sequencevalue;
// Ask the user to enter the first number
cout << "Sequence Size: ";
cin >> loopcount;
// Enter the sequence and start the loop
cout << "\nEnter the Sequence: ";
for (int i = 0; i < loopcount; i++)
{
cin >> sequencevalue;
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
if (sequencevalue > large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Small variables
if (small1 == 0)
{
small1 = sequencevalue;
}
else
{
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
}
// Large variables
if (large1 == 0)
{
large1 = sequencevalue;
}
else
{
if (sequencevalue < large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";
int small1、small2、large1、large2、loopcount、sequencevalue;
//请用户输入第一个数字
cout>循环计数;
//输入序列并开始循环
cout>sequencevalue;
如果(sequencevaluelarge1)
{
大2=大1;
large1=序列值;
}
}
//小变量
if(small1==0)
{
small1=序列值;
}
其他的
{
如果(sequencevalue
注意这个程序的输出
#include <climits>
#include <iostream>
int main() {
std::cout << INT_MAX << std::endl;
std::cout << INT_MIN << std::endl;
return 0;
}
#包括
#包括
int main(){
std::cout您正在寻找的
注意这个程序的输出
#include <climits>
#include <iostream>
int main() {
std::cout << INT_MAX << std::endl;
std::cout << INT_MIN << std::endl;
return 0;
}
#包括
#包括
int main(){
std::cout您的程序具有未定义的行为,因为在初始化之前您正在使用small1
、small2
、large1
、large2
初始化small1
和small2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
int large1 = INT_MIN;
int large2 = INT_MIN;
初始化large1
和large2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
int large1 = INT_MIN;
int large2 = INT_MIN;
更新
在没有排序数组的情况下,正确更新数字的逻辑不是直接的。下面包含了一个工作程序,可以正确计算small1
和small2
。我将把它留给您扩展为计算large1
和large2
#include <iostream>
#include <sstream>
#include <climits>
#include <algorithm>
int main()
{
std::istringstream str("8 5 8 9 12 -6 4 -8 10");
int small1 = INT_MAX;
int small2 = INT_MAX;
int loopcount;
bool small1_found = false;
str >> loopcount;
for (int i = 0; i < loopcount; ++i )
{
int sequencevalue;
str >> sequencevalue;
if ( sequencevalue < small1 )
{
int temp = small1;
small1 = sequencevalue;
if ( small1_found )
{
small2 = std::max(sequencevalue, temp);
}
small1_found = true;
}
}
std::cout << "Two smallest values: " << small1 << " " << small2 << "\n";
}
您的程序具有未定义的行为,因为您在初始化它们之前正在使用small1
、small2
、large1
、large2
初始化small1
和small2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
int large1 = INT_MIN;
int large2 = INT_MIN;
初始化large1
和large2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
int large1 = INT_MIN;
int large2 = INT_MIN;
更新
在没有排序数组的情况下,正确更新数字的逻辑不是直接的。下面包含了一个工作程序,可以正确计算small1
和small2
。我将把它留给您扩展为计算large1
和large2
#include <iostream>
#include <sstream>
#include <climits>
#include <algorithm>
int main()
{
std::istringstream str("8 5 8 9 12 -6 4 -8 10");
int small1 = INT_MAX;
int small2 = INT_MAX;
int loopcount;
bool small1_found = false;
str >> loopcount;
for (int i = 0; i < loopcount; ++i )
{
int sequencevalue;
str >> sequencevalue;
if ( sequencevalue < small1 )
{
int temp = small1;
small1 = sequencevalue;
if ( small1_found )
{
small2 = std::max(sequencevalue, temp);
}
small1_found = true;
}
}
std::cout << "Two smallest values: " << small1 << " " << small2 << "\n";
}
我加入了climits并更正了你的代码
代码示例:
#include <iostream>
#include <climits>
using namespace std;
int main(){
int count = 0;
cout << "Sequence Size: ";
cin >> count;
int sequencevalue;
int large1 = INT_MIN;
int large2 = INT_MIN;
int small1 = INT_MAX;
int small2 = INT_MAX;
for (int i = 0; i < count; i++){
cin >> sequencevalue;
if (sequencevalue >= large1) {
large2 = large1;
large1 = sequencevalue;
}
else if (sequencevalue > large2){
large2 = sequencevalue;
}
if (sequencevalue <= small1) {
small2 = small1;
small1 = sequencevalue;
}
else if (sequencevalue < small2){
small2 = sequencevalue;
}
}
cout << "Sequence size is " << count << endl;
if (0 == count) {}
else if (1 == count){
cout << "Smallest value: " << small1 << endl;
cout << "Largest value: " << large1 << endl;
}
else{
cout << "First and second smallest value: " << small1 << " " << small2 << endl;
cout << "First and second largest value: " << large1 << " " << large2 << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
整数计数=0;
计数;
int序列值;
int large1=int_MIN;
int large2=int_MIN;
int small1=int_MAX;
int small2=int_MAX;
for(int i=0;i>序列值;
如果(sequencevalue>=large1){
大2=大1;
large1=序列值;
}
否则如果(sequencevalue>large2){
large2=序列值;
}
如果(sequencevalueI包含climits并更正了您的代码
代码示例:
#include <iostream>
#include <climits>
using namespace std;
int main(){
int count = 0;
cout << "Sequence Size: ";
cin >> count;
int sequencevalue;
int large1 = INT_MIN;
int large2 = INT_MIN;
int small1 = INT_MAX;
int small2 = INT_MAX;
for (int i = 0; i < count; i++){
cin >> sequencevalue;
if (sequencevalue >= large1) {
large2 = large1;
large1 = sequencevalue;
}
else if (sequencevalue > large2){
large2 = sequencevalue;
}
if (sequencevalue <= small1) {
small2 = small1;
small1 = sequencevalue;
}
else if (sequencevalue < small2){
small2 = sequencevalue;
}
}
cout << "Sequence size is " << count << endl;
if (0 == count) {}
else if (1 == count){
cout << "Smallest value: " << small1 << endl;
cout << "Largest value: " << large1 << endl;
}
else{
cout << "First and second smallest value: " << small1 << " " << small2 << endl;
cout << "First and second largest value: " << large1 << " " << large2 << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
整数计数=0;
计数;
int序列值;
int large1=int_MIN;
int large2=int_MIN;
int small1=int_MAX;
int small2=int_MAX;
for(int i=0;i>序列值;
如果(sequencevalue>=large1){
大2=大1;
large1=序列值;
}
否则如果(sequencevalue>large2){
large2=序列值;
}
if(sequencevalueint small1、small2、large1、large2、loopcount、sequencevalue;
//请用户输入第一个数字
cout>循环计数;
//输入序列并开始循环
cout>sequencevalue;
如果(i==0)
small1=序列值;
small2=序列值;
large2=序列值;
large1=序列值;}
如果(i==1)
{如果(sequencevalue>small1)small2=sequencevalue;
否则{
small2=small1;
small1=序列值;
}
大2=小1;
大1=小2;
}
如果(sequencevaluelarge1)
{
大2=大1;
large1=序列值;
}
}
//最终产量
coutint small1、small2、large1、large2、loopcount、sequencevalue;
//请用户输入第一个数字
cout>循环计数;
//输入序列并开始循环
cout>sequencevalue;
如果(i==0)
small1=序列值;
small2=序列值;
large2=序列值;
large1=序列值;}
如果(i==1)
{如果(sequencevalue>small1)small2=sequencevalue;
否则{
small2=small1;
small1=序列值;
}
大2=小1;
大1=小2;
}
如果(sequencevaluelarge1)
{
大2=大1;
large1=序列值;
}
}
//最终产量
给你
#include <iostream>
int main()
{
while ( true )
{
// Ask the user to enter the first number
std::cout << "Sequence Size: ";
unsigned int loopcount;
if ( not ( std::cin >> loopcount ) || ( loopcount == 0 ) ) break;
int small1, small2, large1, large2;
// Enter the sequence and start the loop
std::cout << "\nEnter the Sequence: ";
unsigned int i = 0;
for ( ; i < loopcount; i++ )
{
int sequencevalue;
std::cin >> sequencevalue;
if ( i == 0 )
{
small1 = sequencevalue;
large1 = sequencevalue;
}
else
{
if ( large1 < sequencevalue )
{
large2 = large1;
large1 = sequencevalue;
}
else if ( i == 1 || large2 < sequencevalue )
{
large2 = sequencevalue;
}
if ( sequencevalue < small1 )
{
small2 = small1;
small1 = sequencevalue;
}
else if ( i == 1 || sequencevalue < small2 )
{
small2 = sequencevalue;
}
}
}
if ( i == 1 )
{
printf( "There is only one largest value %d\n",
large1 );
printf( "And only one mallest value %d\n",
small1 );
}
else
{
printf( "The first largest value is %d and the second largest value is %d\n",
large1, large2 );
printf( "And the first smallest value is %d and the second smallest value is %d\n",
small1, small2 );
}
}
return 0;
}
给你
#include <iostream>
int main()
{
while ( true )
{
// Ask the user to enter the first number
std::cout << "Sequence Size: ";
unsigned int loopcount;
if ( not ( std::cin >> loopcount ) || ( loopcount == 0 ) ) break;
int small1, small2, large1, large2;
// Enter the sequence and start the loop
std::cout << "\nEnter the Sequence: ";
unsigned int i = 0;
for ( ; i < loopcount; i++ )
{
int sequencevalue;
std::cin >> sequencevalue;
if ( i == 0 )
{
small1 = sequencevalue;
large1 = sequencevalue;
}
else
{
if ( large1 < sequencevalue )
{
large2 = large1;
large1 = sequencevalue;
}
else if ( i == 1 || large2 < sequencevalue )
{
large2 = sequencevalue;
}
if ( sequencevalue < small1 )
{
small2 = small1;
small1 = sequencevalue;
}
else if ( i == 1 || sequencevalue < small2 )
{
small2 = sequencevalue;
}
}
}
if ( i == 1 )
{
printf( "There is only one largest value %d\n",
large1 );
printf( "And only one mallest value %d\n",
small1 );
}
else
{
printf( "The first largest value is %d and the second largest value is %d\n",
large1, large2 );
printf( "And the first smallest value is %d and the second smallest value is %d\n",
small1, small2 );
}
}
return 0;
}
也许把值设置为0是错误的值?你会建议我初始化它们吗?从我理解的C++中的任何变量都有一个值,包括NULL,它只是0。在C++中,正确的初始化<代码> SLM1将是<代码> STD::NoimiCiLime::Max()。也许将这些值设置为0是错误的值吗?你会建议我如何初始化它们?从我理解的,C++中的任何变量都有一个值,包括NULL,它只是0。C++中,正确的初始化代码< Simult1< /代码