从数组中删除所有数字

Remove all digits from array

写一个函数remove_digits接收两个int类型的数组。第一个数组包含一些整数,第二个数组是一个数字数组。有必要从第二个数组中删除第一个数组中存在的所有数字。

函数 returns 1 如果弹出是 successful.The function returns 1 如果弹出成功,如果数字数组由于某种原因不正确则为 0,如果数组包含小于 0 或大于 9 的值,或者其中一个成员重复。

示例 1:

int first[2]={12345, -12345},second[2]={3,5};
OUTPUT: 124 -124

示例 2:

int first[5]={25, 235, 1235, 252, 22552255},second[3]={2,3,5};
OUTPUT: 0 0 1 0 0

我的算法:

代码:

#include <stdio.h>
#include <stdlib.h>
int sum_of_digits(int n) {
  int i, sum = 0;
  while (n > 0) {
    sum++;
    n /= 10;
  }
  return sum;
}

int divide(int n) {
  int num_of_digits = sum_of_digits(n);
  switch (num_of_digits) {
  case 1:
    break;
  case 2:
    break;
  case 3:
    n /= 10;
    break;
  case 4:
    n /= 100;
    break;
  case 5:
    n /= 1000;
    break;
  case 6:
    n /= 1000;
    break;
  case 7:
    n /= 10000;
    break;
  case 8:
    n /= 100000;
    break;
  case 9:
    n /= 1000000;
  default:
    break;
  }
  return n;
}

int remove_digits(int *first, int n, int *second, int vel) {
  // first - removing digits from second
  // second - searching for digits
  int i, j, num, digit, neg = 0;
  for (i = 0; i < vel; i++) {
    // invalid digit
    if (second[i] < 0 || second[i] > 9)
      return 0;
    for (j = i + 1; j < vel; j++)
      // repeated digit
      if (second[j] == second[i])
        return 0;
  }
  for (i = 0; i < n; i++) {
    // negative case
    if (first[i] < 0) {
      first[i] = abs(first[i]);
      neg = 1;
    }
    num = first[i];
    while (num > 0) {
      digit = num % 10;
      for (j = 0; j < vel; j++)
        if (second[j] == digit)
          // remove last digit
          first[i] = divide(first[i]) - digit;
      num /= 10;
    }
    if (first[i] <= 0)
      first[i] = 0;
    if (neg == 1)
      first[i] *= -1;
  }
  return 1;
}

int main() {
  int first[2] = {12345, 12345}, second[2] = {3, 5}, i;
  remove_digits(first, 2, second, 2);
  for (i = 0; i < 2; i++)
    printf("%d ", first[i]);
  return 0;
}

我的输出:4 4

你能帮我修改我的算法使其正常工作吗?

针对您的问题的简化方法如下,

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

int removeDigit(int src, int digit){
    int neg = (src < 0)?-1:1;
    int num = abs(src);
    src = 0;
    
    //remove digit
    while(num){
        int num_digit = num%10;
        if(num_digit != digit){
            src = src * 10 + num_digit;
        }
        num /= 10;
    }
    
    //reverse number
    while(src){
        num = num * 10 + src%10;
        src /=10;
    }
    
    return num*neg;
}


int remove_digits(int *first, int n, int *second, int m) {
  // first - removing digits from second
  // second - searching for digits
  int i, j;
  for (i = 0; i < m; i++) {
    // invalid digit
    if (second[i] < 0 || second[i] > 9)
      return 0;
    for (j = i + 1; j < m; j++)
      // repeated digit
      if (second[j] == second[i])
        return 0;
  }
  
  
  for (i = 0; i < n; ++i) {
    for(j =0; j<m; ++j){
        first[i]= removeDigit(first[i],second[j]);
    }
  }
  return 1;
}



int main() {
    
    {
      printf("Test 1\n");
      
      int first[] = {12345, 12345}, second[] = {3, 5}, i;
      
      remove_digits(first, sizeof(first)/sizeof(first[0]), second, sizeof(second)/sizeof(second[0]));
      
      for (i = 0; i < sizeof(first)/sizeof(first[0]); i++)
        printf("%d ", first[i]);
    }
    {
      printf("\n\nTest 2\n");
      
      int first[] = {25, 235, 1235, 252, 22552255}, second[] = {2,3,5}, i;
      
      remove_digits(first, sizeof(first)/sizeof(first[0]), second, sizeof(second)/sizeof(second[0]));
      
      for (i = 0; i < sizeof(first)/sizeof(first[0]); i++)
        printf("%d ", first[i]);
    }

  return 0;
}