36 lines
857 B
C++
36 lines
857 B
C++
#include <bits/stdc++.h>
|
||
|
||
using namespace std;
|
||
typedef long long ll;
|
||
|
||
/**
|
||
* 功能:将数字转为字符串
|
||
* 作者:黄海
|
||
* 时间:2019-11-23
|
||
* @param y
|
||
* @return
|
||
*/
|
||
string convertNumToStr(int y) {
|
||
string res;
|
||
stringstream ss; // 定义流ss
|
||
ss << y; // 将数字a转化成流ss
|
||
ss >> res; // 将流ss转化成字符串
|
||
return res;
|
||
}
|
||
|
||
// 一题多解知识点:数字转字符串,字符串的遍历
|
||
int main() {
|
||
ll a, b, s = 0;
|
||
cin >> a >> b;
|
||
for (ll y = a; y <= b; y++) {
|
||
string str = convertNumToStr(y);
|
||
for (auto ch : str)//ch依次取的是str里面的字符,直到取完为止
|
||
{
|
||
if (ch == '2') {
|
||
s++;
|
||
}
|
||
}
|
||
}
|
||
cout << s << endl;
|
||
return 0;
|
||
} |