紫书 习题3-3 数数字(字符函数,常量数组)

#include <stdio.h>
#include <string.h>

int main(void)
{

	int n ;

	//freopen("input.txt" , "r" , stdin);
	//freopen("output.txt" , "w" , stdout);

	scanf("%d" , &n);

	while(n--)
	{
		int N ;
		int i ;
		int count[10];

		memset(count , 0 , sizeof(count));
		scanf("%d" , &N);

		for( i = 1; i <= N ; i++)
		{
			int num = i ;

			while(num)
			{
				int a = num%10;
				num /=10;
				count[a]++; //类似桶排序
			}
		}

		for( i =0 ; i< 9 ; i++)
		{
			printf("%d " , count[i]);
		}
			printf("%d\n" , count[i]);//注意输出格式
	}

	return 0 ;
}

  

#include <iostream>   //二维数组打表
#include <cstring>  
#include <cstdio>  
using namespace std;  
int f[10000][10];  
  
int main()  
{  
    memset(f, 0, sizeof(f));  
    for (int i = 1 ; i < 10000 ; ++ i) {  
        for (int j = 0 ; j < 10 ; ++ j)  
            f[i][j] = f[i-1][j];  
        int left = i;  
        while (left) {  
            f[i][left%10] ++;  
            left /= 10;  
        }  
    }  
      
    int t,n;  
    while (~scanf("%d",&t))  
    while (t --) {  
        scanf("%d",&n);  
        for (int i = 0 ; i < 9 ; ++ i)  
            printf("%d ",f[n][i]);  
        printf("%d\n",f[n][9]);  
    }  
    return 0;  
}  

  

#include<cstdio>  //打表2
int c[10000][10];  
    
int main()  
{  
    int i, k, t, n;  
    for (i = 1; i < 10000; ++i)  
    {  
        for (k = i; k; k /= 10) ++c[i][k % 10];  
        for (; k < 10; ++k) c[i][k] += c[i - 1][k];  
    }  
    scanf("%d", &t);  
    while (t--)  
    {  
        scanf("%d", &n);  
        for (i = 0; i < 9; ++i)  
            printf("%d ", c[n][i]);  
        printf("%d\n", c[n][9]);  
    }  
    return 0;  
}

  

posted @ 2017-07-18 00:37  Roni_i  阅读(179)  评论(0编辑  收藏  举报