C - 如何检查数字中显示的数字的时间

时间:2021-07-28 23:58:37

Help ME I have a code in C program and I need to check how many time a digit shows in the number. EX: 321 the sum: 6 (3+2+1)

帮助我我在C程序中有一个代码,我需要检查一个数字在数字中显示的时间。 EX:321总和:6(3 + 2 + 1)

EX2: 3211 the sum: 6 (3+2+1)

EX2:3211总和:6(3 + 2 + 1)

#include <stdio.h>

#define TEN 10
int main ()
{
    int number=0, temp=0, sum=0, remainder=0;

    do
    {
        printf("Enter number: ");
        scanf("%d", &number);

    }while (number<=0);

    temp=number;

    while(temp!=0)
    {
        remainder = temp % TEN;
        sum = sum + remainder;
        temp = temp/TEN;


    }

    printf("Sum of the numbers is: %d\n", sum);

    return 0;
 }

how to continue?

怎么继续?

2 个解决方案

#1


1  

You just have to keep track which digit is already covered. For that, you can keep one array of size 10 (one for each digit) with initial value false. when a digit is covered, change that digit flag to true. and check every time if the digit is covered or not.

您只需跟踪已覆盖的数字。为此,您可以保留一个大小为10的数组(每个数字一个),初始值为false。覆盖数字时,将该数字标志更改为true。并且每次检查数字是否被覆盖。

code:

int isvisited[TEN] = {0};
while(temp!=0)
{
    remainder = temp % TEN;

    if(!isvisited[remainder])
    {
        isvisited[remainder] = 1;
        sum = sum + remainder;
    }

    temp = temp/TEN;
}

Edit:

here, array of Integer is used to track if each digit is covered. to keep track each digit point to each element of the array.

这里,Integer数组用于跟踪每个数字是否被覆盖。跟踪每个数字点到数组的每个元素。

It will initialize with 0 value.

它将初始化为0值。

  digit  isvisited
      0      0
      1      0 
      2      0
      3      0
      4      0
      5      0
      6      0
      7      0
      8      0
      9      0

now suppose digit 1, 3 and 6 are visited. the values will be

现在假设访问了数字1,3和6。价值观将是

  digit  isvisited
      0      0
      1      1 
      2      0
      3      1
      4      0
      5      0
      6      1
      7      0
      8      0
      9      0

You will check if the isvisited[digit] is 0, change the isvisited[digit] to 1 and also add it to the sum. if its value is 1, that means it is already added to sum, and you continue the execution for next digit.

您将检查isvisited [digit]是否为0,将isvisited [digit]更改为1并将其添加到总和中。如果其值为1,则表示已将其添加到sum中,并继续执行下一个数字。

Note: in while loop, it is good practice to check like (temp > 0) instead of (temp !=0).

注意:在while循环中,最好检查like(temp> 0)而不是(temp!= 0)。

#2


0  

You could simply make an array of 10 elements and store for each digit 0..9 if it appears in the number. Then you could simply sum the digits which are present using the array.

您可以简单地创建一个包含10个元素的数组,并存储每个数字0..9(如果它出现在数字中)。然后你可以简单地将使用数组存在的数字相加。

#include <stdio.h>
#define TEN 10
int main ()
{
    int number=0, temp=0, sum=0, remainder=0, i;
    int ifElementPresent[TEN] = {0}; //stores 1 if digit (0..9) is present else 0; initialised to zero
    do
    {
        printf("Enter number: ");
        scanf("%d", &number);

    }while (number<=0);

    temp=number;

    while(temp!=0)
    {
        remainder = temp % TEN;
        ifElementPresent[remainder] = 1; //if a digit is present in the number
        temp = temp/TEN;
    }

    for(i = 0; i<TEN; i++) // in order to calculate the sum
    {
        if(ifElementPresent[i] == 1)sum += ifElementPresent[i]; 
    }

    printf("Sum of the numbers is: %d\n", sum);

    return 0;
 }

#1


1  

You just have to keep track which digit is already covered. For that, you can keep one array of size 10 (one for each digit) with initial value false. when a digit is covered, change that digit flag to true. and check every time if the digit is covered or not.

您只需跟踪已覆盖的数字。为此,您可以保留一个大小为10的数组(每个数字一个),初始值为false。覆盖数字时,将该数字标志更改为true。并且每次检查数字是否被覆盖。

code:

int isvisited[TEN] = {0};
while(temp!=0)
{
    remainder = temp % TEN;

    if(!isvisited[remainder])
    {
        isvisited[remainder] = 1;
        sum = sum + remainder;
    }

    temp = temp/TEN;
}

Edit:

here, array of Integer is used to track if each digit is covered. to keep track each digit point to each element of the array.

这里,Integer数组用于跟踪每个数字是否被覆盖。跟踪每个数字点到数组的每个元素。

It will initialize with 0 value.

它将初始化为0值。

  digit  isvisited
      0      0
      1      0 
      2      0
      3      0
      4      0
      5      0
      6      0
      7      0
      8      0
      9      0

now suppose digit 1, 3 and 6 are visited. the values will be

现在假设访问了数字1,3和6。价值观将是

  digit  isvisited
      0      0
      1      1 
      2      0
      3      1
      4      0
      5      0
      6      1
      7      0
      8      0
      9      0

You will check if the isvisited[digit] is 0, change the isvisited[digit] to 1 and also add it to the sum. if its value is 1, that means it is already added to sum, and you continue the execution for next digit.

您将检查isvisited [digit]是否为0,将isvisited [digit]更改为1并将其添加到总和中。如果其值为1,则表示已将其添加到sum中,并继续执行下一个数字。

Note: in while loop, it is good practice to check like (temp > 0) instead of (temp !=0).

注意:在while循环中,最好检查like(temp> 0)而不是(temp!= 0)。

#2


0  

You could simply make an array of 10 elements and store for each digit 0..9 if it appears in the number. Then you could simply sum the digits which are present using the array.

您可以简单地创建一个包含10个元素的数组,并存储每个数字0..9(如果它出现在数字中)。然后你可以简单地将使用数组存在的数字相加。

#include <stdio.h>
#define TEN 10
int main ()
{
    int number=0, temp=0, sum=0, remainder=0, i;
    int ifElementPresent[TEN] = {0}; //stores 1 if digit (0..9) is present else 0; initialised to zero
    do
    {
        printf("Enter number: ");
        scanf("%d", &number);

    }while (number<=0);

    temp=number;

    while(temp!=0)
    {
        remainder = temp % TEN;
        ifElementPresent[remainder] = 1; //if a digit is present in the number
        temp = temp/TEN;
    }

    for(i = 0; i<TEN; i++) // in order to calculate the sum
    {
        if(ifElementPresent[i] == 1)sum += ifElementPresent[i]; 
    }

    printf("Sum of the numbers is: %d\n", sum);

    return 0;
 }