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;
}

最新文章

  1. Sass用法指南
  2. [Unity] Android插件
  3. js删除数据的几种方法
  4. Stack around the variable &#39;szStr&#39; was corrupted.
  5. FZU 1025 状压dp 摆砖块
  6. web文件上传的实现
  7. Codeforce Round #228 Div2
  8. xshell 远程连接Linux
  9. 换行符以及for循环的优化
  10. 201521123088 《Java程序设计》第14周学习总结
  11. Python_正则表达式二
  12. Codeforces1036F Relatively Prime Powers 【容斥原理】
  13. Vue Router学习笔记
  14. 论文阅读笔记一:Tutorial on Gabor Filters
  15. jenkins(8): 实战jenkins+gitlab持续集成发布php项目(代码不需要编译)
  16. 基于AT89C51单片机烟雾传感器
  17. js 函数问题
  18. laravel框架入门
  19. 浅析vue的双向数据绑定
  20. DevExpress换肤

热门文章

  1. Luogu P4118 [Ynoi2016]炸脖龙I
  2. 多进程-Pool进程池
  3. centos配置mutt和msmtp发送邮件
  4. (转)Ubuntu换源方法
  5. Ubuntu与centos的区别小用法
  6. linux库(程序)与包名联系
  7. Linux进程管理工具之ps
  8. CISCO运维记录之4507设备升级IOS(Version 03.03.02.SG版本存在bug)
  9. 958. Check Completeness of a Binary Tree
  10. oracle 环境变量问题