解题思路:

  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. Win32 CRT and MFC 清单文件.manifest配制

    Demo.exe.manifest <?xml version="1.0" encoding="UTF-8" standalone="yes&q ...

  2. CSS实现栅格布局

    CSS实现栅格布局 设置容器container: .grid-container { width: 100%; max-width: 1200px; } 清除浮动: .row:before, .row ...

  3. PHP函数十进制、二进制、八进制和十六进制转换函数说明

    1.十进制转二进制 decbin() 函数,如下实例  echo decbin(12); //输出 1100 echo decbin(26); //输出 11010 2.十进制转八进制 decoct( ...

  4. Photoshop保存文件时的选项

    以 JPEG 格式存储 您可以使用"存储为"命令以 JPEG (*.jpg) 格式存储 CMYK.RGB 和灰度图像.JPEG 通过有选择地扔掉数据来压缩文件大小.也可以使用&qu ...

  5. Springboot统一异常处理(@ControllerAdvice)

    import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind ...

  6. echart纵坐标标签特别长换行显示

    纵坐标 yAxis : [ { type : 'category', data : name, axisLabel: { //坐标轴刻度标签的相关设置. textStyle: { color: '#0 ...

  7. 标题:u-boot 移植步骤详解

    1 U-Boot简介U-Boot,全称Universal Boot Loader,是遵循GPL条款的开放源码项目.从FADSROM.8xxROM.PPCBOOT逐步发展演化而来.其源码目录.编译形式与 ...

  8. STM32 Cubemx 配置定时器定时1mS

    最近才发现原来我把定时器里的配置参数代表的意义给搞混了,这里记录一下,防止以后自己忘记. 以建立一个定时1mS定时器为例: 1.先打开定时器 2.配置好时钟 3.配置定时器设置 重点来了,以前在这里我 ...

  9. Winserver服务器-AD字段对照简图

    AD字段对照简图

  10. Linux多线程实践(四 )线程的特定数据

    在单线程程序中.我们常常要用到"全局变量"以实现多个函数间共享数据, 然而在多线程环境下.因为数据空间是共享的.因此全局变量也为全部线程所共同拥有.但有时应用程序设计中有必要提供线 ...