题目链接: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 1006 ZOJ问题 2010年浙江大学计算机及软件工程研究生机试真题

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16244 解决:2742 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC.是 ...

  2. BNU27937——Soft Kitty——————【扩展前缀和】

    Soft Kitty Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class n ...

  3. Truncated incorrect DOUBLE value: 'NO_REFUND'

    解决办法:Mysql中,如果一个字段是字符串,则一定要加单引号 问题原因: `item_refund_state` ) NOT NULL item_refund_state字段的类型是varchar但 ...

  4. C#把大写英文变成小写英文,把小写英文变成大写英文

    static void Main(string[] args) { string s;  // 声明一个变量,来接受用户输入的值. Console.WriteLine("请输入一个字符串:& ...

  5. 在ASP.NET CORE中启用favicon.ico

    在静态页面中添加网站标志只需在<head>标签中添加<link rel="shortcut icon" href="favicon.ico" ...

  6. react-native学习之环境安装

    1.首先是java环境安装-安装JDK 2.安装Android-SDK,推荐以下地址:http://tools.android-studio.org/index.php/sdk 然后打开SDK Man ...

  7. spring mvc随笔

    一.SpringMvc学习笔记1.使用SpringMvc时需在web.xml文件中添加配置 <servlet> <servlet-name>springMVC</serv ...

  8. 移动端Push推送

    移动端Push推送 移动端开发逃不掉要做推送,这里给出服务端一种省时省力的解决方案. iOS:PushSharp.Apple.苹果有自己的推送服务,我们按照规则推送数据就好.这里我选取PushShar ...

  9. Java温故而知新(4)类String字符串

    字符串是由字符组成,在Java中,字符串是对象,是描述字符的基本数据结构.String类可以用来保存一个字符串,本类是最终类,不允许继承: 1.String对象的创建 初始化 由于String对象特别 ...

  10. Redis数据类型及常用命名总结

    Redis数据类型: Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合).  1.String(字符串) ...