题目链接: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. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  2. KMeans实现

    KMeans实现 符号 \(K\): 聚类的个数 \(x^{(i)}\): 第i个样本 \(\mu_{1},\mu_{2},...\mu_{K}\): K个中心节点 \(c^{(i)}\): 第i个样 ...

  3. badboy详解篇

    上一篇学习了jmeter录制的两种方法,badboy是比较好用的一个,它本身就是个测试工具,今天具体来介绍一下: 1.检查点 检查点就是记录被测系统某个值的预期结果 以百度搜索gogomall为例子 ...

  4. Go语言备忘录(2):反射的原理与使用详解

    本文内容是本人对Go语言的反射原理与使用的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处:Go语言备忘录(2):反射的原理与使用详解,多谢! ...

  5. 正排索引(forward index)与倒排索引(inverted index)

    正常的索引一般是指关系型数据库里的索引. 把不同的数据存放到不同的字段中.如果要实现baidu或google那种搜索,就需要与一条记录的多个字段进行比对,需要 全表扫描,如果数据量比较大的话,性能就很 ...

  6. Full postback triggered by LinkButton inside GridView inside UpdatePanel

    GridView inside of a UpdatePanel,get the button to trigger a partial postback <asp:ScriptManager ...

  7. 一些在线开发手册api文档收藏

    java JavaSE8 api:https://docs.oracle.com/javase/8/docs/api/ JavaSE7 api:http://docs.oracle.com/javas ...

  8. linux 安装php扩展swoole redis

    本文讲的是已经有redis.so 和swoole.so文件的情况 我的环境是xampp php的扩展目录为 /opt/lampp/lib/php/extensions/no-debug-non-zts ...

  9. Java序列话和反序列化理解(New)

    public interface Serializable {} 该接口没有任何实现方法,是一种标志,instance of  Serializable 会判断object类型 一.序列化和反序列化的 ...

  10. 洛谷P1600 天天爱跑步(差分 LCA 桶)

    题意 题目链接 Sol 一步一步的来考虑 \(25 \%\):直接\(O(nm)\)的暴力 链的情况:维护两个差分数组,分别表示从左向右和从右向左的贡献, \(S_i = 1\):统计每个点的子树内有 ...