package com.itjob.algorithm;
/**
 * 第5章 数据结构与算法
 * 考题：求a+aa+aaa+aaaa+aa...a相加的值，其中a是一个数字。例如3+33+333+3333=3702(此时共有4个数相加)
 * @author AmigoXie
 */
public class SumNumbers {
	public SumNumbers(int num, int count) {
		int[] r = getNumbers(num, count);
		System.out.println(getString(r) + "=" + calResult(r));
	}

	/**
	 * 得到需要的数组
	 */
	public int[] getNumbers(int num, int count) {
		int[] numbers = new int[count];
		int temp = 0;
		for (int i = 0; i < count; i++) {
			for (int j = 0; j < count - i; j++) {
				int a = 10;
				temp += num * Math.pow(a, j);
			}
			numbers[i] = temp;
			temp = 0;
		}
		return numbers;
	}

	/**
	 * 对该数组进行计算
	 */
	public int calResult(int[] numbers) {
		int result = 0;
		for (int i = 0; i < numbers.length; i++) {
			result += numbers[i];
		}
		return result;
	}

	/**
	 * 取得该数组的字符串形式
	 */
	public String getString(int[] numbers) {
		String temp = "";
		String result;
		int len = numbers.length;
		for (int i = len - 1; i >= 0; i--) {
			temp += numbers[i] + "+";
		}
		result = temp.substring(0, temp.length() - 1);
		return result;
	}

	public static void main(String args[]) {
		new SumNumbers(3, 4);
	}
}
