题目:自除数
思路:利用除以10的商和余数,依次获取每一位上的数字,然后做出相应的判断即可
【需要注意的是求商和取余的顺序问题】
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> res;
int yu;
for(int i = left; i <= right; i++){
int flag = 1;
int j = i;
while(j){
yu = j % 10;
j = j / 10;
if(yu == 0) flag = 0;
else if(i % yu != 0) {
flag = 0;
}
}
if(flag) res.push_back(i);
}
return res;
}
};
int main() {
Solution s;
vector<int> res = s.selfDividingNumbers(1,22);
for(int i : res)
cout << i << " ";
cout << endl;
system("pause");
return 0;
}