| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 408 人关注过本帖
标题:我这个方法合适吗?求最长平台
只看楼主 加入收藏
yu497272290
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2013-11-2
结帖率:42.86%
收藏
 问题点数:0 回复次数:8 
我这个方法合适吗?求最长平台
一个从小到大排列的数组,这个数组中的一个平台就是连续的一段值相同的元素。例如:122333445中22,333等都是平台,333为最长平台。


我的程序 有错 没法运行

第15行有错,怎么错啦?

我这个思路可以吗?   还可以简化吗?


#include<stdio.h>
#define SIZE 1000
int maxLen(int , int);

main()
{
    int a[SIZE];
    int n, i;
    printf("Please input the number of intergers:");
    scanf("%d", &n);   //输入N个数
   
    for(i=0; i<=n-1; i++)  //依次读取N个数
        scanf("%d", a[i]);
   
    i=maxLen(a[], n);   //调用函数,求最长平台  怎么错的 ([Error]cpp:15: error: expected primary-expression before ']' token)
    printf("%d", i);
   
    return 0;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


int maxLen(int a[], int n)
{
    int b[1000]={0};     //b[]数组做辅助
    int j, pass, hold;
   
    for(j=0; j<=n-1; j++){
   
        if(a[j]==a[j+1])     //若相同,b[这个数]加一
            b[a[j]]++;         
        
    }
   
    for(pass=0; pass<=n-1; pass++){    //泡沫排序,把最大的数放在b[0]
        for(j=0; j<=n-2; j++){
            if(b[j]<b[j-1]){
                hold=b[j];
                b[j]=b[j+1];
                b[j+1]=hold;
            }
        }
    }
   
    return b[0];
   
}
搜索更多相关主题的帖子: include number 元素 
2013-12-07 10:38
ytlcainiao
Rank: 2
等 级:论坛游民
帖 子:48
专家分:74
注 册:2013-11-28
收藏
得分:0 
仍一枚硬币,正面和反面向上的概率都是0.5,但多次仍硬币,总会出现连续多次同一面的情况。我们把每次的实验结果记录下来,形成一个串(0,1分别代表正面、反面向上)。下面的程序统计出0或1最大连续出现的次数。试完善之。
    char* p = "100111000100101010001010111100000000100001111111000000";
    int len = strlen(p);
    int max_k = 1;
    int k = 1;
    for(int i=1; i<len; i++)
    {
        if(p[i]==p[i-1])
            k++;
        else
        {
            if(k>max_k) max_k = k;
            k = 1;
        }
    }
    printf("%d\n", max_k);
2013-12-07 10:45
ytlcainiao
Rank: 2
等 级:论坛游民
帖 子:48
专家分:74
注 册:2013-11-28
收藏
得分:0 
int maxLen(int[] , int);
2013-12-07 10:46
ytlcainiao
Rank: 2
等 级:论坛游民
帖 子:48
专家分:74
注 册:2013-11-28
收藏
得分:0 
在声明的时候就错了。。
2013-12-07 10:47
yu497272290
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2013-11-2
收藏
得分:0 
回复 4楼 ytlcainiao
声明改成  int maxLen(int[] , int);

但是15行怎么还是错的?
2013-12-07 10:55
yu497272290
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2013-11-2
收藏
得分:0 
题目要求必须要调用函数
2013-12-07 10:56
yu497272290
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2013-11-2
收藏
得分:0 
#include<stdio.h>
#define SIZE 1000
int maxLen(int[] , int);

main()
{
    int a[SIZE];
    int n, i;
    printf("Please input the number of intergers:");
    scanf("%d", &n);   //输入N个数
   
    for(i=0; i<=n-1; i++)  //依次读取N个数
        scanf("%d", a[i]);
   
    i=maxLen(a, n);   //调用函数,求最长平台  怎么错的 ([Error]cpp:15: error: expected primary-expression before ']' token)
    printf("%d", i);
   
    return 0;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


int maxLen(int a[], int n)
{
    int b[1000]={0};     //b[]数组做辅助
    int j, pass, hold;
   
    for(j=0; j<=n-1; j++){
   
        if(a[j]==a[j+1])     //若相同,b[这个数]加一
            b[a[j]]++;         
        
    }
   
    for(pass=0; pass<=n-1; pass++){    //泡沫排序,把最大的数放在b[0]
        for(j=0; j<=n-2; j++){
            if(b[j]<b[j-1]){
                hold=b[j];
                b[j]=b[j+1];
                b[j+1]=hold;
            }
        }
    }
   
    return b[0];
   
}


这个可以开始运行了,没错了,但是运行一办还是不行
2013-12-07 11:00
yu497272290
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2013-11-2
收藏
得分:0 
好像这个方法不太好。。。
2013-12-07 11:03
yu497272290
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2013-11-2
收藏
得分:0 
#include<stdio.h>
#define SIZE 1000
int maxLen(int[] , int);

main()
{
    int a[SIZE];
    int n, i;
    printf("Please input the number of intergers:");
    scanf("%d", &n);   //输入N个数
   
    printf("Please input the integers:");
   
    for(i=0; i<=n-1; i++)  //依次读取N个数
        scanf("%d", a[i]);
   
    i=maxLen(a, n);   //调用函数,求最长平台  
    printf("The longest level is:%d", i);
   
    return 0;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


int maxLen(int a[], int n)
{
    int length=1;
    int i;
   
    for(i=0; i<=n-1; i++){
        if(a[i]==a[i-length])
           length++;
    }
   
    return length;
}
这个可以运行 ,  最后一步按了确认  它就显示停止工作了。

为什么啊!!!?
2013-12-07 11:16
快速回复:我这个方法合适吗?求最长平台
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035369 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved