XTU OJ 1210 Happy Number (暴力+打表)
Problem Description
Recently, Mr. Xie learn the concept of happy number. A happy number is a number contain all digit 7 or only 1 digit other than 7. For example, 777 is a happy number because 777 contail all digit 7, 7177 and 87777 both happy number because only 1 digit other
than 7. Whereas 887,799 9807,12345, all of them are not happy number. Now Mr. xie want to know for a given integer n, how many number among [1,n] are happy numbers, but counting them one by one is slow, can you help him?
Input
First line an integer t indicate there are t testcases(1≤t≤100). Then t lines follow, each line an integer n(1≤n≤106, n don't have leading zero).
Output
Output case number first, then the answer.
Sample Input
5
1
7
17
20
30
Sample Output
Case 1: 1
Case 2: 7
Case 3: 10
Case 4: 10
Case 5: 11
#include "stdio.h"
#include "string.h"
const int maxn=1000000+10;
int a[maxn];
void init() //打表枚举
{
int i;
for(i=0;i<=9;i++) //这里今天做的时候卡在这了,把i的值设为了1,后来检查这个错误查了好久,最后看数据少了几个,才发现这里错了;
{ //有最后一位为0的情况没有考虑到 a[i]=1; a[i*10+7]=1; //二位的情况
a[7*10+i]=1; a[i*100+7*10+7]=1;//三位
a[7*100+i*10+7]=1;
a[7*100+7*10+i]=1; a[i*1000+7*100+7*10+7]=1;//四位
a[7*1000+i*100+7*10+7]=1;
a[7*1000+7*100+i*10+7]=1;
a[7*1000+7*100+7*10+i]=1; a[i*10000+7*1000+7*100+7*10+7]=1;
a[7*10000+i*1000+7*100+7*10+7]=1;
a[7*10000+7*1000+i*100+7*10+7]=1;
a[7*10000+7*1000+7*100+i*10+7]=1;
a[7*10000+7*1000+7*100+7*10+i]=1; a[i*100000+7*10000+7*1000+7*100+7*10+7]=1;
a[7*100000+i*10000+7*1000+7*100+7*10+7]=1;
a[7*100000+7*10000+i*1000+7*100+7*10+7]=1;
a[7*100000+7*10000+7*1000+i*100+7*10+7]=1;
a[7*100000+7*10000+7*1000+7*100+i*10+7]=1;
a[7*100000+7*10000+7*1000+7*100+7*10+i]=1;
}
}
int main()
{
memset(a,0,sizeof(a));
init();
int t,i,count,n,j;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
count=0;
scanf("%d",&n);
for(j=1;j<=n;j++)
{
if(a[j]==1)
count++;
}
printf("Case %d: ",i);
printf("%d\n",count);
}
return 0;
}
另一种写法,感觉内存比上面的要少;
#include<iostream>
#include<vector>
#include<cstdio>
using namespace std; int main()
{
// freopen("a.txt","r",stdin);
int t, n, i, j;
int p[200] = {1,2,3,4,5,6,7,8,9,17,27,37,47,57,67,70,71,72,
73,74,75,76,77,78,79,87,97,177,277,377,477,577,
677,707,717,727,737,747,757,767,770,771,772,773,
774,775,776,777,778,779,787,797,877,977,1777,2777,
3777,4777,5777,6777,7077,7177,7277,7377,7477,7577,7677,
7707,7717,7727,7737,7747,7757,7767,7770,7771,7772,7773,7774,
7775,7776,7777,7778,7779,7787,7797,7877,7977,8777,9777,17777,
27777,37777,47777,57777,67777,70777,71777,72777,73777,74777,75777,
76777,77077,77177,77277,77377,77477,77577,77677,77707,77717,77727,
77737,77747,77757,77767,77770,77771,77772,77773,77774,77775,77776,
77777,77778,77779,77787,77797,77877,77977,78777,79777,87777,97777,
177777,277777,377777,477777,577777,677777,707777,717777,727777,737777,
747777,757777,767777,770777,771777,772777,773777,774777,775777,776777,
777077,777177,777277,777377,777477,777577,777677,777707,777717,777727,
777737,777747,777757,777767,777770,777771,777772,777773,777774,777775,
777776,777777,777778,777779,777787,777797,777877,777977,778777,779777,
787777,797777,877777,977777};
cin >> t;
for(i = 1; i <= t; ++ i)
{
cin >> n;
for(j = 0; p[j] <= n && j < 189; ++ j);
printf("Case %d: %d\n",i,j);
}
return 0;
}
XTU OJ 1210 Happy Number (暴力+打表)的更多相关文章
- Friends number NBUT - 1223 (暴力打表)
Paula and Tai are couple. There are many stories between them. The day Paula left by airplane, Tai s ...
- HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)
beautiful number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- HDU 1216 Assistance Required(暴力打表)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1216 Assistance Required Time Limit: 2000/1000 MS (Ja ...
- ACM/ICPC 之 暴力打表(求解欧拉回路)-编码(POJ1780)
///找到一个数字序列包含所有n位数(连续)一次且仅一次 ///暴力打表 ///Time:141Ms Memory:2260K #include<iostream> #include< ...
- HDU 1012 u Calculate e【暴力打表,水】
u Calculate e Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Codeforces 914 C 数位DP+暴力打表+思维
题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...
- 暴力打表之hdu 2089
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 有两种方法: 1.数位DP算法 2.暴力打表——真是个好法子!!! 接下来是注意点: 1.一般这 ...
- hdu 1431 素数回文(暴力打表,埃托色尼筛法)
这题开始想时,感觉给的范围5 <= a < b <= 100,000,000太大,开数组肯定爆内存,而且100000000也不敢循环,不超时你打我,反正我是不敢循环. 这题肯定得打表 ...
随机推荐
- extern "C" {} 来沟通C和C++
比如说你用C++开发了一个DLL库,为了能够让C语言也能够调用你的DLL输出(Export)的函数,你需要用extern "C"来强制编译器不要修改你的函数名. 通常,在C语言的头 ...
- Oracle 面试宝典 - General Questions
转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...
- HDU 1996
Problem Description n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了柱子,并不会把大盘放到小盘上, ...
- 解决JsonFormat日期少一天问题
使用Jackson的@JsonFormat注解时出现少一天 比如数据库存的日期是2015-01-05,转成json则变成了2015-01-04 解决办法: @JsonFormat(pattern=&q ...
- 为什么Tomcat的webapps目录下新建的目录不能访问html文件?
在Tomcat安装目录中,webapps默认为部署网站用的目录.webapps/ROOT是网站的根目录,其它目录都是网站的子目录,如webapps\jsp-examples目录.但是,当我们新建一个子 ...
- Js使用word书签填充内容
Js使用word书签填充内容 1.在模板文件中需要填充的地方插入书签 填充内容为:(|光标所在处) 填写书签名,点击添加完成: 2.使用js打开模板,获取书签位置,填充数据: function pri ...
- BZOJ 1052 覆盖问题
Description 某人在山上种了N棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他决定用3个L*L的正方形塑料薄膜将小 ...
- 【Java】理解 UDDI
跟上规范的不断发展 统一描述.发现和集成(Universal Description, Discovery, and Integration,UDDI)项目继续丰富企业用于在 UDDI 业务注册中心表 ...
- 构造函数语义学之Default Constructor构建操作
一.Default Constructor的构建操作 首先大家要走出两个误区: 1).任何class如果没有定义default constructor,就会被合成一个来. 2).便以其合成出来的def ...
- FFT(快速傅里叶变换):HDU 4609 3-idiots
3-idiots Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...