如何在没有数组的情况下找到 C 中最大整数的出现?
How to find occurrences of the largest integer in C without array?
在我的代码中,我能够在一组要求输入的数字中找到最大的整数。我无法找到我输入的最大整数出现的次数。我觉得我的问题出在 "if and else" 语句上。
例如,当满足第一个 if 语句时,我认为它会递增 "count" 一次并跳过所有其他 "if and else" 语句并执行最后一个打印函数。所以计数总是以 2
结束。
如何让 count
计算最大整数出现的次数?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main ()
{
int count;
int a,b,c,d,e;
count = 1;
printf("Enter 5 integers within 1-10:\n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (e >= a
&& e >= b
&& e >= c
&& e >= d){
printf ("Largest integer is %d\n", e);
count++;
}
else if (d >= a
&& d >= b
&& d >= c
&& d >= e){
printf ("Largest integer is %d\n", d);
count++;
}
else if (c >= a
&& c >= b
&& c >= d
&& c >= e){
printf ("Largest integer is %d\n", c);
count++;
}
else if (b >= a
&& b >= c
&& b >= d
&& b >= e){
printf ("Largest integer is %d\n", b);
count++;
}
else {
printf ("Largest is %d\n", a);
count++;
}
printf ("Largest integer occurred %d times.\n", count);
system ("pause");
return 0;
}
我认为你把事情复杂化了。您可以只有一个变量,而不是五个变量,并在循环中输入它,同时保存最大值和计数:
#define NUMBER_OF_VARS 5
int i;
int input;
int curr_max = INT_MIN;
int count = 0;
for (i = 0; i < NUMBER_OF_VARS; ++i) {
printf("Enter an integer: ");
scanf("%d", &input);
if (input > curr_max) {
curr_max = input;
count = 1;
} else if (input == curr_max) {
++count;
}
}
printf ("Largest integer is %d, appearing %d times\n", curr_max, count);
如果您不需要 5 个变量,那么 Mureinik 有答案。如果你必须有 5 个变量,我会这样做:
int max = -9999;
if (a > max) {
max = a;
}
if (b > max) {
max = b;
}
/* repeat for c d and e */
在我的代码中,我能够在一组要求输入的数字中找到最大的整数。我无法找到我输入的最大整数出现的次数。我觉得我的问题出在 "if and else" 语句上。
例如,当满足第一个 if 语句时,我认为它会递增 "count" 一次并跳过所有其他 "if and else" 语句并执行最后一个打印函数。所以计数总是以 2
结束。
如何让 count
计算最大整数出现的次数?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main ()
{
int count;
int a,b,c,d,e;
count = 1;
printf("Enter 5 integers within 1-10:\n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
if (e >= a
&& e >= b
&& e >= c
&& e >= d){
printf ("Largest integer is %d\n", e);
count++;
}
else if (d >= a
&& d >= b
&& d >= c
&& d >= e){
printf ("Largest integer is %d\n", d);
count++;
}
else if (c >= a
&& c >= b
&& c >= d
&& c >= e){
printf ("Largest integer is %d\n", c);
count++;
}
else if (b >= a
&& b >= c
&& b >= d
&& b >= e){
printf ("Largest integer is %d\n", b);
count++;
}
else {
printf ("Largest is %d\n", a);
count++;
}
printf ("Largest integer occurred %d times.\n", count);
system ("pause");
return 0;
}
我认为你把事情复杂化了。您可以只有一个变量,而不是五个变量,并在循环中输入它,同时保存最大值和计数:
#define NUMBER_OF_VARS 5
int i;
int input;
int curr_max = INT_MIN;
int count = 0;
for (i = 0; i < NUMBER_OF_VARS; ++i) {
printf("Enter an integer: ");
scanf("%d", &input);
if (input > curr_max) {
curr_max = input;
count = 1;
} else if (input == curr_max) {
++count;
}
}
printf ("Largest integer is %d, appearing %d times\n", curr_max, count);
如果您不需要 5 个变量,那么 Mureinik 有答案。如果你必须有 5 个变量,我会这样做:
int max = -9999;
if (a > max) {
max = a;
}
if (b > max) {
max = b;
}
/* repeat for c d and e */