Nearly Lucky Number Problem: Don't Know Why I am Failing Test Case 7

Правка en1, от sjp1114, 2022-08-13 05:21:09

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(); }

Теги math, need help

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский sjp1114 2022-08-13 05:21:09 1349 Initial revision (published)