解题思路:

  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. 算法入门经典-第七章 例题7-4-1 拓展 n皇后问题 回溯法

    实际上回溯法有暴力破解的意思在里面,解决一个问题,一路走到底,路无法通,返回寻找另   一条路. 回溯法可以解决很多的问题,如:N皇后问题和迷宫问题. 一.概念 回溯算法实际类似枚举的搜索尝试过程,主 ...

  2. 在 Ubuntu 15.04 上安装 Android Studio(极其简单)

    sudo apt-add-repository ppa:paolorotolo/android-studio sudo apt-get update sudo apt-get install andr ...

  3. js数组及数组对象的遍历

    一 数组遍历 方法一:for循环 方法二:forEach遍历 forEach遍历数组 性能低于for循环,且不可使用break中断循环,也不能使用return返回外层函数 arr.forEach(fu ...

  4. JavaScript DOM编程艺术(第2版)学习笔记2(4~6章应用实例)

    本书的第4章使用第3章学到的操作DOM的方法和属性写了一个展示图片的网页,并在第5,6章对代码进行了优化. 第一版,搭建网页的静态结构,包括一级标题<h1>,无序列表清单<ul> ...

  5. jQuery应用实例2:简单动画

    效果: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  6. UNP学习笔记4——I/O复用:select和poll函数

    1 概述 之间的学习中发现,传统的阻塞式系统调用不仅浪费进程运行时间,而且会带来狠毒问题.因此进程需要有一种预先告知内核的能力,使得内核一旦发现进程指定的一个或者多个I/O条件就绪,它就通知进程.这个 ...

  7. ZBrush破解版真的好用么?

    安装ZBrush®的时候是不是经常出现各种奇葩问题,使用ZBrush时候是不是经常出现停止工作状况,究其原因,原来都是破解搞的鬼.ZBrush破解版你还敢用么? 随着国人对版权的重视,越来越多的制作商 ...

  8. Django中ORM之创建模型

    ORM 数据库与ORM映射关系 表名 --- 类名 字段 --- 属性 表记录 --- 类示例对象 创建表(建立模型) 模型建立如下 class Book(models.Model): title = ...

  9. day05-2 变量、常量、注释以及内存管理

    目录 什么是变量 Python中定义变量 定义变量名的命名规范 什么是常量 定义常量 注释是什么 注释有什么用 内存管理(重要) 引用计数 垃圾回收机制 小整数池 定义变量的三个特征 什么是变量 变量 ...

  10. Nginx虚拟主机以及自动启动脚本详解

    想要部署Nginx虚拟主机,那么首先需要nginx的环境,那么我们一起来看一下吧systemctl stop firewalldiptables -Fsetenforce 01)安装支持软件yum - ...