数组中的分割数字

时间:2018-10-20 09:02:53

标签: c arrays cs50

我完全是新手,我在C语言中遇到问题。所以我有数组:

A[5] = {14, 2, 7, 3, 2};

我想做这样的事情:

A[6] = {1, 4, 2, 7, 3, 2};

从14到1,4.知道怎么做吗?

所以我用Cs50来解决问题。这是链接:https://docs.cs50.net/2018/x/psets/1/credit/credit.html

这是我的代码:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>

int main(void)
{
    long long cc_number = 0;

    do
    {
        printf("Number: ");
        cc_number = get_long_long();
    }
    while(cc_number <= 0);

    int num_of_digits = 0;
    long long valid = cc_number;

    while(valid > 0)
    {
        valid /= 10;
        num_of_digits++;
    }

    if (num_of_digits != 13 && num_of_digits != 15 && num_of_digits != 16 )     //checking if number have more or less than 13,15,16 digits
    {
        printf("Number is invalid!\n");
    }

    long long k = 1;                //create array for store each number from the card
    int A[16], d, num = 0;
    for(num = 0; num < 16; num++)
    {
        d = (cc_number/(1*k)) % 10;
        A[num] = d;
        k *=10;
    }

///////////////////////////////////////////////////////////////////////
    if (num_of_digits == 16)
    {
        for (int i = 0; i < 16; i = i + 2)
        {
            A[i] *= 2;                          // multiplay each second digit by 2
            printf("this is %i\n", A[i]);       
        }
    }

    else if (num_of_digits == 15 || num_of_digits == 13 )                   
    {
        int sum = 0;

         for (int i = 1; i < 15; i = i + 2)
        {
            int y = A[i];
            A[i] *= 2;                          // multiplay each second digit by 2
            if (A[i] > 9)                       // try to split digit from array
            {
                y = A[i] % 10;
                A[i] /= 10;
            }

            sum += A[i];
            printf("this is %i\n", A[i]);
        }

    printf("this is sum %i\n", sum);
    }

///////////////////////////////////////////////////////////////////////////


}

这就是我想要做的:

  

为便于讨论,让我们首先在其他数字下划线,   以数字的倒数第二个开头:

    378282246310005

    Okay, let’s multiply each of the underlined digits by 2:

    7•2 + 2•2 + 2•2 + 4•2 + 3•2 + 0•2 + 0•2

    That gives us:

    14 + 4 + 4 + 8 + 6 + 0 + 0

    Now let’s add those products' digits (i.e., not the products themselves) together:

    1 + 4 + 4 + 4 + 8 + 6 + 0 + 0 = 27

    Now let’s add that sum (27) to the sum of the digits that weren’t multiplied by 2:

    27 + 3 + 8 + 8 + 2 + 6 + 1 + 0 + 5 = 60

    Yup, the last digit in that sum (60) is a 0, so my card is legit!

2 个答案:

答案 0 :(得分:1)

假设您要从多位数整数数组中创建一个单位整数数组

#include <stdio.h>
#define MAX 1024
void printarr(int *a, int n) { // function to print array
  for(int i = 0; i < n; i++) {
    printf("%d ", a[i]);
  }
  printf("\n");
}
int main() {
  int a[5] = {14, 2, 7, 3, 2};
  int b[MAX];
  int k = 0;
  printarr(a, 5);
  char s[MAX]; // char buffer to store char array
  for(int i = 0; i < 5; i++) {
    sprintf(s, "%d", a[i]);// convert int to char array
    int j = 0;
    while(s[j]!='\0') { // for each digit, create a new integer
      b[k++] = s[j++] - '0';
    }
  }
  printarr(b, k);
  return 0;
}


输出:

14 2 7 3 2 
1 4 2 7 3 2


否则根据具体情况

#include <stdio.h>
#define MAX 1024
void printarr(int *a, int n) { // function to print array
  for(int i = 0; i < n; i++) {
    printf("%d ", a[i]);
  }
  printf("\n");
}
int main() {
  int a[5] = {14, 2, 7, 3, 2};
  int b[6];
  printarr(a, 5);
  b[0] = 1;
  b[1] = 4;
  for(int i = 2; i < 6; i++) {
    b[i] = a[i - 1];
  }
  printarr(b, 6);
  return 0;
}


输出:

14 2 7 3 2 
1 4 2 7 3 2


答案 1 :(得分:0)

这是不使用字符串的解决方案。

首先,我们必须确定新数组将持续多长时间。为此,我们确定每个条目有多少位数。对于n> 0,数字位数为⌊log 10 (n)+1⌋。

然后,我们提取数字。可以使用整数除法123和取模/(整数除法的余数)将%分解为数字:

123 % 10 = 3    least significant digit
123 / 10 = 12
repeat
12  % 10 = 2    second least significant digit
12  / 10 = 1
repeat
1   % 10 = 1    third least significant digit
1   / 10 = 0
end

如您所见,数字是从背面提取的,因此我们也从背面填充输出数组。

#include <stdio.h>
#include <math.h>

int digitCount(int n) {
        if (n)
                return (int) log10(n) + 1;
        return 1;
}

int main() {
        int inputLength = 5;
        int input[] = {14, 1, 9, 0, 5819};
        int outputLength = 0;
        for (int i = 0; i < inputLength; ++i)
                outputLength += digitCount(input[i]);
        int output[outputLength];
        int o = outputLength;
        for (int i = inputLength - 1; i >= 0; --i) {
                int n = input[i];
                do {
                        output[--o] = n % 10;
                        n /= 10;
                } while (n);

        }
        while (o < outputLength) {
                printf("%d ", output[o++]);
        }
}

clang -lm file.c && ./a.out打印1 4 1 9 0 5 8 1 9