zoukankan      html  css  js  c++  java
  • UVA

    //法一:
    //收获:这题对于各位的统计,也没什么特别的技巧,不过是取余;但是,if-else从最高数位开始找,可以有效地避免取余得到的前置0被多余统计;而事实上,其实也根本不会出现多余统计的情况,因为这个选择语句,已经确定了到底有几位,无用的根本没分离,分离出的都是一定要统计的
    
    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {
    	int a[10], t, n;
    	cin >> t;
    	while (t--)
    	{
    		cin >> n;
    		memset(a, 0, sizeof(a));
    		for (int i = 1; i <= n; i++)
    		{
    			if (i == 10000) 
    			{
    				a[1]++;
    				a[0] += 4;	
    			}
    			else if (i / 1000) 
    			{
    				a[i / 1000]++;
    				a[i / 100 % 10]++;
    				a[i / 10 % 10]++;
    				a[i % 10]++;
    			}
    			else if (i / 100)
    			{
    				a[i / 100]++;
    				a[i / 10 % 10]++;
    				a[i % 10]++;
    			}
    			else if (i / 10)
    			{
    				a[i / 10]++;
    				a[i % 10]++;
    			}
    			else a[i]++;
    		}
    		
    		for (int i = 0; i < 10; i++)
    		{
    			if (i) cout << " ";
    			cout << a[i];
    		}
    		cout << endl;
    		
    	} 
    	return 0;
    }



    //法2:其实无非就是用sprintf再写了一次
    //不得不说,有时候学到一个新函数的用法,真的是能让代码变简洁好多,一个个写选择真的很繁琐...
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    int main()
    {
    	int a[10], t, n, len;
    	char s[10];
    	cin >> t;
    	while (t--)
    	{
    		cin >> n;
    		memset(a, 0, sizeof(a));
    		for (int i = 1; i <= n; i++)
    		{
    			sprintf(s, "%d", i);
    			len = strlen(s);
    			for (int j = 0; j < len; j++)
    				a[s[j] - '0']++;
    		}
    		
    		for (int i = 0; i < 10; i++)
    		{
    			if (i) cout << " ";
    			cout << a[i];
    		}
    		cout << endl;
    	}
    	return 0;
    }


  • 相关阅读:
    DNF(一.YUM已死,DNF代之)
    cmd中运行python文件,并带参数
    python中dict[key] 存在key,但是报错:KeyError
    回测指标计算
    python进行excel操作
    myquant平台搭建及使用
    远程连接mongodb时,27017端口连接不上的解决办法
    python---urllib模块
    python---socket模块
    状态模式
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789489.html
Copyright © 2011-2022 走看看