程序找到四个数字之间的第一和第二个最大数字? [英] Program to find 1st and 2nd maximum number between four numbers?

查看:98
本文介绍了程序找到四个数字之间的第一和第二个最大数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到最大和第二大,但第二大答案不正确。



我尝试过: < br $>


 #include< iostream> 
using namespace std;
int main()
{
int number,maximum = 0,seclargest = 0;
for(int i = 0; i< 4; i ++)
{
cout<< 输入任意四个数字;
cin>>数;
if(number> maximum)
{
maximum = number;
}
if(number> seclargest&& number< largest)
{
seclargest = number;
}
}
cout<<最大的<< \ n<< seclargest;
返回0;
}

解决方案

停下来想想你在做什么。

当你输入一个数字时,它可能是迄今为止最大的,或者是第二大的,或者比任何一个都小。



所以你需要两个独立的测试:



它是否大于最大?

如果是,那么之前的最大值自动成为第二大,最大的成为新价值。

其他明智的是,它是否大于第二大?

如果是,第二大成为新值

否则,忽略它。



这会给你你想要的结果,但我认为0是一个不好的值来初始化。我会使用常量值 INT_MIN C ++ Integer Limits [ ^ ]给你一个可以输入的最小值 - 可能比零小很多!


你有 The Hammer ,使用它:

  #include   <   iostream  >  
#include < queue >
使用 命名空间标准;

int main()
{
priority_queue< int> PQ;
for int n = 0 ; n< 4; ++ n)
{
cout<< 请输入数字<< (n + 1)<< / 4<< ENDL;
int x;
cin>> X;
pq.push(x);
}

cout<< 报告最大的:<< ENDL;
for int n = 0 ; n< 2; ++ n)
{
cout<< pq.top()<< ENDL;
pq.pop();
}
}


首先要做的是问题分析:

- 拿一张纸和铅笔。记下当一个新数字取决于它的值时,最大值和第二个值是如何演变的,这是你的算法。



如果程序没有给出预期的结果,那就是程序错了,是时候转向调试器了。

-----

有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

-----

- 学习一种或多种分析方法, EW Djikstra自上而下的方法是一个良好的开端。

https:// en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^

i want to find the largest and 2nd largest but the second largest answer is not correct.

What I have tried:

#include <iostream>
using namespace std;
int main()
{
	int number, largest = 0, seclargest = 0;
	for (int i = 0; i < 4; i++)
	{
		cout << "Enter Any Four Numbers ";
		cin >> number;
		if (number>largest)
		{
			largest = number;
		}
		if (number > seclargest && number < largest)
		{
			seclargest = number;
		}
	}
		cout << largest << "\n" << seclargest;
	return 0;
}

解决方案

Stop and think about what you are doing.
When you enter a number, it could be the largest so far, or the second largest, or smaller than either.

So you need two independent tests:

Is it larger than the largest?
If it is, then the previous largest is automatically the second largest, and teh largest becomes the new value.
Other wise, is it larger than teh second largest?
If it is, the second largest becomes the new value
Otherwise, ignore it.

That gives you the results you want, but I'd argue that 0 is a bad value to initialize to. I would use the constant value INT_MIN: C++ Integer Limits[^] which will give you the smallest possible value that can be entered - which may be a lot smaller than zero!


You have The Hammer, use it:

#include <iostream>
#include <queue>
using namespace std;

int main()
{
  priority_queue<int > pq;
  for (int n=0; n<4; ++n)
  { 
    cout << "please enter  number " << (n+1) << "/4" << endl;
    int x; 
    cin >> x;
    pq.push(x);
  }
  
  cout << "reporting the biggest ones:" << endl;
  for (int n=0; n<2; ++n)
  { 
    cout << pq.top() << endl;
    pq.pop();
  }
}


First thing to do is problem analyze:
- Take sheet of paper and a pencil. Write down how largest and second evolve when a new number comes depending on it value, this is your algorithm.

If the program don't give expected results, something is wrong in program, it is time to turn to debugger.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
- Learn one or more analyze methods, E.W. Djikstra top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]


这篇关于程序找到四个数字之间的第一和第二个最大数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆