LUCKFOUR - editorial

Even if the input is given as an integer (for example in TopCoder SRMs, you have to implement a function instead of reading inputs from STDIO), you could still use sprintf(C++) or str()(Python) or whatever methods your familiar language has and turn the number to string. This will reduce the possibility of making errors.

yes . Actaully just check s[i]==‘4’ .

1 Like

I used numeric input rather than string and got AC

for _ in xrange(input()):
print raw_input().count(‘4’)

Gotta love python. :slight_smile:

7 Likes

#include<stdio.h>
#include<conio.h>
struct count
{
char a[50];
}e[5];
void main()
{
char i,j=0,c=0;

printf(“enter the strings”);
for(i=0;i<5;i++)
{
gets(e[i].a);
}

for(i=0;i<5;i++)
{
for(j=0;j<50;j++)
{
if(e[i].a[j]==‘4’)
c=c+1;
}
}
printf(“number of 4s is %d”,c);

getch();
}

Another way

the difference between the length of original string and string.replace(“4”,"") will give you the answer

public class NumberOfFours {

private static StringBuilder sb= new StringBuilder();

public static void main(String[] args) {
	
	Scanner input= new Scanner(System.in);
	int numberOfTestCases = input.nextInt();
	
	while(numberOfTestCases-- >0){
		
		String inputString = input.next();
		Solve(inputString);
		
	}
	System.out.println(sb);
}

private static void Solve(String inputString) {
	
	String s = inputString.replaceAll("4", "");

	int diff = inputString.length()-s.length();
	sb.append(Integer.toString(diff)).append("\n");
}

}

2 Likes

For reference … I have tried this and it worked for me

import java.lang.;
import java.util.
;

class four
{
public static void main(String[] args)
{
Scanner obj= new Scanner(System.in);
int x,z;
x= obj.nextInt();
if ((x>0)&&(x<=100000))
{
int[] y;
y = new int[x];
for(int i=0; i<x; i++)
{
z = obj.nextInt();

			int count = 0,a=1,b;
			
			while(a!=0)
			{
				b = z%10;
				if (b==4)
				{
					count++;
				}
				a = z/10;
				z = a;
			}
			y[i] = count;
		}
		for(int j = 0 ; j <x ; j++)
		{
			System.out.println(y[j]);
		}
	}
}

}

Is the second method is more efficient than the first method?

1 Like

Hi,
I am not able to get following line :
(Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points.
(Subtask 2): 0 ≤ Numbers from the list ≤ 109 - 67 points.

can someone elaborate it?

Thanks

1 Like

i actually managed to receive 33 points so i guess i got Subtask 1, but i am not able to fulfill Subtask 2 although its the same as Sub1 but with different values. Can someone help me ? I am quite new to programming at all so my solution is not that good i guess :smiley:

Thats what i got so far : (comments are in german, sorry)

https://www.codechef.com/viewsolution/12076756

Hello Guys…!!

I have this C Code which I wrote for this problem. It runs fine and gives me the right answers in my Dev-C++ Environment. But the CodeChef Online Judge says Wrong answer and doesn’t award me points. Can anyone please tell me what is the problem here…??

PS: Code Below


#include <stdio.h>

int main()
{
int T , rem , c , e ;

scanf("%d" , &T) ;

for ( int i = 0 ; i < T ; ++i )
{
	scanf("%d" , &e) ;
	
	while (e != 0)
	{
		rem = e % 10 ;
		
		if (rem == 4)
		{
			++c ;
		}
		
		e = e / 10 ;
	}
	printf("%d\n" , c) ;
	c = 0 ;
}
}

@sagar19122 I think you should define your c=0, because it will give wrong output when number is 0.

How about this:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 0;
    string number = "";
    cin >> t;
    for (int i = 0; i < t; i++)
    {
        cin >> number;
        cout << count(number.begin(), number.end(), '4') << "\n";
    }
}
2 Likes

always TLE

my code is here

==============================
#!/usr/bin/env perl6
use v6;
get();
for lines() -> $input {
my $ans = 0;
my $result = $input;
while ($result > 0) {
my $remainer = $result % 10;
$result = ($result / 10).floor;
if $remainer == 4 { $ans++; }
}
$ans.say;
}

That’s a fun solution!

I hope this will Help You alot :
Just Go Through this

And Subscribe my channel Hello World Please.

While submitting the solution to the problem, I am getting an error with message “Solution to this problem cannot be submitted now”. Can someone explain that why it is happening so? I am new to the platform.

t = int(input())
for i in range(t):
num = input()
four = []
for k in num:
if k== “4”:
four.append(k)
print(len(four))

im noob

did it using recursion. i hope it helps you understand the code better in java.
suggestions are welcomed.

import java.io.;
import java.util.
;
import java.text.;
import java.math.
;
import java.util.regex.*;

class Solution {

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    // StringTokenizer st = new StringTokenizer(br.readLine());
    int t = Integer.parseInt(br.readLine());
    while(t--!=0) {
        long a = Integer.parseInt(br.readLine());
        System.out.println(luckyFour(a));
    }
}

static int luckyFour(long a) {
    if(a<10) {
        if(a==4) {
            return 1;
        }
        return 0;
    }
    else if(a%10==4) {
        return 1 + luckyFour(a/10);
    }
    else {
        return 0 + luckyFour(a/10);
    }
}

}

import java.util.;
import java.lang.
;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
        Scanner sc = new Scanner(System.in);
        int t = sc.hasNextInt()?sc.nextInt():0;
        for(int i=0;i<t;i++){
            String s = sc.hasNext()?sc.next():"";
            int count= 0;
            for(int j = 0;j<s.length();j++){
                count= s.charAt(j)=='4'?count+1:count;
            }
            System.out.println(count);
    	}
	}
}