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 ...
随机推荐
- Bash编程(1) 基础
1. 基本知识 (1) $HOME: 当前用户的家目录 (2) `pwd`或$PWD:当前目录 (3) 脚本命名避免使用test,通过type -a test,可以查看所有匹配test的命令 gas@ ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- 错误:严重: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is
严重: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request proc ...
- [转]前端构建工具gulpjs的使用介绍及技巧
本文转自:http://www.cnblogs.com/2050/p/4198792.html gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非 ...
- HTTP POST上传文件说明及注意事项
这一阵子因为项目需要要对接别家公司的restful接口,其中有一个接口是上传视频文件.接着上网找找资料.后来文件是上传成功了,但上服务器一看,文件没有后缀,我勒个去,这是什么个鬼.接着继续找资料,最终 ...
- (转载).NET的五层架构
我们刚开始学习架构的时候,首先会想到分层的概念,分层架构比较经典的是三层架构,那么,什么是三层架构呢?它包括表现层,业务层,数据访问层:而对于一个新手来说,从抽象意义上的三层架构,逻辑上就划分为三个层 ...
- PLC编程逻辑思路
PLC编程逻辑思路 在整个执行过程的流程中,都是在不断地找启动条件,停止条件以及输出结果.当条件不够时,就得想办法如果添加标志位,根据已有条件去构造条件:当结果开发耦合时,就制造中间继电器去除耦合. ...
- Vue-[v-model]理解示例
对应文档节点: https://vuefe.cn/v2/guide/components.html#Customizing-Component-v-model <body> <div ...
- [android] 切换界面的问题
1. 界面重复创建的问题 2. 中间容器每次切换,都会清空容器中的子对象问题 3. 点击返回键的处理 解决重复创建的问题: 传递Class字节码对象,利用泛型来规定对象 判断界面是否存在,如果存在重复 ...
- CentOS7下配置FTP服务
1.参考教程: 腾讯云开发者实验室:基于 CentOS 搭建 FTP 文件服务 Linux就该这么学>:第11章 使用Vsftpd服务传输文件 (需要自己百度) 2.实验环境: VMware 1 ...