patterncsharpModerate
Calculation of the distance to the next space station
Viewed 0 times
calculationspacethestationdistancenext
Problem
I've made a solution for this problem:
Task:
Write a program for calculation of the distance to the next space station.
Algorithm:
First base is located at the distance equal to
SMK from the beginning of the path (Sirius in our case). The next
station is located at the distance of F(SMK) from the first. Third
station — at the distance of F(F(SMK)) and so on. Here \$F\$ — is the Top
Secret Mars Function (TSMF). Its value is the sum of the cubes of
digits its argument in decimal notation (for example \$F(12) = 1^3 + 2^3
> = 9\$). So if the distance from the (\$I − 1\$)-th to \$I\$-th stations is \$X\$, then the distance between \$I\$-th and (\$I + 1\$)-th stations is \$F(X)\$. Your
cruiser is located between (\$N − 1\$)-th and \$N\$-th space stations at the
distance of \$L\$ from (\$N − 1\$)-th station. Taking \$N\$, \$K\$ (Secret Mars Key)
and \$L\$ as input your program should output the distance \$M\$ between your
cruiser and \$N\$-th station. Oh, by the way! The value of SMK is always
divisible by 3.
Input:
Number \$T\$ (\$2 ≤ T ≤ 33333\$) is placed in the first line of input — it is the number of tests for your program. It followed by the next \$T\$ lines.
Each of these \$T\$ lines contains 3 integer numbers:
\$N\$ (\$2 ≤ N ≤ 33333\$), \$K\$ (\$3 ≤ K ≤ 33333\$) and \$L\$ (\$L ≥ 1\$)
Output:
\$T\$ lines. \$I\$-th line contains the calculated value of \$M\$ for \$I\$-th test case.
It has to run in less than one second and take up less than 16 megabytes of memory.
Sample:
http://img33.imageshack.us/img33/502/81219587.jpg
Here is the compiled code:
```
static void Main()
{
int T = Convert.ToInt32(Console.ReadLine());
for (int k = 0; k < T; k++)
{
string[] split = (Console.ReadLine()).Split(new Char[] { ' ' });
int N = Convert.ToInt32(split[0]);
double K = 0;
string Kst = split[1];
double POST = 0;
for
Task:
Write a program for calculation of the distance to the next space station.
Algorithm:
First base is located at the distance equal to
SMK from the beginning of the path (Sirius in our case). The next
station is located at the distance of F(SMK) from the first. Third
station — at the distance of F(F(SMK)) and so on. Here \$F\$ — is the Top
Secret Mars Function (TSMF). Its value is the sum of the cubes of
digits its argument in decimal notation (for example \$F(12) = 1^3 + 2^3
> = 9\$). So if the distance from the (\$I − 1\$)-th to \$I\$-th stations is \$X\$, then the distance between \$I\$-th and (\$I + 1\$)-th stations is \$F(X)\$. Your
cruiser is located between (\$N − 1\$)-th and \$N\$-th space stations at the
distance of \$L\$ from (\$N − 1\$)-th station. Taking \$N\$, \$K\$ (Secret Mars Key)
and \$L\$ as input your program should output the distance \$M\$ between your
cruiser and \$N\$-th station. Oh, by the way! The value of SMK is always
divisible by 3.
Input:
Number \$T\$ (\$2 ≤ T ≤ 33333\$) is placed in the first line of input — it is the number of tests for your program. It followed by the next \$T\$ lines.
Each of these \$T\$ lines contains 3 integer numbers:
\$N\$ (\$2 ≤ N ≤ 33333\$), \$K\$ (\$3 ≤ K ≤ 33333\$) and \$L\$ (\$L ≥ 1\$)
Output:
\$T\$ lines. \$I\$-th line contains the calculated value of \$M\$ for \$I\$-th test case.
It has to run in less than one second and take up less than 16 megabytes of memory.
Sample:
http://img33.imageshack.us/img33/502/81219587.jpg
Here is the compiled code:
```
static void Main()
{
int T = Convert.ToInt32(Console.ReadLine());
for (int k = 0; k < T; k++)
{
string[] split = (Console.ReadLine()).Split(new Char[] { ' ' });
int N = Convert.ToInt32(split[0]);
double K = 0;
string Kst = split[1];
double POST = 0;
for
Solution
Here are a few tips:
-
Instead of getting each character, creating a new string from it, and then parse the string into a number, just get the character and convert the character code into the number.
-
Alternatively, parse the whole number once, then get the digits numerically using modulo.
-
There are only ten digits, so you can easily set up an array of precalculated cube values.
-
Example:
-
Instead of getting each character, creating a new string from it, and then parse the string into a number, just get the character and convert the character code into the number.
-
Alternatively, parse the whole number once, then get the digits numerically using modulo.
-
There are only ten digits, so you can easily set up an array of precalculated cube values.
-
K is a double, but there are no floating point operations here. Use an int, or a long if needed.Example:
int T = Convert.ToInt32(Console.ReadLine());
int[] cube = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
for (int k = 0; k 0) {
K += cube[Kst % 10];
Kst /= 10;
}
Kst = K;
}
Console.WriteLine(K - Convert.ToInt32(split[2]));
}Code Snippets
int T = Convert.ToInt32(Console.ReadLine());
int[] cube = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 };
for (int k = 0; k < T; k++) {
string[] split = Console.ReadLine().Split(' ');
int N = Convert.ToInt32(split[0]);
int Kst = Convert.ToInt32(split[1]);
int K = 0;
for (int i = 0; i < N - 1; i++) {
K = 0;
while (Kst > 0) {
K += cube[Kst % 10];
Kst /= 10;
}
Kst = K;
}
Console.WriteLine(K - Convert.ToInt32(split[2]));
}Context
StackExchange Code Review Q#9942, answer score: 13
Revisions (0)
No revisions yet.