题目链接:codeforces 792C. Divide by Three

今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下。。。分类吧。删除一个数字模3为M的或删除两个模3为3-M的(还有一些要删零),具体看代码。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int L, M, i, j, W, A[];
bool C;
string S, R;
int main () {
cin>>S;
L=S.length();
for (i=;i<L;i++) {
A[i]=S[i]-'';
M=(M+A[i])%;
}
if (M==) {//不用进行删除
cout<<S<<'\n';
return ;
}
for (i=;i<L;i++) {//从第二个数字开始 选择删除一个数字
if (A[i]%==M) {
for (j=;j<i;j++) {
cout<<A[j];
}
for (j=i+;j<L;j++) {
cout<<A[j];
}
cout<<'\n';
return ;
}
}
if (A[]%==M&&A[]!=) {//还是删除一个数字(模3为M),这种情况是第二个数字不为0并且可以删除第一个
for (j=;j<L;j++) {
cout<<A[j];
}
cout<<'\n';
return ;
}
for (i=L-;i>=;i--) {//删除两个数字(均是模3为3-M) 或更多
if (A[i]%==-M) {
if (W) {//判断有两个可删数字
for (j=;j<L;j++) {
if (j!=W&&j!=i) {//删除两个及以上,可能有前导零
if (A[j]) {
C=;
cout<<A[j];
}
else if (C) {
cout<<;
}
}
}
if (!C) {
if (L<) {
cout<<-<<'\n';
}
else {
cout<<<<'\n';
}
}
return ;
}
else {
W=i;
}
}
}
if (A[]%==M) {//删除一个数字或更多 这种情况是第一个数字是模3为M,第二个数字为0。这里先删除第一个数字
for (j=;j<L;j++) {
if (A[j]) {//可能有前导零
C=;
cout<<A[j];
}
else if (C) {
cout<<;
}
}
if (!C) {
if (L==) {
cout<<-<<'\n';
}
else {
cout<<<<'\n';
}
}
}
return ;
}
/*
//这代码的选择删除 模M 或 模3-M 的顺序很重要, 最后两种方法如果换个先后顺序就错了,比如这组数据
//20000111
//200001
//之前我的做法是同时进行两种删除方法,通过判断哪种方法删除数字更少,来选择输出更优的那个,而这个代码则不用比较两种情况,直接按这种顺序输出就行
*/

codeforces 792C. Divide by Three的更多相关文章

  1. CodeForces - 792C Divide by Three (DP做法)

    C. Divide by Three time limit per test: 1 second memory limit per test: 256 megabytes input: standar ...

  2. CodeForces 792C - Divide by Three [ 分类讨论 ]

    删除最少的数位和前缀0,使得剩下的数能被3整除 等价于各数位数字之和能被3整除. 当前数位和可能是 0, 1, 2(mod 3) 0: 直接处理 1: 删除一个a[i]%3 == 1 或者 两个a[i ...

  3. 【codeforces 792C】Divide by Three

    [题目链接]:http://codeforces.com/contest/792/problem/C [题意] 让你删掉最少的数字使得剩下的数字%3==0 [题解] 看代码..内置题解了现在. [完整 ...

  4. Divide by Three CodeForces - 792C

    A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You ...

  5. Codeforces 1176A Divide it!

    题目链接:http://codeforces.com/problemset/problem/1176/A 思路:贪心,对第二个操作进行俩次等于将n变成n/3,第三个操作同理,我们将n不断除以2,再除以 ...

  6. Codeforces 977D Divide by three, multiply by two(拓扑排序)

      Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, ...

  7. Codeforces 1270E - Divide Points(构造+奇偶性)

    Codeforces 题目传送门 & 洛谷题目传送门 显然,直接暴力枚举是不可能的. 考虑将点按横纵坐标奇偶性分组,记 \(S_{i,j}=\{t|x_t\equiv i\pmod{2},y_ ...

  8. Codeforces 792C

    题意:给出一个由0到9数字构成的字符串,要求删去最少的数位,使得这个字符串代表的数能被3整除,同时要求不能有前导零,并且至少有一位(比如数字11,删去两个1后就没有数位了,所以不符合).如果能够处理出 ...

  9. CodeForces - 1176A Divide it! (模拟+分类处理)

    You are given an integer nn. You can perform any of the following operations with this number an arb ...

随机推荐

  1. 【c++】访问控制

    1.  类内的访问控制 在基类中,public和private具有普通的含义:用户(即基类的对象)可以访问public成员(包括函数.数据),而不能访问private成员.private只能被基类的成 ...

  2. 文档类型DTD,DOCTYPE和浏览器模式

    出处:http://blog.csdn.net/freshlover/article/details/11616563 浏览器从服务端获取网页后会根据文档的DOCTYPE定义显示网页,如果文档正确定义 ...

  3. Java - 如何进行安全发布

    首先让我简单解释一下所谓"发布". 发布(publish),使对象可以在当前作用域之外的代码中可见,如果该对象被发布,则该对象的非私有域中引用的所有实例同样也会被发布. 不仅仅是作 ...

  4. java读取txt文件,对字符串进行操作后导出txt文件

    嘿嘿,代码略为简单,不再多做解释,直接上码! package org.lq.com.util; import java.io.File; import java.io.InputStreamReade ...

  5. ES6,先知道这些必会的才行

    变量声明 const 和 let 不要用 var,而是用 const 和 let,分别表示常量和变量.不同于 var 的函数作用域,const 和 let 都是块级作用域. const DELAY = ...

  6. linux系统下php扩展的安装

    0. 这里以php安装redis扩展为例 1. 首先下载并解压redis扩展包 [root@xxx ~]# cd /usr/local/src [root@xxx src]# wget https:/ ...

  7. mysql case when & concat & SUBSTRING_INDEX & not & having 使用的小case

    1. 代码 SELECT a.id, a.activity_name, ( CASE WHEN a.activity_end_time > now() THEN '参与中' ELSE ( CAS ...

  8. 51nod 1135 原根(原根)

    题意 题目链接 Sol 可以证明素数的原根不会超过他的\(\frac{1}{4}\) 那么预处理出\(P - 1\)的所有的质因数\(p_1, p_2 \dots p_k\),暴力判断一下,如果$\e ...

  9. C#中的Sealed和J#中的Final比较(转载)

    Sealed与Final修饰符其实并不是一个语言平台的产物,他们有着各自所属的语言环境,但这两个关键字都是.Net平台中不可或缺的,那么二者用法几何,随本文一探究竟. 一.Sealed sealed ...

  10. Open images from USB camera on linux using V4L2 with OpenCV

    I have always been using OpenCV's VideoCapture API to capture images from webcam or USB cameras. Ope ...