首页
论坛
课程
招聘
雪    币: 201
活跃值: 活跃值 (10)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝

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

2008-11-12 15:09 5375

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

2008-11-12 15:09
5375
输入文件: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]);
    }
}



HWS计划·2020安全精英夏令营来了!我们在华为松山湖欧洲小镇等你

最新回复 (0)
游客
登录 | 注册 方可回帖
返回