如何解决这个C编程练习?

时间:2016-09-21 07:58:06

标签: c

这是我第一次学习C.所以我必须编写一个代码,可以从任何输入的数字中找到第二大数字。

代码是:

#include <stdio.h>
int main() {
    int x, max, max2;
    max = max2 = -1;

    while (scanf("%d", &x) != EOF) {
        if (x > max) {
            max2 = max;
            max = x;
        } else if (x > max2) {
            max2 = x;
        }
    }

    printf("%d\n", max2);
    return 0;
}

问题是,如果我在这里输入1 2 3 4 5,答案应该是4,因为4是第二个最大数字。但代码应适用于任何输入的数字。我简直无法理解。

我必须使用Linux终端和vim来解决它。我使用vim input在终端中创建了一个输入文件并创建了一个文件并在那里输入1 2 3 4 5。但是当我使用./a.out <input运行代码时,它仍显示5.如何解决此问题?

2 个答案:

答案 0 :(得分:0)

我让自己变得容易,并用数组替换你的输入。

int main (void)
{
char lst [] = { 10, 11, 2, 33 };  // my input (must be char because 
  // I use sizeof to determine the number of inputs)

int max = -1;   // biggest
int submax = -1;  // 2nd biggest

if (sizeof (lst) < 2)  // must be > 1 to find 2 biggest numbers
{
    printf ("bad");
    return 0;
}

for (int i = 0; i < sizeof (lst); i ++)
{
    int x = lst [i];
    if (x > max)
    {
        submax = max;
        max = x;  // new biggest makes the old biggest to 2nd biggest
    }
    else  // its not biggest but maybe 2nd biggest
    {
        if (x > submax)
        {
            submax = x;
        }
    }

    printf ("\n%d: check %d - max=%d, submax=%d\n", i, x, max, submax);
}

return 0;
}

答案 1 :(得分:0)

“输入的任何数字”是什么意思?非排序输入?否定也是? 这是一个函数,它获取正整数列表并返回第二个最大值,如果遇到错误则返回-1(参数不够)。为了适应您的需求。

 kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- <0N CpocsSs7CircuitCat#040200000000009a|6501646464644309|6501646464c90000-6503010117c80000-1.0.3|a40200003e3d8fd5|0000000e|0000000000000000
    kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- |RC USSDString=*1234*#|MSISDN=93707678224|
    kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- |NF NOT: src=ERROR_APPLICATION sev=SEVERITY_MAJOR id=010117c800000001
    kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049- de.siemens.advantage.in.featureframework.FeatureException: GenAcc> [0]theGenericAccess: No value available for SubsDMDB.Subscriber.LanguageID and type INTEGER
    kbl1infn8:CCC_USER_2049.0002:2016/09/20-17:00:03.560451-00540-03276-CCC_USER_2049-  at de.siemens.advantage.in.features.genericAccess.impl.DynamicAsciiBuffer$Handle.throwNotAvailableException(DynamicAsciiBuffer.java:1105)

--
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- <0N CpocsSs7CircuitCat#020200000000008a|6501646464644309|6501646464c90000-6501646464640000-1.1.1|a20200003cc
31dd2|0000000e|0000000000000000
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- |RC CdPA=173|CgPA=93705040139|
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- |NF NOT: src=ERROR_APPLICATION sev=SEVERITY_MAJOR id=6503010103c80016
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025- Exception in flexible core (e.g. during logic execution):de.siemens.advantage.in.featureframework.FeatureExc
eption: Call.checkIfCcOperationIsAllowed():  operation Call.playAnnouncement() only allowed within an open call control dialog
kbl1infn4:CCC_USER_1025.0009:2016/09/20-00:23:03.981403-25661-28403-CCC_USER_1025-  at de.siemens.advantage.in.features.flexDTMF.actions.dtmfActions.impl.DTMFActionsController.playAnnounc
ementList(DTMFActionsController.java:360)
--