解题思路:

  1. 只有尾数为25,50,75,00的数才可能是25的倍数。

  2. 对字符串做4次处理,以25为例。

    a. 将字符串中的最后一个5移到最后一位。计算交换次数。(如果没有找到5,则不可能凑出25,考虑50、75、00)

    b. 字符串已经改变,将此时最后一个2移到倒数第二位。计算交换次数。 (如果没有找到2,则也不可能凑出25,考虑50、75、00)

    c. 将除了最后两位之外的第一个非0的数字移到首位,计算交换次数。(如果找不到除了最后两位之外的非0数字,则不可能凑出25,考虑50、75、00)

注意:

我的WA点:

输入25

输出0

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; int solve(string s, char c1, char c2){
// cout << s << endl;
int len = s.length()-1;
int ans = 0;
int flag = false;
for(int i = 0;s[i]; ++i){
if(s[i] == c2){
flag = true;
for(int j = i;j > 0; j--){
swap(s[j],s[j-1]);
ans++;
}
break;
}
}
// cout << ans << endl;
if(!flag) return -1;
flag = false;
for(int i = 1;s[i]; ++i){
if(s[i] == c1){
flag = true;
for(int j = i;j > 1; j--){
swap(s[j],s[j-1]);
ans++;
}
break;
}
}
if(!flag) return -1;
if(len == 1) return ans;
int con = -1;
for(int i = len;i > 1; --i){
if(s[i] != '0'){
con = i;
break;
}
}
if(con == -1){
return -1;
}else{
return ans+len-con;
}
} int main(){
ios::sync_with_stdio(false);
string s;
while(cin >> s){
reverse(s.begin(), s.end());
int ans = INT_MAX;
int t;
t = solve(s, '2', '5');
// cout << t << endl;
if(t != -1){
ans = min(ans, t);
}
t = solve(s, '5', '0'); // cout << t << endl;
if(t != -1){
ans = min(ans, t);
}
t = solve(s, '7', '5'); // cout << t << endl;
if(t != -1){
ans = min(ans, t);
}
t = solve(s, '0', '0'); // cout << t << endl;
if(t != -1){
ans = min(ans, t);
}
if(ans == INT_MAX) ans = -1;
cout << ans << endl;
}
return 0;
}

Codeforces 988E. Divisibility by 25的更多相关文章

  1. CF 988E Divisibility by 25 思维 第十二

    Divisibility by 25 time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

    传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...

  3. Codeforces Round #486 (Div. 3) E. Divisibility by 25

    Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  4. Divisibility by 25 CodeForces - 988E (技巧的暴力)

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  5. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  6. Divisibility by 25 CodeForces - 988E(模拟)

    遇见模拟题 有两种做法 例如这题: 1.直接去算次数(统计哪个数在第几位,然后去运算) 2.模拟操作 贴一个别人的代码...https://blog.csdn.net/weixin_39453270/ ...

  7. Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】

     Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. [math] Codeforces 597A Divisibility

    题目:http://codeforces.com/problemset/problem/597/A Divisibility time limit per test 1 second memory l ...

  9. codeforces 630J Divisibility

    J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...

随机推荐

  1. Core篇——初探Core的Http请求管道&&Middleware

    目录: 1.Core 处理HTTP请求流程 2.中间件(Middleware)&&处理流程 3.创建自定义中间件&&模拟Core的请求管道 Core 处理HTTP请求流 ...

  2. 快速新建一个纯净的java pom项目 project

    前期的java环境安装就不再阐述了使用步骤java -jar project-creator-0.1.jar projectName [war] 1> 比如你要创建一个项目名字叫 smile-o ...

  3. 路飞学城Python-Day33

    1.简述计算机操作系统中的“中断”的作用? 为什么有中断? 现代操作系统一般都是采用基于时间片的优先级调度算法,把CPU的时间划分为很细粒度的时间片,一个任务每次只能时间这么多的时间,时间到了就必须交 ...

  4. Day01-04学习内容总结

    学习内容小结 1.什么是编程,编程有什么用,什么是编程语言 2.计算的组成原理及组成部分 3.机械硬盘的工作原理 4.什么是操作系统,操作系统做了什么,为什么要有操作系统,操作系统有什么用 5.应用程 ...

  5. [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 3 线性代数初步

    向量和矩阵 什么是矩阵/向量? Vectors and matrix are just collections of ordered numbers that represent something: ...

  6. ios 人魔七七

    http://www.cnblogs.com/qiqibo/category/533488.html

  7. Python-基础-day5

    1.内置函数 2.文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 文件句柄 = file('文件路径', '模式') 注:python中打开文件有两种方式,即:open ...

  8. 【Codeforces Round #482 (Div. 2) C】Kuro and Walking Route

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把x..y这条路径上的点标记一下. 然后从x开始dfs,要求不能走到那些标记过的点上.记录节点个数为cnt1(包括x) 然后从y开始 ...

  9. 【hihocoder 1519】 逃离迷宫II

    [题目链接]:http://hihocoder.com/problemset/problem/1519?sid=1098756 [题意] Chinese [题解] bfs题; 根据bfs的性质; 第一 ...

  10. Mysql学习总结(30)——MySQL 索引详解大全

    什么是索引? 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类 ...