[英]Finding the largest and smallest integers in C
正如我在另一個問題中提到的,我一直在用KN King's C Programming:A Modern Approach(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章!
干杯安德魯
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()
函數的實現swap()
練習。
另一種方式是這樣的:
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語句;)
在本章的范圍內:
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;
}
我有同樣的書,我承認,這個程序讓我很頭疼。 這對初學程序員來說有點棘手。
首先,比較第一對整數(代碼中的a和b),並將局部最小值和最大值存儲在某處。 對第二對做同樣的事情。 然后比較局部最小值以獲得全局最小值,並使用最大值執行相同的操作。 不超過四個if。
#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;
}
有更好的方法可以解決這個問題,其中一些在這里展示,但本書將在后面的章節中介紹它們。
我也正在經歷KN 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 }
#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);
}
我設法用甚至不到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);
}
但是我不知道我做的是不是做對了。 至少我認為它會幫助別人:)
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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.