U E D R , A S I H C RSS

Reverse And Add/Celfin

~cpp
#include <iostream>
#include <cmath>
using namespace std;

int testcase, number, i, counting;
int input[10], temp[10], carry[10];
int tempNum;
bool isPanline;

void getReverseAndAdd()
{
	counting=0;
	while(!isPanline)
	{
		isPanline=true;
		for(i=0; i<10; i++)
			carry[i]=0;

		for(i=9; i>=number; i--)
		{
			if(input[i]+input[number-i+9]+carry[i]>9)
			{
				carry[i-1]=1;
				temp[i] = (input[i]+input[number-i+9]+carry[i])%10;
			}
			else
				temp[i] = input[i]+input[number-i+9]+carry[i];
		}

		if(carry[i]==1)
		{
			temp[i]=1;
			number--;
		}

		for(i=0; i<10; i++)
			input[i] = temp[i];
		counting++;

		for(i=0; i<10; i++)
			if(carry[i]==1)
				isPanline=false;
	}

	cout << counting << " ";
	for(i=number; i<10; i++)
		cout << input[i];
	cout << endl;
}

int main()
{
	cin >> testcase;
	while(testcase--)
	{
		cin >> tempNum;
		number=9;
		for(i=0; i<10; i++)
		{ 
			if(i==0)
				input[number] = tempNum%10;
			else
			{
				if((int)(tempNum/pow(10, i))!=0)
				{
					number--;
					input[number]=(int)(tempNum/pow(10, i))%10;
				}
			}
		}
		isPanline = false;
		getReverseAndAdd();
	}

	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:54
Processing time 0.0141 sec