正如我在另一个问题中所提到的,我一直在用K.N 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章!
干杯 安德鲁
答案 0 :(得分:15)
if (first > second)
swap(&first, &second);
if (third > fourth)
swap(&third, &fourth);
if (first > third)
swap(&first, &third);
if (second > fourth)
swap(&second, &fourth);
printf("Smallest: %d\n", first);
printf("Largest: %d\n", fourth);
swap()
功能的实现留作练习。
答案 1 :(得分:2)
另一种方式是:
int one, two, three, four;
//Assign values to the four variables;
int largest, smallest;
largest = max(max(max(one, two), three), four);
smallest = min(min(min(one, two), three), four);
不需要if语句;)
答案 2 :(得分:1)
在本章的范围内:
if (a > b) {
max = a;
min = b;
} else {
max = b;
min = a;
}
if (c > d) {
max2 = c;
min2 = d;
} else {
max2 = d;
min2 = c;
}
if (max < max2) {
max = max2;
}
if (min > min2) {
min = min2;
}
答案 3 :(得分:0)
我有同样的书,我承认,这个程序让我很头疼。 这对初学程序员来说有点棘手。
首先,比较第一对整数(代码中的a和b),并将局部最小值和最大值存储在某处。对第二对做同样的事情。然后比较局部最小值以获得全局最小值,并使用最大值执行相同的操作。不超过四个。
#include <stdio.h>
int main (void)
{
int a, b, c, d, min1, max1, min2, max2, min, max;
scanf ("%d %d %d %d", &a, &b, &c, &d);
if (a > b)
{
max1 = a;
min1 = b;
}
else
{
max1 = b;
min1 = a;
}
if (c > d)
{
max2 = c;
min2 = d;
}
else
{
max2 = d;
min2 = c;
}
if (min1 < min2) min = min1;
else min = min2;
if (max1 > max2) max = max1;
else max = max2;
printf ("%d %d", max, min);
return 0;
}
有更好的方法可以解决这个问题,其中一些是在这里展示的,但本书将在后面的章节中介绍它们。
答案 4 :(得分:0)
我正在经历K.N. King的“C编程:现代方法,第二版”也是。以下是我对该主题原始问题的解决方案。
请注意,我只使用了本书第5章介绍的C概念。最初的编程问题来自第5章编程问题7。
21 #include <stdio.h>
22
23 int main(void)
24 {
25 int i1, i2, i3, i4, large_1, small_1, large_2, small_2,
26 largest, smallest;
27
28 printf("\nEnter four integers: ");
29 scanf("%d %d %d %d", &i1, &i2, &i3, &i4);
30
31 if (i1 < i2) {
32 small_1 = i1;
33 large_1 = i2;
34 } else {
35 small_1 = i2;
36 large_1 = i1;
37 }
38
39 if (i3 < i4) {
40 small_2 = i3;
41 large_2 = i4;
42 } else {
43 small_2 = i4;
44 large_2 = i3;
45 }
46
47 if (large_1 < large_2)
48 largest = large_2;
49 else
50 largest = large_1;
51
52 if (small_1 < small_2)
53 smallest = small_1;
54 else
55 smallest = small_2;
56
57 printf("Largest: %d\n", largest);
58 printf("Smallest: %d\n\n", smallest);
59
60 return 0;
61 }
答案 5 :(得分:0)
#include <stdio.h>
/*
SOLUTION 1
int main(void) {
int a1,a2,a3,a4,max,min,max1,min1,max2,min2;
printf("Enter four integers : ");
scanf("%d %d %d %d",&a1,&a2,&a3,&a4);
if (a1 > a2) {
max1 = a1;
min1 = a2;
} else {
max1 = a2;
min1 = a1;
}
if (a3 > a4) {
max2 = a3;
min2 = a4;
} else {
max2 = a4;
min2 = a3;
}
if (max1 > max2)
max = max1;
else
max = max2;
if (min1 < min2)
min = min1;
else
min = min2;
printf("Largest : %d\n",max);
printf("Smallest : %d\n",min);
}
*/
/*
SOLUTION 2
*/
int main(void) {
int a1,a2,a3,a4;
printf("Enter four integers : ");
scanf("%d %d %d %d",&a1,&a2,&a3,&a4);
if (a1 > a2) {
int temp1 = a1; a1 = a2; a2 = temp1; // Swap the numbers (a1 to contain smallest number)
}
if (a3 > a4) {
int temp2 = a3; a3 = a4; a4 = temp2; // Swap the numbers (a1 to contain smallest number)
}
if (a1 > a3) {
int temp3 = a1; a1 = a3; a3 = temp3; // Swap the numbers (a1 to contain smallest number)
}
if (a2 > a4) {
int temp4 = a2; a2 = a4; a4 = temp4; // Swap the numbers (a1 to contain smallest number)
}
printf("Largest : %d\n",a4);
printf("Smallest : %d\n",a1);
}
答案 6 :(得分:-1)
printf("Largest: %d\n",(one>two ? one:two)>(three>four ? three:four)
? (one>two ? one:two):(three>four ? three:four));
printf("Smallest: %d",(one<two ? one:two)<(three<four ? three:four)
? (one<two ? one:two):(three<four ? three:four));
答案 7 :(得分:-1)
我设法用甚至少于4个if语句来解决这个问题,这是我的解决方案:
#include<stdio.h>
int main(void){
int no1, no2, no3, no4;
int max1, max2, max3, min1, min2, min3;
printf("Enter four integers:");
scanf_s("%d %d %d %d", &no1, &no2, &no3, &no4);
if(no1 > no2 || no1 < no2 && no3 > no4 || no3 < no4){
no1 > no2 ? (max1=no1) : (max1=no2);
no1 > no2 ? (min1=no2) : (min1=no1);
no3 > no4 ? (max2=no3) : (max2=no4);
no3 > no4 ? (min2=no4) : (min2=no3);
}
if(max1 > max2 || max1 < max2 && min1 > min2 || min1 < min2){
max1 > max2 ? (max3=max1) : (max3=max2);
min1 > min2 ? (min3=min2) : (min3=min1);
}
printf("The largest number is %d \n", max3);
printf("The smallest number is %d \n", min3);
}
然而,我不知道我做的是否正确。至少我认为它会帮助某人:)