用c语言编程怎么输入一批个数是不确定的非零整数,以0为结束符,输出其中的最大值

如题所述

#include "stdio.h"

int main()

{

    int n,max=0;

    while(1)

    {

        scanf("%d",&n);

        if(n==0)

             break;

        if(n>max)

             max=n;

 

    }

    printf("输入的数据中,最大值为%d\n",max);

}

结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-15
#include<stdio.h>
Main()
{
int x,max;
printf(“输入一批非零整数,以0为结束符”);
scanf(“%d”,&x);
max = x; /*现将第一个数赋值给变量max*/
for(; x!=0 ;)
{
scanf(“%d”,&x);
if(max<x)
max=x; /*输入值比变量max大,用当前值替换max*/
}
printf(“max=%d\n”,max); /*输出最大值*/
}
第2个回答  2013-04-15

如果是非0整数的话,那么max只能默认是输入的第一个数,因为我们不知道输入的下限是多少。

#include<stdio.h>
int main()
{
int max, n;
scanf("%d", &n);
max = n;
while(scanf("%d", &n), n)
{
if(n>max)
{
max = n;
}
}
printf("max = %d\n", max);
return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网