首页
社区
课程
招聘
[原创][代码之美]试题1
2008-11-12 15:09 7468

[原创][代码之美]试题1

2008-11-12 15:09
7468
输入文件:in.txt
输出文件:out.txt
编译环境:ddk 2003

#include <stdio.h>
#include <stdlib.h>

//
// The max number length
//
#define NUM_MAX_LEN 12

//
// Hold the result.
//
int result_array[10] = {0};

void count(unsigned int i)
{
    unsigned int j;
    unsigned int tmp;

    memset(result_array, 0, sizeof(result_array));

    if (i > 100000000)
    {
        return;
    }

    result_array[0] = 1;

    for (j=1; j<=i; j++)
    {
        for (tmp=j; tmp>0;)
        {
            if (tmp != 0)
            {
                result_array[tmp%10]++;

                tmp /= 10;
            }
            else
            {
                continue;
            }
        }
    }
}

int __cdecl main(void)
{
    unsigned int line_num = 0;
    int i;
    FILE *input_file;
    FILE *output_file;
    char read_buffer[NUM_MAX_LEN];

    input_file = fopen("in.txt", "r");

    output_file = fopen("out.txt", "w+");

    fgets(read_buffer, NUM_MAX_LEN, input_file);

    line_num = atoi(read_buffer);

    while (fgets(read_buffer, NUM_MAX_LEN, input_file))
    {
        count(atoi(read_buffer));

        for (i=0; i<9; i++)
        {
            fprintf(output_file, "%d, ", result_array[i]);
        }

        fprintf(output_file, "%d\n", result_array[9]);
    }
}


[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

收藏
点赞0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回