查找序列中的 2 个最大和最小整数
Finding the 2 greatest and smallest integers in a sequence
我正在尝试在 class 中编写一个程序,它将找到给定序列中的 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
不允许我使用排序或数组。我整晚都在做,但我似乎无法弄清楚,有人能指出我正确的方向吗?这是我目前停留的地方 - 它不会编译,因为 '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";
非常感谢您就此事提供的帮助,感谢您抽出宝贵时间。
您正在寻找 <climits>
注意这个程序的输出
#include <climits>
#include <iostream>
int main() {
std::cout << INT_MAX << std::endl;
std::cout << INT_MIN << std::endl;
return 0;
}
您的程序有未定义的行为,因为您在 small1
、small2
、large1
、large2
初始化之前使用它们。
初始化 small1
和 small2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
初始化 large1
和 large2
的正确方法是:
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";
}
输出:
Two smallest values: -8 -6
我包含了 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;
}
上面的代码示例将相同的数字视为唯一元素(这可能导致第一个和第二个相同 smallest/largest)。
如果您需要忽略相同的数字作为唯一元素,则 for 循环应如下所示:
for (int i = 0; i < count; i++){
cin >> sequencevalue;
if (sequencevalue > large1) {
large2 = large1;
large1 = sequencevalue;
}
else if (sequencevalue != large1 && sequencevalue > large2){
large2 = sequencevalue;
}
if (sequencevalue < small1) {
small2 = small1;
small1 = sequencevalue;
}
else if (sequencevalue != small1 && sequencevalue < small2){
small2 = sequencevalue;
}
}
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(i==0)
small1=sequencevalue;
small2=sequencevalue;
large2=sequencevalue;
large1=sequencevalue;}
if(i==1)
{if(sequencevalue>small1)small2=sequencevalue;
else{
small2=small1;
small1=sequencevalue;
}
large2=small1;
large1=small2;
}
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
if (sequencevalue > large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";
给你
#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;
}
程序输出看起来像
Sequence Size: 10
Enter the Sequence: 1 9 8 0 7 6 5 4 3 2
The first largest value is 9 and the second largest value is 8
And the first smallest value is 0 and the second smallest value is 1
Sequence Size: 0
我正在尝试在 class 中编写一个程序,它将找到给定序列中的 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
不允许我使用排序或数组。我整晚都在做,但我似乎无法弄清楚,有人能指出我正确的方向吗?这是我目前停留的地方 - 它不会编译,因为 '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";
非常感谢您就此事提供的帮助,感谢您抽出宝贵时间。
您正在寻找 <climits>
注意这个程序的输出
#include <climits>
#include <iostream>
int main() {
std::cout << INT_MAX << std::endl;
std::cout << INT_MIN << std::endl;
return 0;
}
您的程序有未定义的行为,因为您在 small1
、small2
、large1
、large2
初始化之前使用它们。
初始化 small1
和 small2
的正确方法是:
int small1 = INT_MAX;
int small2 = INT_MAX;
初始化 large1
和 large2
的正确方法是:
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";
}
输出:
Two smallest values: -8 -6
我包含了 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;
}
上面的代码示例将相同的数字视为唯一元素(这可能导致第一个和第二个相同 smallest/largest)。
如果您需要忽略相同的数字作为唯一元素,则 for 循环应如下所示:
for (int i = 0; i < count; i++){
cin >> sequencevalue;
if (sequencevalue > large1) {
large2 = large1;
large1 = sequencevalue;
}
else if (sequencevalue != large1 && sequencevalue > large2){
large2 = sequencevalue;
}
if (sequencevalue < small1) {
small2 = small1;
small1 = sequencevalue;
}
else if (sequencevalue != small1 && sequencevalue < small2){
small2 = sequencevalue;
}
}
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(i==0)
small1=sequencevalue;
small2=sequencevalue;
large2=sequencevalue;
large1=sequencevalue;}
if(i==1)
{if(sequencevalue>small1)small2=sequencevalue;
else{
small2=small1;
small1=sequencevalue;
}
large2=small1;
large1=small2;
}
if (sequencevalue < small1)
{
small2 = small1;
small1 = sequencevalue;
}
if (sequencevalue > large1)
{
large2 = large1;
large1 = sequencevalue;
}
}
// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";
给你
#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;
}
程序输出看起来像
Sequence Size: 10
Enter the Sequence: 1 9 8 0 7 6 5 4 3 2
The first largest value is 9 and the second largest value is 8
And the first smallest value is 0 and the second smallest value is 1
Sequence Size: 0