题1:数字统计
【题目描述】
请统计某个给定范围[L,R][L, R][L,R]的所有整数中,数字222 出现的次数。
比如给定范围[2,22][2, 22][2,22],数字222 在数222 中出现了111 次,在数121212 中出现111 次,在数202020 中出现111 次,在数212121 中出现111 次,在数222222 中出现222 次,所以数字222 在该范围内一共出现了666次。
【输入文件】
共111 行,为两个正整数LLL 和RRR,之间用一个空格隔开。
【输出文件】
共111 行,表示数字222 出现的次数。
【输入样例1】
2 22
【输出样例1】
6
【输出样例2】
2 100
【输出样例2】
20
【数据范围】
1≤L≤R≤100001 \le L \le R \le 100001≤L≤R≤10000。
【代码如下】:
#include<bits/stdc++.h>
using namespace std;
//ifstream cin("two.in");
//ofstream cout("two.out");
int twos(int n); //twos(n)即f(x)
int tpow[]={1,10,100,1000,10000,100000,1000000,10000000}; //打表10的幂
int twos(int n){
int count=0,i;
const int bits=7;
for (i=0;i<bits;i++){
count+=n/tpow[i+1]*tpow[i];
if (n%tpow[i+1]>=3*tpow[i])
count+=tpow[i];
else
if (n%tpow[i+1]>=2*tpow[i])
count+=(n%tpow[i+1]-2*tpow[i]+1);
}
return count;
}
int main(void){
int l,r;
cin >> l >> r;
cout << twos(r)-twos(l-1);
return 0;
}
计算指定范围内数字2出现的次数:编程挑战
2634

被折叠的 条评论
为什么被折叠?



