Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 查找给定整数中的最大偶数_C_Arrays - Fatal编程技术网

C 查找给定整数中的最大偶数

C 查找给定整数中的最大偶数,c,arrays,C,Arrays,我正在上一门在线C课程,但教授拒绝回复电子邮件,我需要一些帮助 无论如何,我们的任务是编写一个程序,从用户那里获取一个整数,并找出最大的偶数,以及该数字在给定整数中出现的次数 #include <stdio.h> void extract(int); void menu(void); int main() { menu(); } void menu() { int userOption; int myValue; int extractDigi

我正在上一门在线C课程,但教授拒绝回复电子邮件,我需要一些帮助

无论如何,我们的任务是编写一个程序,从用户那里获取一个整数,并找出最大的偶数,以及该数字在给定整数中出现的次数

#include <stdio.h>

void extract(int);
void menu(void);


int main() {
    menu();
}

void menu() {
    int userOption;
    int myValue;
    int extractDigit;

    do {
        printf("\nMENU"
            "\n1. Test the function"
            "\n2. Quit");
        scanf("%d", &userOption);

        switch (userOption) {
        case 1:
            printf("Please enter an int: ");
            scanf("%d", &myValue);

            extractDigit = digitExtract(myValue);

            break;

        case 2:
            printf("\nExiting . . . ");
            break;

        default:
            printf("\nPlease enter a valid option!");
        }
    } while (userOption != 2);
}

void digitExtract(int userValue) {
    int tempValue;
    int x;
    int myArr[10] = { 0 };

    tempValue = (userValue < 0) ? -userValue : userValue;

    do {
        myArr[tempValue % 10]++;
        tempValue /= 10;
    } while (tempValue != 0);


    printf("\nFor %d:\n", userValue);
    for (x = 0; x < 10; x++) {
        printf("\n%d occurence(s) of %d",myArr[x], x);
    }   
}
#包括
空洞提取(int);
作废菜单(作废);
int main(){
菜单();
}
无效菜单(){
int用户选项;
int-myValue;
整数位;
做{
printf(“\n菜单”
“\n1.测试函数”
“\n2.退出”);
scanf(“%d”、&userOption);
开关(用户选项){
案例1:
printf(“请输入整数:”);
scanf(“%d”、&myValue);
extractDigit=DigiteExtract(myValue);
打破
案例2:
printf(“\nxiting…”);
打破
违约:
printf(“\n请输入有效选项!”);
}
}while(userOption!=2);
}
void digitExtract(int userValue){
int值;
int x;
int myArr[10]={0};
tempValue=(userValue<0)?-userValue:userValue;
做{
myArr[tempValue%10]+;
tempValue/=10;
}while(tempValue!=0);
printf(“\n%d:\n”,userValue);
对于(x=0;x<10;x++){
printf(“\n%d次,共%d次”,myArr[x],x);
}   
}
我已经让程序显示奇偶数字和它的出现

我唯一坚持的部分是让程序只显示最大的偶数数字和它的出现。我尝试过的一切要么破坏了程序的逻辑,要么产生了一些奇怪的输出

关于我应该如何进行的任何提示或想法


提前感谢。

从最大的偶数数字到最小的偶数数字进行循环

for (x = 8; x >=0; x-=2)
{
    if(myArr[x]>0) //if myArr[x]=0 then x does not exist
    {
        printf("%d occurs %d times",x,myArr[x]);
        break; //we have found our maximum even digit. No need to proceed further
    }

}

注意:要优化,您应该只计算和存储偶数数字的出现次数。

为什么还要使用额外的循环?要查找整数中最大的偶数及其出现次数,修改第一个循环就足够了

考虑以下几点(未经测试,但我希望您能理解):

int-tempValue;
int x;
int myArr[10]={0};
int maxNum=0;
tempValue=(用户值<0)-userValue:userValue;
做{
int currNum=tempValue%10;
myArr[currNum]++;
tempValue/=10;
如果(currNum%2==0&&currNum>maxNum)
maxNum=currNum;
}while(tempValue!=0);

在此之后,
maxNum
应该包含最大的偶数,而
myArr[maxNum]
应该是它出现的次数。

只需按降序(8、6、4)检查偶数,如果
myArr[x]>0,则打印并断开。您可以使用10的数组来存储计数,当用户退出时,您会找到最大计数,然后打印索引minor:注意:如果
myValue==INT\u MIN
,则代码会出现问题。谢谢,执行if语句检查最大偶数位数是否最有效?否。我们的想法是从可能的最大偶数位数开始检查,这是
8
。如果出现
8
,则计数是我们的答案,我们将中断。如果
8
未出现,我们将检查下一个最大的偶数,即
6
,依此类推。啊,谢谢!我了解“现在休息”的用法。:)
int tempValue;
int x;
int myArr[10] = { 0 };

int maxNum = 0;

tempValue = (userValue < 0) ? -userValue : userValue;

do {
    int currNum = tempValue % 10;

    myArr[currNum]++;
    tempValue /= 10;

    if (currNum % 2 == 0 && currNum > maxNum)
        maxNum = currNum;
} while (tempValue != 0);