将数字乘以C中的数组 [英] Multiply of digits into an array in C

查看:93
本文介绍了将数字乘以C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求在C中创建一个程序,其中将采用2d数组。 3个数字,每次都会找到一个数字的持久性乘以它直到我们只有一个数字。然后每个数字的结果将被归档到数组的第二行。



例如。



例如,数字2736的持久性为0:首先我们发现2 * 7 * 1 * 6 = 252,然后2 * 5 * 2 = 20,最后2 * 0 = 0,我们得到一位数。 (在那个例子中是0)。



现在我们可以说我们在数组的每一行都有这些数字:

I have been asked to create a program in C in which will take an 2d array of eg. 3 numbers and will find the persistance of one number every time by multiplying it until we gat only one number. Then the result of each number will be filed in the second row of the array.

Eg.

For example, the persistence of the number 2736 is 0: first we find that 2 * 7 * 1 * 6 = 252, then that 2 * 5 * 2 = 20 and finally 2 * 0 = 0 where we come to a single digit. (which in that example is 0).

Now lets say we have that numbers in every row of the array :

2716 , 2717, 2718, 2719, 2720 




array[5][2] = { {2716,0}, {2717,0}, {2718,0}, {2719,0}, {2720,0} };





每个持久性的图形表示将是





The graphical presentaion of every persistance would be

Number | Persist
-----------------
  2716  |    3
  2717  |    4
  2718  |    2
  2719  |    3
  2720  |    1





和数组之后将是:



and the array after wil be :

array[5][2] = { {2716,3}, {2717,4}, {2718,2}, {2719,3}, {2720,1} };







我试图创建一个代码,但我有两件事。

首先当我进行第一次乘法时我需要重做它导致数字不能是一位数(2720除外,结果是第一次为0)



和秒我无法将每个数字的持久结果传递给数组。



这是我的代码到目前为止:






I tried to create a code but i get stacked in two things.
First when I make the first multiplyzation I need to redo it cause the number cant be one digit (except in 2720 which the result is 0 from the first time)

and second I can't pass the persist result of each number into the array.

Here's my code so far:

#include <stdio.h> 
#include <conio.h> 

int main()
{
	// set 0 to every second row cause we don't know the persist of the 1 row number yet
	int array[5][2] = { {2716,0}, {2717,0}, {2718,0}, {2719,0}, {2720,0} };
	int a, b = 1;
	int number;
	
	for(int i =0; i < 5; i++)
	{
		number = array[i][0];
		
		for(;number != 0; number = number / 10)
		{
			a = number % 10;
			b = b * a ;
			array[i][1] = b;  //set array[i][1] cause we want to fill the 2nd column of the array
		}
		
	}
	
	
	for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%d ", array[i][j]);
        }
        printf("\n");
    }
   
}

推荐答案

我会考虑乘数运算,即:

I would factor out the multiply-the-digits operation, namely:
#include <stdio.h>


int multiply_digits(int x)
{
  int z = 1;
  while ( x )
  {
    z *= x % 10;
    x /= 10;
  }

  return z;
}


int main()
{
  // set 0 to every second row cause we don't know the persist of the 1 row number yet
  int array[5][2] = { {2716,0}, {2717,0}, {2718,0}, {2719,0}, {2720,0} };

  int i, j;
  for (i=0; i<5; ++i)
  {
    int n = array[i][0];
    int j = 0;
    while (n>9)
    {
      n = multiply_digits(n);
      j++;
    }
    array[i][1] = j;
  }

  for (i = 0; i < 5; i++)
  {
    for (j = 0; j < 2; j++)
    {
       printf("%d ", array[i][j]);
    }
    printf("\n");
  }

  return 0;
}


这篇关于将数字乘以C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆