Easy
We want to turn the given integer into a number that has only one non-zero digit using a tail rounding approach. This means that at each step we take the last non 0 digit of the number and round it to 0 or to 10. If it's less than 5 we round it to 0 if it's larger than or equal to 5 we round it to 10 (rounding to 10 means increasing the next significant digit by 1). The process stops immediately once there is only one non-zero digit left.
Example
-
For n = 15, the output should be
rounders(n) = 20; -
For n = 1234, the output should be
rounders(n) = 1000.1234 -> 1230 -> 1200 -> 1000.
-
For n = 1445, the output should be
rounders(n) = 2000.1445 -> 1450 -> 1500 -> 2000.
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] integer n
A positive integer.
Guaranteed constraints:
1 ≤ value ≤ 10^8. -
[output] integer
- The rounded number.
[C] Syntax Tips
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
char * answer = malloc(strlen(name) + 8);
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, ");
strcat(answer, name);
return answer;
}
Solution
int rounders(int n)
{
int N=0;
while(pow(10,N)<n)
{
if((n/(int)pow(10,N))%10>=5 && n/(int)pow(10,N+1)!=0)
n+=(int)pow(10,N+1);
n-=n%(int)pow(10,N);
N++;
}
return n;
}
'Codesignal' 카테고리의 다른 글
<Codesignal> Count Black Cells (0) | 2020.04.14 |
---|---|
<Codesignal> Candles (0) | 2020.04.13 |
<Codesignal> Increase Number Roundness (0) | 2020.04.12 |
<Codesignal> Apple Boxes (0) | 2020.04.12 |
<Codesignal> Addition Without Carrying (0) | 2020.04.12 |