0

Five-digit number is given. Determine whether the number contains at least two identical digits

This is my code, written in C++ :

#include <iostream>
using namespace std;

int main()
{
    int n,a,b,c,d,f;
    cin >> n;
    a=n/10000;
    b=n/1000%10;
    c=n/100%10;
    d=n/10%10;
    f =n%10;
    if(a==b && c==d && b!=c ||
       a==c && b==d && c!=b ||
       a==d && b==c && d!=b ||
       a==f && f==b && f!=d )
        cout << "YES";
    else
        cout << "NO";
    return 0;
}

This doesn’t work… Can someone please help?

Emily
  • 11

3 Answers3

3

This should work

#include <iostream>
using namespace std;

int main(){
    int n, a, b, c, d, f;
    cin >> n;

    a = n / 10000;
    b = n / 1000 % 10;
    c = n / 100 % 10;
    d = n / 10 % 10;
    f = n % 10;

    if( a==b || b==c || c==d ||
        a==c || b==d || c==f ||
        a==d || b==f || d==f ||
        a==f )
        cout << "YES";
    else
        cout << "NO";
    
    return 0;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

Read the input as a string and then scan for duplicate integers with a table.

std::string n;
int digits[10] = {0}; // the number of occurrences of all digits from 0..9
bool dupes = false;

cin >> n;

for (char c : n) {
   if ((c >= '0') && (c <= '9')) {
       int index = c - '0';
       digits[index]++;
       if (digits[index] > 1) {
           dupes = true;
       }
   }
}

if (dupes) {
   cout << "YES";
}
else {
   cout << "NO";
}
selbie
  • 100,020
  • 15
  • 103
  • 173
1

This code will work for Integer input

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  vector<int>vec;
  set<int>s;
  cin>>n;
  while(n!=0)
  {
    int re=n%10;
    n=n/10;
    vec.push_back(re);
    s.insert(re);
  }
  if ((vec.size()-s.size())>=1)
    cout<<"YES"<<endl;
  else
    cout<<"NO"<<endl;
  return 0;


}