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

比赛看到这道题的时候,看到这句话counting them one by one is slow,认为暴力就可能会超时,当时就没有往那方面去想了,后来没题目做了,就专心搞这道题,用暴力+打表去尝试就过了。比赛的时候还是要敢于尝试啊,仅仅要有想法就要去实现,哪怕想法是错误的,我们也要勇于尝试!仅仅有不断的探索才会有进步;

这道题的思路就是打表,由于数据量还不算太大,打表还是能够做的出;

以下是ac代码;

#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;
}
时间: 2024-09-30 14:14:32

XTU OJ 1210 Happy Number (暴力+打表)的相关文章

HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)

beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 801    Accepted Submission(s): 518 Problem Description Let A=∑ni=1ai?10n?i(1≤ai≤9)(n is the number of A's digits). We call A as "

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

筛法暴力打表 --- hdu : 12876 Quite Good Numbers

Quite Good Numbers Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 77, Accepted users: 57 Problem 12876 : No special judgement Problem description A "perfect" number is an integer that is equal to the sum

【ZOJ】3785 What day is that day? ——浅谈KMP应用之ACM竞赛中的暴力打表找规律

首先声明一下,这里的规律指的是循环,即找到最小循环周期.这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”. 先来看一道题 ZOJ 3785 What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? Input There are multiple tes

XTU OJ 1175 Hurry Up(三分法&amp;&amp;穷举法)

 Hurry Up Accepted : 88   Submit : 345 Time Limit : 1000 MS   Memory Limit : 65536 KB Problem Description GG is some what afraid of his MM. Once his MM asks, he will always try his best to rush to their home. Obvious, he can run home in straight li

HDU 4952 Number Transformation 打表规律

点击打开链接 Number Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 495    Accepted Submission(s): 248 Problem Description Teacher Mai has an integer x. He does the following operation

acdream 1210 Chinese Girls&#39; Amusement (打表找规律)

题意:有n个女孩围成一个圈从第1号女孩开始有一个球,可以往编号大的抛去(像传绣球一样绕着环来传),每次必须抛给左边第k个人,比如1号会抛给1+k号女孩.给出女孩的人数,如果他们都每个人都想要碰到球一次,那么这个k应该是多少(满足 1 ≤ K ≤ N/2 且 k必须尽量大)?   例如:n=7,那么1号开始拿球,抛球的顺序是 1, 4, 7, 3, 6, 2, 5, 1.  当球重新回到1女孩手中时,每个人刚好只玩了一次.注:这个数字相当大(3 ≤ N ≤ 102000) 思路: 方法(1): 暴

HDU 1012 u Calculate e【暴力打表,水】

u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 46844    Accepted Submission(s): 21489 Problem Description A simple mathematical formula for e is where n is allowed to go to infini

hdu 1431 素数回文(暴力打表,埃托色尼筛法)

这题开始想时,感觉给的范围5 <= a < b <= 100,000,000太大,开数组肯定爆内存,而且100000000也不敢循环,不超时你打我,反正我是不敢循环. 这题肯定得打表,筛素数肯定用埃托色尼筛法(不好意思把大名鼎鼎的埃拉托色尼名字打错了,表打我). 再看当你所找的回文数的位数为偶数时,有如下定理除11外所有偶数位数的回文数都能被11整除,所以所有偶数位数的回文都不是素数. 证明看如下(我手写的) 手机像素渣(凑活着吧)字丑也凑和着. 证完后我们在来说题目给的数据范围 所以当