Evaluate the value of s = a + aa + aaa + aaaa + aa... a, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (at this time, there are 5 numbers added (a can also be 10, 99, 919, etc)
The following are Java classes:
Public class CalculateA {
// Required length of the calculated expression
Private int num;
// The value of
Private int;
Public CalculateA (int num, int ){
// TODO Auto-generated constructor stub
This. num = num;
This. a =;
}
Public long getCalculatedResult (){
Long result = 0L;
Int length = String. valueOf (a). length ();
Double repeate = Math. pow (10, length );
Int I = 0;
Int k = 1;
While (I <= num-1 ){
Result + = (num-I) * a * k;
I ++;
K * = repeate;
}
Return result;
}
}
Test class:
Import junit. framework. Assert;
Import org. junit. Test;
Public class CalculateATest {
@ Test
Public void testGetCalculatedResult (){
// When a is 0
CalculateA calculate = new CalculateA (5, 0 );
Assert. assertEquals (calculate. getCalculatedResult (), 0 );
// 0 <a <= 9
Calculate = new CalculateA (2, 1 );
Assert. assertEquals (calculate. getCalculatedResult (), 12 );
// A> 9
Calculate = new CalculateA (3, 10 );
Assert. assertEquals (calculate. getCalculatedResult (), 102030 );
}
}
This article is from the "yi" blog