标题:请问这个问题要怎么编?
只看楼主
清风幽然
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2019-9-25
结帖率:66.67%
已结贴  问题点数:20 回复次数:4 
请问这个问题要怎么编?
You are given a file containing an unknown amount of numbers. Each number
is one of the numbers 1 to 9. A number can appear zero or more times and can
appear anywhere in the file. The number 0 indicates the end of the data. Some
sample data are:

5 3 7 7 7 4 3 3 2 2 2 6 7 4 7 7
2 2 9 6 6 6 6 6 8 5 5 3 7 9 9 9 0

Write a program to read the data once and print the number that appears the most in
consecutive positions and the number of times it appears. Ignore the possibility of a tie.
For the above data, output should be 6  5.

试了很多次都不对
搜索更多相关主题的帖子: and file data the number 
2019-09-26 16:25
自学的数学
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:46
帖 子:967
专家分:4146
注 册:2017-11-15
得分:0 
代码呢??
2019-09-26 16:37
清风幽然
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2019-9-25
得分:0 
回复 2楼 自学的数学
#include <stdio.h>
int main (){
 int s,n=0;
 FILE *in=fopen("a.txt","r");
 fscanf (in,"%d",&s);
 while (s!=0){
  if (s=s) { n++; }
  else if (n<n) { n=n; }
   fscanf (in,"%d",&s);    }
   printf ("%d %d",s,n);
   fclose(in);
}
 
  
2019-09-26 16:53
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
听不懂,下次自己写
程序代码:
#include <stdio.h>

int main( void )
{
    char digit_most = '0';
    unsigned count_most = 0;
    {
        FILE* f = fopen( "D:/sources/cpp006/src.txt", "r" );
        if( !f )
            return 1;

        char digit_last = '?';
        unsigned count_last = 0;
        for( ; ; )
        {
            int ch = fgetc( f );
            if( ch == EOF )
                ch = '0';
            else if( ch<'0' || ch>'9' )
                continue;

            if( ch == digit_last )
                ++count_last;
            else
            {
                if( count_last > count_most )
                {
                    digit_most = digit_last;
                    count_most = count_last;
                }

                digit_last = ch;
                count_last = 1;
            }

            if( ch == '0' )
                break;
        }

        fclose( f );
    }
    printf( "%c %u\n", digit_most, count_most );
}

2019-09-26 17:15
清风幽然
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2019-9-25
得分:0 
回复 4楼 rjsp
感谢!虽然你写的一部分还没学
2019-09-26 21:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-497024-1-1.html




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

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