PAT:1006(换个格式输出整数想·)

      

1006 换个格式输出整数 (15分)

让我们用字母 B 来表示“百”、字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n<10),换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234,因为它有 2 个“百”、3 个“十”、以及个位的 4。

输入格式:

每个测试输入包含 1 个测试用例,给出正整数 n(<1000)。

输出格式:

每个测试用例的输出占一行,用规定的格式输出 n。

            

//输入有三种情况 输入:三位数、两位数、一位数
//百位数和十位数都是按照数字多少就输出多少个‘B' 或'S',个位上的是显示个位数字包括自己之前的所有数,顺序输出
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string number = {0};
	cin >> number;
	int length = 0;
	length = number.length();
	int size = 0;
	string database[100] = { "0" };
	for (int i = 0; i < length; i++)
	{
		int count = 0;
		//输入为三位数的情况
		if (length == 3)
		{
			//判断百位
			if (i == 0)
			{
				count = number[i] - '0';
				while (count--)
				{
					database[size++] = 'B';
				}
			}
			//判断十位
			else if (i == 1)
			{
				count = number[i] - '0';
				while (count--)
				{
					database[size++] = 'S';
				}

			}

			//判断个位
			else
			{
				count = number[i] - '0';
				int step = 1;
				while (count--)
				{
					database[size++] = step+'0';
					step++;
				}
			}
		}
		//输入为两位数的情况
		else if (length == 2)
		{
			//判断十位
			if (i == 0)
			{
				count = number[i] - '0';
				while (count--)
				{
					database[size++] = 'S';
				}
			}
			//判断个位
			else
			{
				count = number[i] - '0';
				int step = 1;
				while (count--)
				{
					database[size++] = step + '0';
					step++;
				}
			}
		}
		//输入一位数的情况
		else
		{
			count = number[i] - '0';
			int step = 1;
			while (count--)
			{
				database[size++] = step + '0';
				step++;
			}

		}
	}
	//输出
	for (int j = 0; j < size; j++)
	{
		cout << database[j];
	}
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/zongji/p/12240986.html
今日推荐