Codeforces 988E. Divisibility by 25
解题思路:
只有尾数为25,50,75,00的数才可能是25的倍数。
对字符串做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的更多相关文章
- CF 988E Divisibility by 25 思维 第十二
Divisibility by 25 time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类
传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for ...
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- 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 ...
- 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 ...
- Divisibility by 25 CodeForces - 988E(模拟)
遇见模拟题 有两种做法 例如这题: 1.直接去算次数(统计哪个数在第几位,然后去运算) 2.模拟操作 贴一个别人的代码...https://blog.csdn.net/weixin_39453270/ ...
- Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】
Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- [math] Codeforces 597A Divisibility
题目:http://codeforces.com/problemset/problem/597/A Divisibility time limit per test 1 second memory l ...
- codeforces 630J Divisibility
J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...
随机推荐
- ROS-Rviz-turtlebot3仿真信息查看
前言:Rviz是ROS自带的一种3D可视化工具. 一.安装turtlebot3功能包 1.1 安装依赖包 sudo apt-get install ros-kinetic-joy ros-kineti ...
- Ubuntu14.04下Mongodb数据库可视化工具安装部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb(离线安装方式|非apt-get)安装部署步骤(图文详解)(博主推荐) Ubuntu14.04下Mongodb官网安装部署步骤(图 ...
- SQL Server 分割字符串转列
CREATE FUNCTION dbo.sf_DS_SplitNVarchar ( @strValues nvarchar(4000) ) RETURNS @tblStrList TABLE (id ...
- linux下服务启动脚本
#!/usr/bin/env python# -*- coding: utf-8 -*-# @File : deployment.py# @Author: Anthony.waa# @Date : 2 ...
- make 编译 linux 内核是单线程的任务 才用-j4命令使用4 线程加速
今天使用 make 编译 linux 内核,发现CPU只用了30%多一点,而我的电脑是4核的,所以如果没有意外的话,make 编译 linux 内核的任务是用单线程做的. 又了解到,使用-j4参数可以 ...
- [NOIP2004提高组]虫食算
题目:洛谷P1092.codevs1064.Vijos P1099. 题目大意:给你一个$n$进制.每个数都是$n$位的三个数a,b,c,这些数的数位由字母表示(共$n$个字母,从‘A’开始),所有数 ...
- jQuery 简单介绍
jQuery 简单介绍 jQuery的定义 jQuery是一个快速,小巧,功能丰富的JavaScript库.它通过易于使用的API在大量浏览器中运行,使得 HTML文档遍历和操作,事件处理,动画 ...
- 小学生都能学会的python(生成器)
小学生都能学会的python(生成器) 1. 生成器 生成器的本质就是迭代器. 生成器由生成器函数来创建或者通过生成器表达式来创建 # def func(): # lst = [] # for i i ...
- CODEVS——T 1005 生日礼物
http://codevs.cn/problem/1005/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description ...
- leetCode(24):Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...