牛客每日练习----幸运数字Ⅰ,幸运数字Ⅱ,幸运数字Ⅲ

本文介绍了解决幸运数字问题的三个编程挑战,包括寻找最频繁出现的幸运数字子串、计算幸运数字序列之和以及通过特定操作改变数字。文章提供了详细的算法思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我喜欢给自己压力,必须得定一个很高的目标,逼自己朝着这个目标前进,不管会不会实现,都是一个动力。                                      ----喻言

链接:https://ac.nowcoder.com/acm/problem/15290
来源:牛客网

题目描述

定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
现在,给定一个字符串s,请求出一个字符串,使得:
1、它所代表的整数是一个幸运数字;
2、它非空;
3、它作为s的子串(不是子序列)出现了最多的次数(不能为0次)。
请求出这个串(如果有多解,请输出字典序最小的那一个)。

输入描述:

串s(1 <= |s| <= 50)。s只包含数字字符,可以有前导零。

输出描述:

一个串表示答案。
无解输出-1。

示例1

输入

复制

047

输出

复制

4

示例2

输入

复制

16

输出

复制

-1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
typedef long long ll;
using namespace std;
const int mod=998244353 ;  
const int N=1e6;
int a=0,b=0;
char R[55];
int main()
{
    
    scanf("%s",R);
    for(int i=0;R[i]!='\0';i++)
    {
        if(R[i]=='4')
			a++;
        if(R[i]=='7')
			b++;
    }
    if(!a&&!b)
		printf("-1\n");
    else if(a>=b)
		printf("4\n");
    else 
		printf("7\n");
    return 0;
}

链接:https://ac.nowcoder.com/acm/problem/15291
来源:牛客网

题目描述

定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
定义next(x)为大于等于x的第一个幸运数字。给定l,r,请求出next(l) + next(l + 1) + ... + next(r - 1) + next(r)。

输入描述:

两个整数l和r (1 <= l <= r <= 1000,000,000)。

输出描述:

一个数字表示答案。

示例1

输入

复制

2 7

输出

复制

33

示例2

输入

复制

7 7

输出

复制

7
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
typedef long long ll;
using namespace std;
const int mod=998244353 ;  
const int N=1023;
ll l,r,p[1100];
int ct = 1;
void hs(ll x) {
    if (ct >= N||x > 1e9)
		return;
    if (x != 0) {
        p[ct] = x;
        ct++;
    }
    hs(x * 10 + 4);
    hs(x * 10 + 7);
}
int main() {
    cin >> l >> r;
    p[0] = 4444444444;
	hs(0);
    sort(p, p + ct);
    ll jg = 0,x = l - 1;
    for (int i = 0;i < ct;i++) {
        if (p[i] < l)
			continue;
        if (p[i] >= r) {
            jg = jg + p[i] * (ll)(r - x);
            break;
        }
        jg = jg + p[i] * (ll)(p[i] - x);
        x = p[i];
    }
    cout << jg << endl;
    return 0;
}

链接:https://ac.nowcoder.com/acm/problem/15292
来源:牛客网

题目描述

定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
假设现在有一个数字d,现在想在d上重复k次操作。
假设d有n位,用d1,d2,...,dn表示。
对于每次操作,我们想要找到最小的x (x < n),使得dx=4并且dx+1=7。
如果x为奇数,那么我们把dx和dx+1都变成4;
否则,如果x为偶数,我们把dx和dx+1都变成7;
如果不存在x,那么我们不做任何修改。
现在请问k次操作以后,d会变成什么样子。

输入描述:

第一行两个整数n,k表示d的长度和操作次数。
第二行一个数表示d。数据保证不存在前导零。
1 <= n <= 100,000
0 <= k <= 1000,000,000

输出描述:

一个数字表示答案。

示例1

输入

复制

7 4
4727447

输出

复制

4427477

示例2

输入

复制

4 2
4478

输出

复制

4478
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
typedef long long ll;
using namespace std;
const int mod=998244353 ;  
const int N=100010;
char d[N];
int n,k,l;
int main()
{
    scanf("%d %d %s",&n,&k,d);
    for(int i=0;i<n;i++)
    {
        if(l==k)
        	break;
        if(d[i]=='4'&&d[i+1]=='7')
        {
            if(i-1>=0&&d[i-1]=='4'&&i%2==1)
            {
                int m=k-l;
                if(m%2==0)
                    break;
                else
                {
                    if(i%2==0)
                        d[i+1]='4';
                    else
                    	d[i]='7';
                    break;
                }
            }
            else
            {
                if(i%2==0)
                    d[i+1]='4';
                else
                    d[i]='7';
                l++;
            }
        }
        else
            continue;
        i=0;
    }
    printf("%s\n",d);
    return 0;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值