程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)

hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)

編輯:關於C

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5676

ztr loves lucky numbers

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 594Accepted Submission(s): 257

Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.

One day ztr came across a positive integer n. Help him to find the least super lucky number which is not less than n.

Input There are T(1≤n≤105)cases

For each cases:

The only line contains a positive integern(1≤n≤1018). This number doesn't have leading zeroes.

Output For each cases
Output the answer
Sample Input
2
4500
47

Sample Output
4747
47

Source BestCoder Round #82 (div.2)

題目大意:

ztr喜歡幸運數字,他對於幸運數字有兩個要求
1:十進制表示法下只包含4、7
2:十進制表示法下4和7的數量相等
比如47,474477就是
而4,744,467則不是

現在ztr想知道最小的但不小於n的幸運數字是多少

解題思路:暴力打出所有幸運數~~~~然後二分查找即可。

詳見代碼。

#include 
#include 

using namespace std;

#define ll long long

ll a[100000];
int k=0;

void dfs(ll ans,int num4,int num7)
{
    if (num4==0&&num7==0)
    {
        a[k++]=ans;
        return;
    }
    if (num4==0)
    {
        dfs(ans*10+7,num4,num7-1);
    }
    else if (num7==0)
    {
        dfs(ans*10+4,num4-1,num7);
    }
    else
    {
        dfs(ans*10+4,num4-1,num7);
        dfs(ans*10+7,num4,num7-1);
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    for (int i=2;i<=18;i+=2)
        dfs(0,i/2,i/2);
    while (t--)
    {
        ll n;
        scanf("%lld",&n);
        if(n>777777777444444444LL)
        {
            printf("44444444447777777777\n");
            continue;
        }
        int l=0,r=k;
        while (r>l)
        {
            int mid=(l+r)/2;
           // cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved