注册 登录
编程论坛 C语言论坛

请问这个问题要怎么编?

清风幽然 发布于 2019-09-26 16:25, 2279 次点击
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.

试了很多次都不对
4 回复
#2
自学的数学2019-09-26 16:37
代码呢??
#3
清风幽然2019-09-26 16:53
回复 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);
}
 
  
#4
rjsp2019-09-26 17:15
听不懂,下次自己写
程序代码:
#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 );
}

#5
清风幽然2019-09-26 21:27
回复 4楼 rjsp
感谢!虽然你写的一部分还没学
1