简体   繁体   English

(C)代码无法在所有需要的条件下工作

[英](C) code cant work with all wanted conditions

#include <stdio.h>
#include <stdlib.h>

#define TEN 10 
int main ()
{
 int number = 0;
 int digit = 0;
 int last_digit = 0;
 int digit_sum = 0;
 int i = 0;
while (i == 0)
{
 printf("Please Enter A Positive Number! \n"); //explaining
 scanf("%d",&number);
 if (number > 0)
{
    i++;
}

}

    while (number > 0)
    {
    digit = number % TEN; //breaking number into digits
    number /= TEN;

    if (last_digit != digit) //comparing digits
    {
       last_digit = digit;
       digit_sum += digit;
    }


    }


    printf("The Sum Of The Digits Is : %d",digit_sum);
    return 0;

} }


the code will divide the number into digits and check if there are duped digits, in case there are, only one of them will be calculated for exmple: 3211 3+2+1, but my problem is thats the code wont work with numbers like 31211 Im thankful for any kind of help. 该代码会将数字分为数字,并检查是否有重复的数字,如果有的话,将仅计算其中一个:3211 3 + 2 + 1,但是我的问题是该代码无法使用类似数字的代码31211我感谢您提供的任何帮助。

The code doesn't work because there is no guarantee that duplicate's will appear consecutive manner. 该代码不起作用,因为不能保证重复项将连续出现。 your code handles that not the other ways. 您的代码无法通过其他方式来处理。 That's why it fails. 这就是为什么它失败了。

A simple solution would be to consider a 10 element array where you will keep count of which element appeared and which didn't. 一个简单的解决方案是考虑使用10个元素的数组,您将在其中记录出现的元素和未出现的元素。

The idea is to map the digits to the array indices of the 10 element array. 这个想法是将数字映射到10个元素数组的数组索引。 Intialized with 0 . 初始化为0

...
int digitAppeared[10]={0};
while (number)
{
    digit = number % TEN; //breaking number into digits
    number /= TEN;
    digit_sum  += (1 - digitAppeared[digit]) * digit;
    digitAppeared[digit] = 1;
}
...

To give you a clear idea this line basically checks whether the element appeared or not and as per the result it will add the digit. 为了让您有一个清晰的主意,此行基本上检查该元素是否出现,并根据结果将添加数字。

If digit D appeared then digitAppeared[D]=1 and if it didn't then digitAppeared[D]=0 . 如果出现数字DdigitAppeared[D]=1 ;如果没有出现,则digitAppeared[D]=0

We will add it to digitsum if it appears first time. 如果它是第一次出现,我们会将其添加到digitsum中。 That's why the (1-digitAppeared[D]) will tell us whether to add it or not. 这就是为什么(1-digitAppeared[D])会告诉我们是否添加它的原因。

    digit_sum  += (1 - digitAppeared[digit]) * digit;

Convert the number to string use itoa() . 使用itoa()将数字转换为字符串。 sort it and then walk through it , looking for unique number and do your calculation 对其进行排序,然后遍历,寻找唯一编号并进行计算

You can mark which digits were already added by using setting logical flags as represented by done in program below: 您可以通过设置逻辑标志来标记已经添加了哪些数字,如下面程序中的done所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEN 10 
int main ()
{
 int number = 0;
 int digit = 0;
 int last_digit = 0;
 int digit_sum = 0;
 int i = 0;
 int done[10];
while (i == 0)
{
 printf("Please Enter A Positive Number! \n"); //explaining
 scanf("%d",&number);
 if (number > 0)
{
    i++;
}

}
    memset(done,0,sizeof(done));
    while (number > 0)
    {
    digit = number % TEN; //breaking number into digits
    number /= TEN;
      if(done[digit] == 0)
      {
       digit_sum += digit;
       done[digit] = 1;
      }
    }


    printf("The Sum Of The Digits Is : %d",digit_sum);
    return 0;
}

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

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