題目鏈接
請統計某個給定范圍[L,?R]的所有整數中,數字?2?出現的次數,
比如給定范圍[2,?22],數字?2?在數 2 中出現了?1 次,在數?12 中出現?1?次,在數?20?中出現?1?次,在數?21?中出現?1?次,在數?22?中出現?2?次,所以數字?2?在該范圍內一共出現了?6 次,
輸入格式
輸入共?1?行,為兩個正整數?L?和?R,之間用一個空格隔開,
輸出格式
輸出共?1?行,表示數字?2?出現的次數,
資料范圍
1≤L≤R≤10000
輸入樣例:
2 22
輸出樣例:
6
思路:
直接列舉 1 到 n 中的每個數,再依次判斷每一位是否等于 2 即可,
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 4e5 + 10;
const int M = 35;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;
void solve(){
int l,r;
cin>>l>>r;
int ans=0;
rep(i,l,r){
for(int j=i;j;j/=10){
if(j%10==2) ans++;
}
}
cout<<ans<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/260617.html
標籤:其他
上一篇:kotlin撰寫安卓APP
