TopCoder算法竞赛题4:SRM 148 DIV 2, 250-point

来源:互联网 发布:天下3张凯枫捏脸数据 编辑:程序博客网 时间:2024/05/29 11:21

Problem Statement

Create a class DivisorDigits containing a method howMany which takes an int number and returns how many digits in number divide evenly into number itself.

 

Definition

Class: DivisorDigits

Method: howMany

Parameters: int

Returns: int

Method signature: int howMany(int number)

(be sure your method is public)

 

Notes

No number is divisible by 0.

 

Constraints

number will be between 10000 and 999999999.

 

Examples

0)

12345

Returns: 3

12345 is divisible by 1, 3, and 5.

 

1)

661232

Returns: 2

661232 is divisible by 1 and 2 (doubled).

 

2)

52527

Returns: 0

52527 is not divisible by 5, 2, or 7.

 

3)

730000000

Returns: 0

Nothing is divisible by 0. In this case, the number is also not divisible by 7 or 3.

 

Sourece code

#include<iostream>

using namespace std;

 

class DivisorDigits

{

public:

      int howMany(int number)

      {

           int count = 0;

           int x = number;

           while(x)

           {

                 int y = x%10;

                 x/=10;

                 if(0 == y) continue;

                 if(0 == number%y) count++;

           }

           return count;

      }

};

 

int main(void)

{

      DivisorDigits dd;

//   int cnt = dd.howMany(12345);

//   int cnt = dd.howMany(730000000);

      int cnt = dd.howMany(661232);

      cout<<cnt<<endl;

      return 0;

}