简体   繁体   English

扫描 C 中的 300 位数字

[英]Scanf a number with 300 digits in C

There is a question: I should scan a number with 300 digits and print the sum the digits but I cant scan it with long long int and I dont know what to do.有一个问题:我应该扫描一个 300 位数字并打印数字总和,但我无法用long long int扫描它,我不知道该怎么做。

You can scan the number as a string with fgets() or even simply one byte at a time with getchar() :您可以使用fgets()将数字扫描为字符串,甚至可以使用getchar()一次扫描一个字节:

#include <stdio.h>

int main() {
    int c;
    int sum = 0;
    while ((c = getchar()) != EOF) {
        if (c >= '0' && c <= '9')
            sum += c - '0';
        else
            break;
    }
    printf("sum: %d\n", sum);
    return 0;
}

I you must use scanf() , here is an alternative:我必须使用scanf() ,这是另一种选择:

#include <stdio.h>

int main() {
    char buf[2];
    int sum = 0;
    while (scanf("%1[0-9]", buf) == 1) {
        sum += *buf - '0';
    }
    printf("sum: %d\n", sum);
    return 0;
}
#include <stdio.h>

int main() 
{
    char digit;
    int sum = 0;
    while ( scanf("%c", &digit) != EOF) 
    {
         sum += digit - '0';
    }
    printf("sum: %d\n", sum);
    return 0;
}

In such a case you need to enter a number as a string.在这种情况下,您需要输入一个数字作为字符串。

To enter a number you should use the standard function fgets .要输入数字,您应该使用标准 function fgets The corresponding character array must have 302 characters: 300 characters for digits, one character for the new line character '\n' that is appended by the function fgets to the entered string and one character for the terminating zero character '\0' of the string.对应的字符数组必须有 302 个字符:300 个字符用于数字,1 个字符用于fgets fget 附加到输入字符串的换行符'\n'和 1 个字符用于结尾的零字符'\0'细绳。

If the new line character '\n' is not present in the string it means that the user entered more than 300 characters.如果字符串中不存在换行符'\n' ,则表示用户输入了超过 300 个字符。

Also the user can enter a number with leading or trailing spaces.用户也可以输入带有前导或尾随空格的数字。

You need to check that the entered string contains a valid number.您需要检查输入的字符串是否包含有效数字。

Here is a demonstration program.这是一个演示程序。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int is_valid_number( const char *number )
{
    while ( isblank( ( unsigned char )*number ) ) ++number;
    
    int valid = *number != '\0';
    
    if ( valid )
    {
        while ( *number && '0' <= *number && *number <= '9' ) ++number;
        while ( isblank( ( unsigned char )*number ) ) ++number;
        valid = *number == '\0';
    }       
    
    return valid;
}

int main( void )
{
    enum { N = 300 };
    char number[N + 2] = { 0 };

    printf( "Enter a non-negative number (no greater than %d digits): ", N );

    fgets( number, sizeof( number ), stdin );
    
    int valid = strchr( number, '\n' ) != NULL;
    
    if ( valid )
    {
        number[ strcspn( number, "\n" ) ] = '\0';
        valid = is_valid_number( number );
    }
    
    if ( !valid )
    {
        puts( "Invalid number." );
    }
    else
    {
        unsigned int sum = 0;

        for ( char *digit = number; *digit; ++digit )
        {
            sum += *digit - '0';
        }

        printf( "The sum of digits = %u\n", sum );
    }
}

Its output might look like它的 output 可能看起来像

Enter a non-negative number (no greater than 300 digits): 1234567890123456789012345678901234567890
The sum of digits = 180

The most straight forward solution seems to be to repeatedly read in 1-digit ints and sum them up while that works.最直接的解决方案似乎是重复读取 1 位整数并在可行时对其进行总结。
One digit numbers can easily be read into an int (no long needed) and even the sum of 300 digits will not exceed an int.一位数字可以很容易地读入一个 int(不再需要),即使 300 位数字的总和也不会超过一个 int。

#include <stdio.h>

int main()
{
    int digit=0;
    int sum=0;
    while(1==scanf("%1d",&digit))sum+=digit;
    printf("sum:%d\n", sum);

    return 0;
}

you can use char to scan each digit.您可以使用 char 扫描每个数字。 also in this way you do not need string.同样通过这种方式你不需要字符串。 enter image description here在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM