#include <iostream>
using namespace std;
long long int b[3],k=-1;
void revers(long long int a){
if(a==0){
return;
}
revers(a/1000);
k++;
b[k]=a%1000;
}
int main() {
long long int A;
cin >> A;
revers(A);
for(int i=0;i<3;i++){
cout<<b[i];
if(i!=2){
cout<<",";
}
}
}
@hjr265 , Actually when I looked at the earliest solution, I found that it has a bug. If you input 0, it’ll output " 0, "
I don’t know how but this logic had been passed.
# include <stdio.h>
char n[10] ;
int len (char ara [])
{
int i;
for (i = 1; ara[i] != '\0'; i ++);
return i;
}
int main ()
{
scanf ("%s", &n);
int i = len (n);
int j = i%3;
int k;
for (k = 0; k < i; k ++){
printf ("%c", n [k]);
if (k == (j-1)){
if (j == i){
return 0;
}
printf (",");
j++;
j++;
j++;
}
}
return 0;
}
Is there any way of getting the inputs of the test cases? My code is getting wrong answer in test case 6. it would have been beneficial for me if I get the input.
my written code is (written using python)
x = int(input())
length = len(str(x))
if length % 3 == 0:
loop = length / 3
else:
loop = (length / 3) + 1
l = []
for i in range(int(loop)):
q = x / 1000
r = x % 1000
x = int(q)
l.append(r)
l.reverse()
str_l = [str(a) for a in l]
for i in range(len(str_l)):
if str_l[i] == '0':
str_l[i] = '000'
print("," . join(str_l))
I’m continuously getting wrong answer in test case 3. I updated my code in different styles for a number of times but didn’t understand what’s wrong.
Please someone help me finding the problem.
My code is here:
A = int(input())
def formatter(num, Num): #num = original number, Num = listed Number
if len(Num)<3:
print(num)
else:
for j in range(len(Num)%3):
print(Num.pop(0), end = "")
for m in range(len(Num)):
if m%3 == 0:
print(",%s" %Num[m], end = "")
else:
print(Num[m], end = "")
formatter(A, list(str(A)))
#include <iostream>
#include <cstdio>
void recur(char c,int k,int *num)
{
if(c!='\n')
{
*num=k;
recur(getchar(),++k,*(&num));
if(*num==k-1)
{
int a;//Counting the number of commas required
a=(int)((*num+1)/3);
if((*num+1)%3==0)//If 6 digits than number of commas will be only 1 but if 7 then it would be 2
a--;
for(int i=(*num-k+1);i<(*num)+a;i++)//The loop can also work from 0 to k-1+a
std::cout<<"_";//_ is a temp value and can be replaced and has no particular significance
}
else
std::cout<<"\b\b";//Puts the cursor on the '_' before the current cursor position
std::cout<<c;//Prints the number
if((*num-k-1)%3==0&&(k-1)!=0)
std::cout<<"\b\b,";
}
else
return;
}
int main()
{
int num=0;
recur(getchar(),0,&num);
return 0;
}
This code is giving the desired output what they actually wanted if I run in other IDE.But they are not taking it when I am submitting.What’s wrong in this code?
@Sabiha.101735 You should test your code for various inputs before submitting. For example here I tried to test your code against some made up samples:
It is apparent that your code only works when the there is 7 digits on the input. But it is said that input can be anywhere between 0 to 200,000,000 [inclusive].