You Are Given a Decimal String...
2024-10-07 02:40:15
B. You Are Given a Decimal String...
这个题需要求出从某一个尾数 n 变为 m 所需要的 x 和 y 的最小个数(i+j) 那么就需要预处理出一个数组来存放这个值。数组 b[ ] 中存的是所需要多添加的数的个数 (i+j-1)
int b[10];
for (int i = 0; i < 10; ++i) b[i] = inf;
for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
{
if (i || j)
b[(i * x + j * y) % 10] = min(b[(i * x + j * y) % 10], i+j-1);
//i+j-1是需要多添加的数的个数
}
代码:
// Created by CAD on 2019/8/9.
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
for (int x = 0; x < 10; ++x)
for (int y = 0; y < 10; ++y)
{
int b[10];
for (int i = 0; i < 10; ++i) b[i] = inf;
for (int i = 0; i < 10; ++i)
for (int j = 0; j < 10; ++j)
{
if (i || j)
b[(i * x + j * y) % 10] = min(b[(i * x + j * y) % 10], i + j - 1);
}
int ans = 0;
for (int i = 0; s[i + 1] != '\0'; ++i)
if (b[(s[i + 1] - s[i] + 10) % 10] == inf)
{
ans = -1;
break;
} else ans += b[(s[i + 1] - s[i] + 10) % 10];
cout << ans;
if (y == 9) cout << endl;
else cout << " ";
}
return 0;
}
最新文章
- Sass用法指南
- [Unity] Android插件
- js删除数据的几种方法
- Stack around the variable &#39;szStr&#39; was corrupted.
- FZU 1025 状压dp 摆砖块
- web文件上传的实现
- Codeforce Round #228 Div2
- xshell 远程连接Linux
- 换行符以及for循环的优化
- 201521123088 《Java程序设计》第14周学习总结
- Python_正则表达式二
- Codeforces1036F Relatively Prime Powers 【容斥原理】
- Vue Router学习笔记
- 论文阅读笔记一:Tutorial on Gabor Filters
- jenkins(8): 实战jenkins+gitlab持续集成发布php项目(代码不需要编译)
- 基于AT89C51单片机烟雾传感器
- js 函数问题
- laravel框架入门
- 浅析vue的双向数据绑定
- DevExpress换肤