算法描述:
输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数
算法实现:
/*************************************************************************
> File Name: main.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年04月22日 星期五 09时50分17秒
************************************************************************/
#include "NumberOf1Between1AndN.h"
void Test(char *name, int n, unsigned int expected)
{
if (name != NULL)
{
if (NumberOf1Between1AndN_solution1(n) == expected)
printf("Test %s pass in solution 1!\n", name);
else
printf("Test %s fail in solution 1!\n", name);
if (NumberOf1Between1AndN_solution2(n) == expected)
printf("Test %s pass in solution 2!\n", name);
else
printf("Test %s fail in solution 2!\n", name);
}
}
void Test1()
{
Test("Test1", 10, 2);
}
int main()
{
Test1();
return 0;
}
/*************************************************************************
> File Name: NumberOf1Between1AndN.h
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年04月22日 星期五 09时43分21秒
************************************************************************/
#ifndef _NUMBEROF1BETWEEN1ANDN_H
#define _NUMBEROF1BETWEEN1ANDN_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int NumberOf1Between1AndN_solution1(unsigned int n);
int NumberOf1_solution1(unsigned int n);
int NumberOf1Between1AndN_solution2(int n);
int NumberOf1_solution2(const char *StrN);
#endif
CC = gcc
CFLAGS = -g -O2 -Wall
%.o:%.c
$(CC) -o $@ -c $(CFAGS) $<
main:main.o NumberOf1Between1AndN.o
$(CC) main.o NumberOf1Between1AndN.o -o main $(CFLAGS)
clean:
rm -rf *.o main
/*************************************************************************
> File Name: NumberOf1Between1AndN.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年04月22日 星期五 09时45分00秒
************************************************************************/
#include "NumberOf1Between1AndN.h"
/*
* 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数
* */
int NumberOf1Between1AndN_solution1(unsigned int n)
{
int numbers = 0;
int i = 0;
for (i = 0; i <= n; i++)
{
numbers += NumberOf1_solution1(i);
}
return numbers;
}
int NumberOf1_solution1(unsigned int n)
{
int numbers = 0;
while(n)
{
if (n%10 == 1)
numbers++;
n /= 10;
}
return numbers;
}
int powerBase10(unsigned int n)
{
int result = 1;
int i = 0;
for (i = 0; i < n; i ++)
result *= 10;
return result;
}
int NumberOf1Between1AndN_solution2(int n)
{
if (n <= 0)
return 0;
char StrN[50];
sprintf(StrN, "%d", n);
return NumberOf1_solution2(StrN);
}
int NumberOf1_solution2(const char *StrN)
{
if (!StrN || *StrN < '0' || *StrN > '9' || *StrN == '\0')
return 0;
int first = *StrN - '0';
int length = strlen(StrN);
if (length ==1 && first ==0)
return 0;
if (length == 1 && first > 0)
return 1;
int numFirst = 0;
if (first > 1)
numFirst = powerBase10(length -1);
else if (first == 1)
numFirst = atoi(StrN + 1) + 1;
int numOther = first*(length -1)*powerBase10(length -2);
int numRecursive = NumberOf1_solution2(StrN + 1);
return numFirst + numRecursive + numOther;
}