258. Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:   Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.


Follow up:
Could you do it without any loop/recursion in O(1) runtime?

一:算法实现:

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

int addDigits(int num)
{
	int sum = 0;

	if (num < 10)
		return num;
	
	while (num >= 1)
	{
		sum += num % 10;
		num /= 10;
	}

	addDigits(sum);

}

int main()
{
	int num;
	printf("请输入一个数字:");
	scanf_s("%d", &num);

	printf("num = %d\tresult = %d\n", num, addDigits(num));

	system("pause");
}

运行结果:

请输入一个数字:38
num = 38        result = 2
请按任意键继续. . .

这种方式可以解决该问题,但严重影响到了效率!不但用到了循环,而且还用到了递归算法。那么,应该怎样解决该问题呢?


二 算法优化:


题目中提醒我们可以不用循环或者递归实现该算法吗,既然这么说了,那么一定有能够解决该算法的方法,但是一眼望去很是迷茫,不如输入一百个数字,统计一下规律:

num = 1 result = 1
num = 2 result = 2
num = 3 result = 3
num = 4 result = 4
num = 5 result = 5
num = 6 result = 6
num = 7 result = 7
num = 8 result = 8
num = 9 result = 9
num = 10        result = 1
num = 11        result = 2
num = 12        result = 3
num = 13        result = 4
num = 14        result = 5
num = 15        result = 6
num = 16        result = 7
num = 17        result = 8
num = 18        result = 9
num = 19        result = 1
num = 20        result = 2
num = 21        result = 3
num = 22        result = 4
num = 23        result = 5
num = 24        result = 6
num = 25        result = 7
num = 26        result = 8
num = 27        result = 9
num = 28        result = 1
num = 29        result = 2
num = 30        result = 3
num = 31        result = 4
num = 32        result = 5
num = 33        result = 6
num = 34        result = 7
num = 35        result = 8
num = 36        result = 9
num = 37        result = 1
num = 38        result = 2
num = 39        result = 3
num = 40        result = 4
num = 41        result = 5
num = 42        result = 6
num = 43        result = 7
num = 44        result = 8
num = 45        result = 9
num = 46        result = 1
num = 47        result = 2
num = 48        result = 3
num = 49        result = 4
num = 50        result = 5
num = 51        result = 6
num = 52        result = 7
num = 53        result = 8
num = 54        result = 9
num = 55        result = 1
num = 56        result = 2
num = 57        result = 3
num = 58        result = 4
num = 59        result = 5
num = 60        result = 6
num = 61        result = 7
num = 62        result = 8
num = 63        result = 9
num = 64        result = 1
num = 65        result = 2
num = 66        result = 3
num = 67        result = 4
num = 68        result = 5
num = 69        result = 6
num = 70        result = 7
num = 71        result = 8
num = 72        result = 9
num = 73        result = 1
num = 74        result = 2
num = 75        result = 3
num = 76        result = 4
num = 77        result = 5
num = 78        result = 6
num = 79        result = 7
num = 80        result = 8
num = 81        result = 9
num = 82        result = 1
num = 83        result = 2
num = 84        result = 3
num = 85        result = 4
num = 86        result = 5
num = 87        result = 6
num = 88        result = 7
num = 89        result = 8
num = 90        result = 9
num = 91        result = 1
num = 92        result = 2
num = 93        result = 3
num = 94        result = 4
num = 95        result = 5
num = 96        result = 6
num = 97        result = 7
num = 98        result = 8
num = 99        result = 9
请按任意键继续. . .

看到规律了吗 result = 1+(num -1)%9;


改进的算法:

int addDigits(int num)
{
	return 1 + (num - 1) % 9;

}


你可能感兴趣的:(258. Add Digits)