'commit'
This commit is contained in:
36
LuoGu/RuMen/P1179_2.cpp
Normal file
36
LuoGu/RuMen/P1179_2.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user