Manchester


私信TA

用户名:wenyajie

访问量:312873

签 名:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

等  级
排  名 1
经  验 62755
参赛次数 1
文章发表 188
年  龄 0
在职情况 学生
学  校 Xiamen University
专  业 计算机科学

  自我简介:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

解题思路:
1.输入一串数字,输出其中最大的数字,我们定义一个存最大值的变量max,把输入的数中的第一个数分离出来输入,让首先让max等于这个数;

2.之后每次输入一个数让它和max比较,比max大,则更新max;

3.最后所有数字输完后,输出max;

注意事项:
若只输入-1,不能把-1输出出来,所以在整个求最大数的过程外面,需要加一个判断,如果输入的第一个就是-1的话,直接结束;


参考代码:

c:

#include <stdio.h>

int main()
{
    int num, max;
    scanf( "%d", &num );
    if ( num != -1 )
    {
        max = num;
        while ( num != -1 )
        {
            if ( num > max )
                max = num;
            scanf( "%d", &num );
        }
        printf( "%d", max );
    }
    return 0;
}


c++:

#include<iostream>
using namespace std;

int main()
{
    int num, max;
    cin>>num;
    if ( num != -1 )
    {
        max = num;
        while ( num != -1 )
        {
            if ( num > max )
                max = num;
            cin>>num;
        }
        cout<<max;
    }
    return 0;
}

别忘点赞哦-.-

 

0.0分

35 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

#include<stdio.h>
int main()
{
	int num,max=0;
	scanf("%d",&num);
	max=num;
	while(num!=-1)
	{
	    scanf("%d",&num);
	    
		if((num!=-1)&&num>max)
		{
			max=num;
		}
	}
	printf("%d",max);
	return 0;
}
2022-01-03 13:13:24
#include<stdio.h>
void main()
{
	int num,max=0;
	while((scanf("%d",&num))!=EOF&&num!=-1)
	{
		if(num>max)
		{
			max=num;
		}
		scanf("%d",&num);
	}
	printf("%d",max);
}
2021-08-11 20:48:55
#include <stdio.h>
void main()
{
    int a[99], max;
    for (int i = 0; i < 9; i++)
    {
        scanf("%d", &a[i]);
        if (i == -1)
        {
            break;
        }
    }
    max = a[1];
    for (int i = 0; i < 9; i++)
    {
        if (a[i + 1] > max)
        {
            max = a[i + 1];
        }
    }
    printf("%d", max);
}
求助啊!
2020-11-24 14:16:27
int main()
{
	int a = 0, max;
	scanf("%d", &a);
	max = a;
	while (a != -1)
	{
		scanf("%d", &a);
		if (a > max)
			max = a;
	}
	if(max!=-1)
		printf("%d", max);

	return 0;
}
请问哪种情况下会出错,系统判定错误14%,我实在想不出错哪了,求指导
2019-04-29 18:13:15
请问,不应该先输入scanf( "%d", &num );后判定if ( num > max )
                max = num;吗?
2019-04-27 22:08:50
  • «
  • 1
  • »