输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数

算法描述:

输入一个整数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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值