1到n之间1的个数
来源:互联网 发布:淘宝上买狗可靠的店 编辑:程序博客网 时间:2024/05/16 01:26
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.
For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
算法思想:
循环求出每个数中1的个数,累计之,若满足f(n)=n,则退出,否则继续。
代码如下:
/**//************************************************************************
* 0~n之间1的个数,如f(13)=6
* 1,2,3,4,5,6,7,8,9,10,11,12,13.1的个数为6
* 要求:输入一个正整数n,求出f(n),及求解f(n)=n
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <Windows.h>
class CalculationTimes
...{
public:
CalculationTimes()...{}
~CalculationTimes()...{}
int GetTimes(int n);
};
//计算正整数n中1的个数
int CalculationTimes::GetTimes(int n)
...{
int count=0;
while(n)
...{
if(n%10==1)
count++;
n/=10;
}
return count;
}
//显示菜单
void show_menu()
...{
printf("--------------------------------------------- ");
printf("input command to test the program ");
printf(" i or I : input n to test ");
printf(" g or G : get n to enable f(n)=n ");
printf(" q or Q : quit ");
printf("--------------------------------------------- ");
printf("$ input command >");
}
void main()
...{
char sinput[10];
int n;
show_menu();
scanf("%s",sinput);
while(stricmp(sinput,"q")!=0)
...{
int t=0,count=0;
if(stricmp(sinput,"i")==0)
...{
printf(" please input an integer:");
scanf("%d",&n);
count=0;
CalculationTimes obj;
t=GetTickCount();
for(int i=1;i<=n;i++)
count+=obj.GetTimes(i);
t=GetTickCount()-t;
printf(" count=%d time=%d ",count,t);
}
else if(stricmp(sinput,"g")==0)
...{
CalculationTimes obj;
n=2;
count=1;
t=GetTickCount();
while(1)
...{
count+=obj.GetTimes(n);
if(count==n)
break;
n++;
}
t=GetTickCount()-t;
printf(" f(n)=n=%d time=%d ",n,t);
}
//输入命令
printf("$ input command >");
scanf("%s",sinput);
}
}
* 0~n之间1的个数,如f(13)=6
* 1,2,3,4,5,6,7,8,9,10,11,12,13.1的个数为6
* 要求:输入一个正整数n,求出f(n),及求解f(n)=n
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <Windows.h>
class CalculationTimes
...{
public:
CalculationTimes()...{}
~CalculationTimes()...{}
int GetTimes(int n);
};
//计算正整数n中1的个数
int CalculationTimes::GetTimes(int n)
...{
int count=0;
while(n)
...{
if(n%10==1)
count++;
n/=10;
}
return count;
}
//显示菜单
void show_menu()
...{
printf("--------------------------------------------- ");
printf("input command to test the program ");
printf(" i or I : input n to test ");
printf(" g or G : get n to enable f(n)=n ");
printf(" q or Q : quit ");
printf("--------------------------------------------- ");
printf("$ input command >");
}
void main()
...{
char sinput[10];
int n;
show_menu();
scanf("%s",sinput);
while(stricmp(sinput,"q")!=0)
...{
int t=0,count=0;
if(stricmp(sinput,"i")==0)
...{
printf(" please input an integer:");
scanf("%d",&n);
count=0;
CalculationTimes obj;
t=GetTickCount();
for(int i=1;i<=n;i++)
count+=obj.GetTimes(i);
t=GetTickCount()-t;
printf(" count=%d time=%d ",count,t);
}
else if(stricmp(sinput,"g")==0)
...{
CalculationTimes obj;
n=2;
count=1;
t=GetTickCount();
while(1)
...{
count+=obj.GetTimes(n);
if(count==n)
break;
n++;
}
t=GetTickCount()-t;
printf(" f(n)=n=%d time=%d ",n,t);
}
//输入命令
printf("$ input command >");
scanf("%s",sinput);
}
}
- 1到n之间1的个数
- 求0到n之间出现的"1"的个数
- 求1到n之间素数的个数
- 统计0到n之间1的个数
- 统计0到n之间1的个数[数学]
- 统计0到n之间1的个数
- 统计0到n之间1的个数
- 求解1到n之间素数的个数
- 统计0到n之间1的个数
- 计算1 到 N 之间 某个数字出现的个数
- 写一个函数,计算0到n之间2的个数 (1-9的个数)
- 计算从1到n(包括n)之间出现的各位数字的个数
- 1到n,n个数的最小公倍数
- 0~n之间1的个数
- 求 1~n 之间素数的个数
- 有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。
- 有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数
- 统计1到n之间的所有数字中1出现的个数
- 送给编程初学者的良言警句
- 数据库定义语言DDL
- 使用 Linux 系统调用的内核命令
- 深刻的自我剖析——这些你都做到了吗
- Yahoo!社区架构
- 1到n之间1的个数
- web 2.0 简介
- Tailrank 网站架构
- J2SE综合:深入了解Java 5.0的垃圾收集
- 小工具集_运行时截图
- [笔记/简译]XAML揭秘(4)
- 程序员面试题精选{57):求n的加法组合
- 进阶--学习J2SE过程中的30个基本概念 来源:赛迪网 作者:2277802
- 密室逃脱系列