zoukankan      html  css  js  c++  java
  • 洛谷P1980 计数问题

    题目描述

    试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1

    到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。

    输入输出格式

    输入格式:

    输入文件名为 count.in。

    输入共 1 行,包含 2 个整数 n、x,之间用一个空格隔开。

    输出格式:

    输出文件名为 count.out。

    输出共 1 行,包含一个整数,表示 x 出现的次数。

    输入输出样例

    输入样例#1:

    11 1


    输出样例#1:

    4


    说明

    对于 100%的数据,1≤ n ≤ 1,000,000,0 ≤ x ≤ 9。


    WriteUp:


    参考AC代码:

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    
    int main(void)
    {
    	int n,x;
    	int temp;
    	int count = 0;
    	scanf("%d%d",&n,&x);
    	for (int i=1;i<=n;i++)
    	{
    		temp = i;
    		while (temp)
    		{
    			if (temp%10 == x)
    			{
    				count++;
    			}
    			temp /= 10;
    		}
    	}
    	printf("%d",count);
    	return 0;
    }


  • 相关阅读:
    JavaScript中的数组
    JavaScript中的对象
    Highcharts中设置x轴为时间的写法
    CSS 选择器(基础)
    DOM节点
    平衡木蜻蜓
    python2.7 qt4
    C语言优先级
    树莓派与stm32通信
    linux下USB转串口配置
  • 原文地址:https://www.cnblogs.com/csnd/p/12897081.html
Copyright © 2011-2022 走看看