从1到N中1的个数

示例1,2...9,10,11中有四个1

int getNumber(int n)
{
	int count = 0;
	int factor = 1;
	int low = 0;
	int cur = 0;
	int high = 0;
	while (n /factor != 0)
	{
		low = n - (n / factor) * factor;
		cur = (n / factor) % 10;
		high =  n / (factor * 10);
		switch(cur)
		{
		case 0:
			count += high * factor;
			break;
		case 1:
			count += high *factor + low + 1;
			break;
		default:
			count += (high+1)* factor;
				break;

		}
		factor = factor * 10;
	}
	return count;
}
原文地址:https://www.cnblogs.com/liuweilinlin/p/3300107.html