打印

[原创] C++ 算法 求10个或N个数中最大值

我用C写的!汉字不支持,但是调试通过!

#include <stdio.h>
main()
{
int i,n,put;                    //n为要对比的数量,put是当前输入的数
int max;                       //max最大数
printf( "请输入要对比数的个数: ");
scanf( "%d" , &n);
for(i=1;i<=n;i++)
{
  printf( "请输入第%d个数: ",i);
  scanf( "%d" , &put);
  if(i==1)max=put;
  if(max<put)max=put;
}
printf( "最大数为%d: ",max);
}

TOP

#include"stdio.h"
void main()
{
int a[10],n,c,max=0;                //MAX是最大的数,N用来控制循环,c来控制
printf("输入一组数字:");
for(n=0;n<10;n++)
scanf("%d",&a);
max=+a[0]
for(n=0;n<10;n++)
{
if(max>a[n])
max=a[n]
}
printf("%d\n",max);
}

TOP

引用:
原帖由 梦我心永恒 于 2008-5-11 13:02 发表
#include"stdio.h"
void main()
{
int a[10],n,c,max=0;                //MAX是最大的数,N用来控制循环,c来控制
printf("输入一组数字:");
for(n=0;n
楼上的算法有很严重的问题!
当我输入10个数都是负数,
你在用你的程序试试看结果吧,
最大数是0!
max的初值一定不能自己定义,
因为世界上没有能用一串数字表示的最小数,
所以max的初值=a[0]就没有问题了!
不知道我说得没有道理,
请大家多多指正!

TOP

汗,楼上的,不好意思,我才开始学!
希望给我修改下上面的程序!

TOP

回复 14# 的帖子

我就是13楼的!
你可以参考下我的C语言程序,
在11楼有写,
我想应该可以完全在C++上运行!
至于你的程序,
也就是按照你当时的思路去修改的话,
我可以给你改下,
但是我用的是C写的,
应该没有关系吧!

#include"stdio.h"
main()
{
int a,n,max;
for(n=0;n<10;n++)
{
printf("请输入第%d个数:",n+1);
scanf("%d",&a);
if(n==0)max=a;
if(max<a)max=a;
}
printf("max=%d\n",max);
}

TOP

呵呵,大概的就这样,谢谢,主要一些算法还不清晰,所以……

TOP

根据楼主定义的10个数组数字做的,结果是88,在VC6.0下编译通过。
#include"stdio.h"
#include <iostream>
int main()
{
using namespace std;
int i,t;
int x[10] = {20,14,22,10,88,45,78,56,43,28};
    for(i=1;i<=9;i++)
                if (x[0]<x)
                {
                        t=x[0];
                        x[0]=x;
                        x=t;
                }
    cout<<x[0]<<endl;
return 0;

}
研究VB和C++技术,X杂志忠实读者

TOP

用 STL 可能会很简单

TOP

引用:
原帖由 521322132 于 2008-5-28 05:58 发表
根据楼主定义的10个数组数字做的,结果是88,在VC6.0下编译通过。
#include"stdio.h"
#include
int main()
{
using namespace std;
int i,t;
int x[10] = {20,14,22,10,88,45,78,56,43,28};
    for(i=1;i ...
呵呵,这个标签很麻烦……不知道有什么方法解决这个问题,我刚才的帖子把i换成j了……orz
我爱X档案~~

TOP

不好意思我不会C++, 我写的VC你看行不
#include<stdio.h>
void main()
{
        int i,h,a[10];
        for(i=0;i<10;i++)
                scanf("%d",&a);
        h=a[0];
        for(i=1;i<10;i++)
        {
                if(a>h)
                        h=a;               
        }
        printf("%d",h);
}

TOP

Processed in 0.164403 second(s), 5 queries, Gzip enabled.