sjp1114's blog

By sjp1114, history, 7 weeks ago, In English

include <bits/stdc++.h>

using namespace std;

void solve(){ int number; cin >> number; string n = to_string(number);

//run a for loop and count number of lucky digits: either 4 or 7!
int lucky_num = 0;
for(int i = 0; i < n.size() and n[i] != '\0'; i++){
    if(n[i] == '4' or n[i] == '7'){
        lucky_num++;
    }
}
string lucky_num_s = to_string(lucky_num);

if(lucky_num_s.size() == 1){
    if(lucky_num_s[0] == '4'){
        cout << "YES";
        return;
    }
    else if(lucky_num_s[0] == '7'){
        cout << "YES";
        return;
    }
    else{
        cout << "NO";
        return;
    }
}
//boolean flag to check if current num is nearly lucky!
bool is_lucky = true;
for(int j = 0; j < lucky_num_s.size() and lucky_num_s[j] != '\0'; j++){
    if(lucky_num_s[j] == '4'){
        continue;
    }
    else if(lucky_num_s[j] == '7'){
        continue;
    }
    else{
        is_lucky = false;
        break;
    }
}
if(is_lucky){
    cout << "YES";
    return;
}
else{
    cout << "NO";
}

} int main(){ solve(); }

 
 
 
 
  • Vote: I like it
  • -8
  • Vote: I do not like it

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

n can be up to 10^18, which causes overflow with an int. Use a long long when initializing number.