求C中最大和最小的整数
正如我在另一个问题中提到的,我一直在 KN King 的 C 编程:现代方法 (2ndEdn) 中自学 C。
我很喜欢它,但我希望在适当的情况下在这里发布奇怪的问题以寻求建议,因为不幸的是我没有导师,有些问题提出了更多问题,然后他们回答了!
我正在解决一个问题,要求我编写一个程序,查找用户输入的四个整数中的最大和最小......我想出了一种方法来找到最大的,但对于我的生活来说可以不知道如何取出最小的。该问题表明四个 if 语句就足够了。数学不是我的强项,我将不胜感激任何建议!
#include <stdio.h>
int main(int argc, const char *argv[])
{
int one, two, three, four;
printf("Enter four integers: ");
scanf("%d %d %d %d", &one, &two, &three, &four);
if (four > three && four > two && four > one)
printf("Largest: %d", four);
else if (three > four && three > two && three > one)
printf("Largest: %d", three);
else if (two > three && two > four && two > one)
printf("Largest: %d", two);
else
printf("Largest: %d", one);
return 0;
}
我尽量保持简单,因为我只读到了 27 章中的第 5 章!
干杯 安德鲁
As I mentioned in another question I've been teaching myself C out of of K.N King's C Programming: A Modern Approach (2ndEdn).
I'm enjoying it, but am hoping to post the odd question here for advice if appropriate because unfortunately I don't have a tutor and some bits raise more questions then they answer!
I'm up to a question that asks me to write a program that finds the largest and smallest of four integers entered by the user... I've come up with a way to find the largest, but for the life of me can't work out how to get the smallest out. The question says that four if statements should be sufficient. Math is not my forte, I'd appreciate any advice!
#include <stdio.h>
int main(int argc, const char *argv[])
{
int one, two, three, four;
printf("Enter four integers: ");
scanf("%d %d %d %d", &one, &two, &three, &four);
if (four > three && four > two && four > one)
printf("Largest: %d", four);
else if (three > four && three > two && three > one)
printf("Largest: %d", three);
else if (two > three && two > four && two > one)
printf("Largest: %d", two);
else
printf("Largest: %d", one);
return 0;
}
I'm trying to keep it simple, as I'm only up to chapter 5 of 27!
Cheers
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
swap()
函数的实现留作练习。The implementation of the
swap()
function is left as exercise.另一种方式是这样的:
不需要一个 if 语句;)
another way would be as such:
Not a single if statement needed ;)
在本章的上下文中:
Within the context of the chapter:
我有同一本书,我承认,那个程序让我很头疼。
对于初学者程序员来说,这有点棘手。
首先,比较第一对整数(代码中的 a 和 b),并将本地最小值和最大值存储在某处。对第二对做同样的事情。然后比较局部最小值以获得全局最小值,并对最大值做同样的事情。不超过四个如果。
有更好的方法来解决这个问题,其中一些方法在这里展示,但本书将在后面的章节中介绍它们。
I have that same book, and I'll admit it, that program gave me quite a headache.
It's a little tricky for a beginner programmer.
First, you compare the first pair of integers (a and b in the code), and store local min and max somewhere. Do the same thing with the second pair. Then compare local minimums to get the global minimum, and do the same thing with maximums. No more than four if's.
There are better ways to solve this, some of them are shown here, but the book covers them in the later chapters.
我也在阅读 KN King 的“C 编程:现代方法,第二版”。以下是我对该线程原始问题的解决方案。
请注意,我仅使用本书第 5 章中介绍的 C 概念。最初的编程问题来自第 5 章,编程问题 7。
I'm going through K.N. King's "C Programming: A Modern Approach, Second Edition" too. Below is my solution for this thread's original question.
Note that I'm using only C concepts introduced up through Chapter 5 of the book. The original programming problem comes from Chapter 5, Programming Problem 7.
我设法用少于 4 个 if 语句解决了这个问题,这是我的解决方案:
但是我不知道我是否在做正确的事情。至少我认为这会对某人有所帮助:)
I managed to solve this problem in even fewer than 4 if statements, here is my solution:
However I don't know if I am doing the right things. At least I think it will help someone :)