I have seen many questions for this in C++ section. And the most of them suggested using uint64_t, unsigned long long
and i have tried
- long long
- unsigned long long
- uint64_t
I am giving input of 10^18 as its max input. But the number being processed is returned as some garbage values.
I am giving it input as 111111111111111110
,
And at the line when I divide it by 10^(step-1)
and multiply it by 10^(step-1)
The value seems to be oscillating at
Right Now In 111111111111111103
Right Now In 111111111111111104 //But it should be 111111111111111100 and then -- to decreas one more
This approach works well with lesser values. But not working with greater inputs
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<math.h>
#include<stdint.h>
#include<inttypes.h>
int step = 0;
bool isTidy(uint64_t n){
step++;
if(n < 10) return true;
int lastNumber = n % 10;
int secondLastNumber = ((n - lastNumber)/10) % 10;
return (lastNumber >= secondLastNumber) && isTidy((n - lastNumber)/10);
}
int main(){
char* inputString;
uint64_t testNumber;
int numberOfTestcases;
int iterator = 1;
scanf("%d", &numberOfTestcases);
while(numberOfTestcases --> 0){
scanf("%"SCNu64,&testNumber);
do{
step = 0;
if(isTidy(testNumber)){
printf("Case #%d: %"PRIu64"\n",iterator++, testNumber);
break;
}
printf("Right Now In %"PRIu64"\n",testNumber);
testNumber /= pow(10,step - 1);
testNumber *= pow(10,step - 1);
printf("Right Now In %"PRIu64"\n",testNumber);
}while(testNumber --> 0);
}
return 0;
}
edits: Tried to clear up the question. And fixed the scan input problem