Create a class DivDigits containing a method howMany which takes as an argument an int number and returns how many digits in number that number itself is divisible by. Count all occurences of such digits in the number, not just the first. See examples for more information.
class DivisorDigits {
public:
int howMany(int number) {
ostringstream ss;
string s;
int ans=0;
ss << number;
s = ss.str();
repsz(i,s){
if(!(s[i]-'0')){continue;}
if(number%(s[i]-'0')==0)ans++;
}
return ans;
}
};
No comments:
Post a Comment