@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";
}
}
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!
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);
}
}
}
my code worked in python 2.7.13
check out this code for your reference:
x=int(input())
for i in range(x):
n=int(input())
count=0
while(n>0):
res=n%10
if(res==4):
count=count+1
n=n/10
print(count)
I don’t understand what I’ve to do under Subtasks 1 and 2. Can someone help me out? I have identified the frequency of 4s present in my numeral and that part works perfectly
its not working
bro i dont understand ur logic explain plz
I used string instead of integer in each test case
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
cin.ignore();
while(t–){
string s;
cin>>s;
int count=0;
for (int i = 0; i < s.size(); ++i)
{
if ((s[i]-‘0’)==4)
{
count=count+1;
}
}
cout<<count<<endl;
}
}
Okay so what I did is I took the input of each test case as string and ran a for loop from 0 to length of the string -1 and compared each of the index of string string with 4 whenever I found 4 I increased the count and finally printed the count
I don’t know what to do, please help me:
process.stdin.resume();
process.stdin.setEncoding(‘utf8’);
// your code goes here
process.stdin.on(‘data’, data => {
let userInput = data.toString();
let long = userInput.replace(/\s/g,".");
let arr = long.split(’.’);
const t = arr[0];
let a = 0;
let par1 = 1;
let count = 0;
for (let i = 0; i < t; i++){
a = arr[par1].split('');
for(let i = 0; i < a.length; i++){
if (a[i] === '4'){
count += 1;
}
}
console.log(count);
par1 += 1;
count = 0;
}
process.exit();
});
While submitting the solution to the problem, I am getting an error with message “Solution to this problem cannot be submitted now”. .
Can you please share a screenshot or video of the full page?
constrains
1≤ T≤ 105
- (Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points.
- (Subtask 2): 0 ≤ Numbers from the list ≤ 109- 67 points.
i don’t understand what subtask 1 and 2 mean here?
I was using C++ and in the reference code they used long long for all variables instead of int. why is it so?
without using long long , my code got submitted.