codeforces 792C. Divide by Three
题目链接: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的更多相关文章
- CodeForces - 792C Divide by Three (DP做法)
C. Divide by Three time limit per test: 1 second memory limit per test: 256 megabytes input: standar ...
- CodeForces 792C - Divide by Three [ 分类讨论 ]
删除最少的数位和前缀0,使得剩下的数能被3整除 等价于各数位数字之和能被3整除. 当前数位和可能是 0, 1, 2(mod 3) 0: 直接处理 1: 删除一个a[i]%3 == 1 或者 两个a[i ...
- 【codeforces 792C】Divide by Three
[题目链接]:http://codeforces.com/contest/792/problem/C [题意] 让你删掉最少的数字使得剩下的数字%3==0 [题解] 看代码..内置题解了现在. [完整 ...
- Divide by Three CodeForces - 792C
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You ...
- Codeforces 1176A Divide it!
题目链接:http://codeforces.com/problemset/problem/1176/A 思路:贪心,对第二个操作进行俩次等于将n变成n/3,第三个操作同理,我们将n不断除以2,再除以 ...
- 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, ...
- Codeforces 1270E - Divide Points(构造+奇偶性)
Codeforces 题目传送门 & 洛谷题目传送门 显然,直接暴力枚举是不可能的. 考虑将点按横纵坐标奇偶性分组,记 \(S_{i,j}=\{t|x_t\equiv i\pmod{2},y_ ...
- Codeforces 792C
题意:给出一个由0到9数字构成的字符串,要求删去最少的数位,使得这个字符串代表的数能被3整除,同时要求不能有前导零,并且至少有一位(比如数字11,删去两个1后就没有数位了,所以不符合).如果能够处理出 ...
- CodeForces - 1176A Divide it! (模拟+分类处理)
You are given an integer nn. You can perform any of the following operations with this number an arb ...
随机推荐
- h5移动端设置键盘搜索
点击键盘上的搜索按钮实现页面跳转 <form action="#list?goods_title={{message?message:''}}" @submit.preven ...
- [转]error MSB4018: The "GenerateResource" task failed unexpectedly
本文转自:https://github.com/Microsoft/msbuild/issues/364 After uninstall Visual Studio 2015 Update 1 RC ...
- .Net程序员玩转Android系列之一~Java快速入门
前言 前段时间受公司业务发展需要,探索性进入Android开发领域.一切从零开始,java基础,Java进阶,Android框架学习,Eclipse熟悉,最终到第一个即时通讯App完成,历经一个月的时 ...
- nltk模块
1. nltk简介 http://www.nltk.org 2. nltk能做什么? 2.1 搜索文本 单词搜索 相似词搜索 相似关键词识别 词汇分布图 生成文本 from nltk.book imp ...
- Spring学习笔记:Spring动态组装打印机
一.如何开发一个打印机 1.可灵活配置使用彩色魔盒或灰色魔盒 2.可灵活配置打印页面的大小 二.打印机功能的实现依赖于魔盒和纸张 三.步骤: 1.定义墨盒和纸张的接口标准 package cn.pri ...
- JSON 转 VO
需求 将获取的json数据直接转为vo 解决 利用net.sf.json.JSONObject的toBean() 确保json中的key值和vo中的字段名称一致 JSONObject jsonObje ...
- 使用mpvue搭建一个初始小程序
1. 初始化一个 mpvue 项目 现代前端开发框架和环境都是需要 Node.js 的,如果没有的话,请先下载 nodejs 并安装. 然后打开命令行工具: # 1. 先检查下 Node.js 是否安 ...
- 关于纯css写三角形在firefox下的锯齿问题
相信很多人都用过利用border来实现小三角箭头,百度一下,这类的文章多如牛毛,这里我还是啰嗦点把常用的方法陈列出来: .triangle_border_up{ width:; height:; bo ...
- [转]webapi部署在IIS7.5报404的解决方案
1.iis 目录权限设置 2.转自:http://www.cnblogs.com/youlies/p/6042169.html 在web.config添加如下节点 <system.webServ ...
- python SyntaxError: EOL while scanning string literal
错误原因是,字符串以 \ 结尾 或者字符串缺少引号. 写代码拼接windows 路径出现这个错误, 查资料才知道 python中字符串不能以 \ 结尾 我的代码如下 import os dirname ...