I am very new at c++, and like I mentioned in the title, I have no ideas how to even start the problem, hope someone can help me. thanks a lot.
Write a function that computes how many digits two positive
integers m and n have in common. For example, if m is 112358 and
n is 179881, then the result is 2 because the numbers have the digits
1 and 8 in common. It does not matter how often a digit occurs.
// return true if the number contains digit d
bool has_digit( unsignedint number, unsignedint d )
{
if( number < 10 ) return number == d ; // single digit number
while( number > 9 ) // till there is only one digit left
{
// number%10 yields the last (rightmost) digit of numbe
if( number%10 == d ) returntrue ; // found the digit
number /= 10 ; // number/=10 chops off the rightmost digit of the number
}
returnfalse ;
}
With this, you should now be able to examine the digits of a number one by one.
Use of valarray to produce a quasi-histogram. (Changing line 8 to for ( ; n; n /= 10 ) f[n%10]++; would produce an actual histogram, but preclude the use of valarray operations to do the counting via the sum on line 17.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <valarray>
usingnamespace std;
valarray<int> isPresent( int n )
{
valarray<int> f( 0, 10 );
for ( ; n; n /= 10 ) f[n%10] = 1;
return f;
}
int main()
{
int A, B;
cout << "Input A and B: ";
cin >> A >> B;
cout << "Number of digits in common = " << ( isPresent( A ) * isPresent( B ) ).sum();
}
Input A and B: 112358 179881
Number of digits in common = 2
Stringify and look for common characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std;
int common( string a, string b )
{
int counter = 0;
for ( char c : "0123456789" ) counter += ( a.find( c ) != string::npos ) * ( b.find( c ) != string::npos );
return counter;
}
int main()
{
int A, B;
cout << "Input A and B: ";
cin >> A >> B;
cout << "Number of digits in common = " << common( to_string( A ), to_string( B ) );
}
Stringify and use a set to count the unique characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
#include <set>
usingnamespace std;
int uniq( string s )
{
set<char> S( s.begin(), s.end() );
return S.size();
}
int main()
{
int A, B;
cout << "Input A and B: ";
cin >> A >> B;
string sA = to_string( A ), sB = to_string( B );
cout << "Number of digits in common = " << uniq( sA ) + uniq( sB ) - uniq( sA + sB );
}
#include <iostream>
usingnamespace std;
int isPresent( int n )
{
int bits = 0;
for ( ; n; n /= 10 ) bits = bits | 1 << (n%10);
return bits;
}
int countBits( int b )
{
int counter = 0;
for ( ; b; b = b >> 1 ) counter += b & 1;
return counter;
}
int main()
{
int A, B;
cout << "Input A and B: "; cin >> A >> B;
cout << "Number of digits in common = " << countBits( isPresent( A ) & isPresent( B ) );
}
This kind of assignment is not about being fancy. It is about being able to split a number into digits using division and remainder.
The assignment has an added trick: for each digit you find, keep track of whether or not it exists. This is a simple histogram trick.
Write yourself a function that fills a histogram with the number of times a digit appears (or even just if a digit appears, the number of times is not really important, only that it appeared).
void find_digits( int n, int digits[10] );
Now all you need to do is call the function for your two numbers, then compare the two histograms:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
int a, b;
int adigits[ 10 ] = { 0 };
int bdigits[ 10 ] = { 0 };
// (get a and b from user here)
find_digits( a, adigits );
find_digits( b, bdigits );
// now just loop through the two arrays. When you find a non-zero pair, print it.
for (int n = 0; n < 10; n++)
if (adigits[ n ] && bdigits[ n ])
...
A lot o these are pretty good. I have one with a single loop (only as long as the longest number). Might be a little heavier cause of vectors, and because I arbitrarily filled one with all -1, and one with all -2, just to differentiate for the upcoming set intersection (not as fancy as dot products, etc). Left in some debug couts so you can see how it works.
There’s no such thing as a “silly” question. You’ll keep asking them as long as you are a programmer. Just, over time, there will be fewer people able to answer your questions.