Codeforces Round #306 (Div. 2) C 模拟

本文介绍了一种算法,用于判断通过删除某些数字后剩余的数字串是否能组成被8整除的数字。该算法首先检查单个数字,然后是两位数和三位数的情况。




链接:戳这里


C. Divisibility by Eight
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.

Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.

If a solution exists, you should print it.

Input
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.

Output
Print "NO" (without quotes), if there is no such way to remove some digits from number n.

Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.

If there are multiple possible answers, you may print any of them.

Examples
input
3454
output
YES
344
input
10
output
YES
0
input
111111
output
NO


题意:

给出n个数字,任意删除一些数判断是否能整除8


思路:

首先1000/8=125所以超过三位数以上的只需要判断后三位能否整除8就行了

在处理两位的一位的


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
string s;
bool pd(char A,char B,char C){
    int x=(A-'0')*100+(B-'0')*10+(C-'0');
    if(x%8==0) return true;
    return false;
}
int main(){
    cin>>s;
    int n=s.size();
    for(int i=0;i<n;i++){
        if(s[i]=='8' || s[i]=='0'){
            cout<<"YES"<<endl;
            cout<<s[i]<<endl;
            return 0;
        }
    }
    if(n==1){
        int x=s[0]-'0';
        if(x%8==0){
            cout<<"YES"<<endl;
            cout<<x<<endl;
        } else {
            cout<<"NO"<<endl;
        }
        return 0;
    }
    char O='0';
    if(n==2){
        if(pd(O,O,s[0])){
            cout<<"YES"<<endl;
            cout<<s[0]<<endl;
        } else if(pd(O,s[1],s[0])){
            cout<<"YES"<<endl;
            cout<<s[1]<<s[0]<<endl;
        } else if(pd(O,O,s[1])){
            cout<<"YES"<<endl;
            cout<<s[1]<<endl;
        } else {
            cout<<"NO"<<endl;
        }
        return 0;
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(pd(O,s[i],s[j])){
                cout<<"YES"<<endl;
                cout<<s[i]<<s[j]<<endl;
                return 0;
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n;k++){
                if(pd(s[i],s[j],s[k])){
                    cout<<"YES"<<endl;
                    cout<<s[i]<<s[j]<<s[k]<<endl;
                    return 0;
                }
            }
        }
    }
    cout<<"NO"<<endl;
    return 0;
}



### Codeforces Round 703 Div. 2 比赛题解与总结 #### A. Adjacent Replacements Problem 对于此问题,给定一个长度为 \(n\) 的整数数组 \(a_1, a_2, \ldots , a_n\) 和一个正整数 \(k\). 如果存在一对相邻位置 \(i,i+1\) (\(1≤i<n\)),使得 \(|a_i−a_{i+1}|>k\) 则可以执行一次操作:选择任意一对这样的相邻元素并使它们相等。目标是最少的操作次数让所有的差值不超过 \(k\)[^1]。 解决方法是遍历整个数组,计算每对相邻元素之间的差异,并判断这些差异是否超过了 \(k\) 。如果超过,则记录下需要调整的位置数量作为最终的结果返回。 ```cpp #include <iostream> using namespace std; void solveA(){ int t; cin >> t; while(t--){ int n,k; cin>>n>>k; vector<int>a(n); for(auto& x:a){ cin>>x; } bool flag=true; int cnt=0; for(int i=0;i<n-1;++i){ if(abs(a[i]-a[i+1])>k){ ++cnt; flag=false; } } cout<<(flag?"Yes":"No")<<endl; } } ``` #### B. Minimum Grid Path 该题目要求在一个无限大小的网格上找到一条从起点到终点的最短路径,在移动过程中某些格子可能被标记为障碍物不可通过。为了最小化步数,应当优先考虑横向或纵向直线前进直到遇到阻碍为止;之后转向其他方向继续前行直至抵达目的地[^2]。 实现思路在于模拟行走过程中的决策逻辑——当面临多个可行的选择时总是挑选能够最快接近目标的那个选项。具体做法可以通过广度优先搜索算法来完成。 ```cpp // 假设此处省略了完整的B题解答代码... ``` #### C. Maximum width of Binary Tree 这个问题涉及到二叉树的最大宽度定义以及如何有效地求得这一数值。最大宽度是指某一层拥有最多的节点数目。一种有效的策略是从根部开始逐层遍历整棵树结构,利用队列辅助存储每一级待访问结点的信息,从而轻松统计各层的实际规模并找出其中最大的那个层次所含有的节点总数即为我们所需的答案[^3]。 上述三种不同类型的挑战展示了编程竞赛中常见的几种思考模式和技术手段的应用实例。无论是简单的贪心法还是较为复杂的图论模型构建都体现了选手们灵活运用基础知识解决问题的能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值