1. 程式人生 > >P1179 數字統計

P1179 數字統計

pan turn bsp out spa code nbsp while 文件

題目描述

請統計某個給定範圍[L, R]的所有整數中,數字 2 出現的次數。

比如給定範圍[2, 22],數字 2 在數 2 中出現了 1 次,在數 12 中出現 1 次,在數 20 中出

現 1 次,在數 21 中出現 1 次,在數 22 中出現 2 次,所以數字 2 在該範圍內一共出現了 6

次。

輸入輸出格式

輸入格式:

輸入文件名為 two.in。

輸入共 1 行,為兩個正整數 L 和 R,之間用一個空格隔開。

輸出格式:

輸出文件名為 two.out。

輸出共 1 行,表示數字 2 出現的次數。

輸入輸出樣例

輸入樣例#1:
【輸入樣例1】
2 22
【輸入樣例2】
2 100
輸出樣例#1:
【輸出樣例1】
6
【輸出樣例2】
20

說明

1 ≤ L ≤R≤ 100000。

模擬

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int L ,R,T;
int ans,yu;
int main()
{
    cin>>L>>R;
    
for(int i=L;i<=R;i++) { T=i; while(T) { yu=T%10; if(yu==2) ans++; T=(T-yu)/10; } } cout<<ans; return 0; }

P1179 數字統計