【问题标题】:not able to scan the string with spaces无法扫描带空格的字符串
【发布时间】:2016-05-20 06:08:05
【问题描述】:

https://www.codechef.com/problems/LADDU 无法在代码第 12 行扫描数组“work”中的字符串。

     #include<stdio.h>

     int main()
     {
        long long int i,j,T,actv,points,a,b,c;
        char origin[100],work[100];
        scanf("%lld",&T);
        while(T--)
        {
            points=0;
            scanf("%lld %s",&actv,origin);
            for(i=0;i<actv;i++)
            {
                    printf("hie\n");
                    scanf("%[^\n]s",work);
                    printf("hello\n");
            }
       }

       return 0;
    }

【问题讨论】:

  • scanf("%[^\n]s",work); --> scanf(" %99[^\n]%*c", work);
  • 速度是这个 codechef 问题的一个重要考虑因素。 (size_t 而不是int
  • 根据问题集,没有要输入的带空格的字符串。
  • 为了便于阅读和理解:1) 通过空行分隔代码块(for、if、else、while、do...while、switch、case、default) 2) 遵循公理:每行只有一个语句,并且(最多)每个语句有一个变量声明。 3) 变量名称应该表明内容或用法(或者更好,两者兼而有之)。
  • 不要使用制表符进行缩进。因为每个文字处理器/编辑器的制表位/制表宽度设置不同。建议为每个缩进级别使用 4 个空格,因为即使使用可变宽度字体也是如此。强烈建议:不要省略可选的大括号“{”和“}”,因为它们使代码更易于阅读/理解,并在将来执行编辑时最大限度地减少某些类型的格式错误。

标签: c arrays scanf


【解决方案1】:

使用fgets() 代替scanf() 来扫描带有空格的字符串。

fgets(work,sizeof(work),stdin);

注意: fgets() 带有换行符。所以

size_t n = strlen(work);
if(n>0 && work[n-1] == '\n')
{
   work[n-1] = '\0';
}

【讨论】:

    【解决方案2】:

    使用fgets 代替scanf。

    #define BUFFERSIZE sizeof(work)
    
    if (fgets(work, BUFFERSIZE , stdin) != NULL)
    {
       // your stuff
    }
    

    获取带空格的字符串。

    如果您的 C-stirng 比 BUFFERSIZE 短,则 null 终止符之前的最后一个字符将是 '\n'

    【讨论】:

      【解决方案3】:

      scanf("%lld %s",&amp;actv,origin);

      行执行后,换行保持不消耗。

      scanf("%[^\n]s",work);

      由于%[^\n]不接受换行符,它不执行读取。
      (另外,s 不是必需的。例如%[^\n]s --> %[^\n]

      所以,在下面进行更改。

      scanf("%lld %s%*c",&amp;actv,origin);//%*c 用于跳过一个字符(换行符)或while(getchar()!='\n');//跳过换行符
      ...
      scanf("%99[^\n]%*c",work);

      【讨论】:

        【解决方案4】:

        对于数字的快速读/写使用:

        #include <stdio.h>
        
        void fastRead( size_t *a );
        void fastWrite( size_t a );
        
        inline void fastRead(size_t *a)
        {
            int c=0;
            // note: 32 is space character
            // consume leading trash
            while (c<33) c=getchar_unlocked();
        
            // initialize result value
            *a=0;
        
            // punctuation parens, etc are show stoppers
            while (c>47 && c <58)
            { // then in range 0...9
                *a = (*a)*10 + (size_t)(c-48);
                c=getchar_unlocked();
            }
            //printf( "%s, value: %lu\n", __func__, *a );
        } // end function: fastRead
        
        
        inline void fastWrite(size_t a)
        {
            char snum[20];
            //printf( "%s, %lu\n", __func__, a );
        
            int i=0;
            // decompose 'a' into char array
            do
            {
                // 48 is numeric character 0
                snum[i++] = (char)((a%10)+(size_t)48);
                a=a/10;
            }while(a>0);
        
            i=i-1; // correction for overincrement from prior 'while' loop
        
            while(i>=0)
            {
                putchar_unlocked(snum[i--]);
            }
            putchar_unlocked('\n');
        } // end function: fastWrite
        

        除了以上两个函数,下面是一个使用这些函数的典型程序:

        #define MAX_VALUE (1000000)
        
        int array[ MAX_VALUE +1 ];
        
        int main( void )
        {
            // get number of test cases
            size_t  numTestCases;
            fastRead( &numTestCase );
            //scanf( "%lu", &numTestCases );
            //printf( "%s, Number of Test Cases: %lu\n", __func__, numTestCases);
        
            // accumulate test cases, sorted
            for( size_t i=0; i<numTestCases; i++ )
            {
                size_t value;
                fastRead( &value );
                //scanf( "%lu", &value );
                array[value]++;
            }
        
            // output the unique values, assending
            for( size_t i=0; i<MAX_VALUE; i++ )
            {
                if( array[i] )
                {
                    fastWrite( i );
                    //printf( "%s. %lu\n", __func__, i );
                }
            }
        
        
            return 0;
        }
        

        fastRead 和 fastWrite 函数(而不是 printf 和 scanf)将大大加快您的代码速度。

        现在你只需要实现问题集。

        注意:读取字符串:

        size_t i = 0;
        while( i < sizeof( inputArray ) && (ch = getchar_unlocked()) && ' ' != ch )
        {
            inputArray[i] = ch;
            i++;
        }
        inputArray[i] = '\0';
        

        您可以使用其他字符串分隔符,例如 '\n',而不是 ' '

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-23
          • 2019-12-09
          • 1970-01-01
          • 2014-12-29
          • 2020-06-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多