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 ...
随机推荐
- JQuery学习系列篇(一)
jQuery是一套Javascript脚本库:注意jQuery是脚本库, 而不是脚本框架. "库"不等于"框架", 比如"System程序集" ...
- 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...
- IOS - IOS之同步请求、异步请求、GET请求、POST请求(转载)
转载:http://www.open-open.com/lib/view/open1355055986679.html 1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务 ...
- NuSOAP笔记:如何创建复杂数据类型
PHP已经有了内置的SOAP扩展,但是它不具备自动生成WSDL的能力,所以很多时候,NuSOAP还是有一定诱惑力的. 在应用稍微复杂点的时候,单靠integer, string等简单数据类型是不能满足 ...
- BZOJ 2402 陶陶的难题II (01分数规划+树剖+线段树+凸包+二分)
题目大意:略 一定范围内求最大值,考虑二分答案 设现在选择的答案是$mid$,$max \left \{ \frac{yi+qj}{xi+pj} \right \} \geq mid $ 展开可得,$ ...
- vivo输入法 需求分析
我使用的输入法:vivo输入法 1,用户界面:界面为白色和灰色,整体简洁大方,个人而言外观挺不错.但是不能自定义界面,更改背景图片或是主题. 2,记住用户选择:可记忆上次使用后的键盘方式(26键或是9 ...
- 【转】python 关键字
转自:http://www.cnblogs.com/hongten/p/hongten_python_keywords.html python3.3.2中的关键字如下: The following i ...
- 【codeforces 235B】Let's Play Osu!
[题目链接]:http://codeforces.com/problemset/problem/235/B [题意] 让你玩一个游戏,游戏结果由一个长度为n的01字符组成; 这个结果的分数与连续的1的 ...
- ajax的两种使用方式
一.Ajax概述 1.什么是同步,什么是异步 同步现象:客户端发送请求到服务器端,当服务器返回响应之前,客户端都处于等待 卡死状态 异步现象:客户端发送请求到服务器端,无论服务器是否返回响应,客户端都 ...
- UVALive 5545 Glass Beads
Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...