19+199+1999+……+1999…9(1999个9)和是多少?

#include <iostream>
#include <cstdlib>
using namespace std;
int num[1000001];
//19+199+1999+……+1999…9(1999个9)和是多少?
int main()
{
	int n, t = 2, temp = 0, jin = 0, j = 0, hou = 0;
	cin >> n; 
	for (int i = n; i >= 1; --i)	
	{
		temp = t + jin;
		num[j++] = temp % 10;
		jin = temp / 10;
	}
	if (jin > 0)
	{
		num[j] = jin;
	}
	else
	{
		--j;
	}
	if (n >= 5)
	{
		for (int i = j; i >= 5; --i)//减2减n 
		{
			printf("%d", num[i]);//从头到尾的程序是多余的,直接输出n个2再相减即可 
		}
	}
	for (int i = 4; i >= 0; --i)
	{
		hou = hou + num[i];
		hou *= 10;//最后一个2变成20,如果输入1,则hou变成20 
	}
	hou = hou - n;//每次多加了一个1,最后减出来 
	cout << hou;	
	return 0;
}

========================================Talk is cheap, show me the code=======================================
posted @ 2018-02-27 16:00  绿叶萌飞  阅读(421)  评论(0编辑  收藏  举报