目录

题目

Input Specification:

Output Specification:

Sample Input:

Sample Output:

思路

代码


题目

"Forever number" is a positive integer A with K digits, satisfying the following constrains:

  • the sum of all the digits of A is m;
  • the sum of all the digits of A+1 is n; and
  • the greatest common divisor of m and n is a prime number which is greater than 2.

Now you are supposed to find these forever numbers.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤5). Then N lines follow, each gives a pair of K (3<K<10) and m (1<m<90), of which the meanings are given in the problem description.

Output Specification:

For each pair of K and m, first print in a line Case X, where X is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution.

Sample Input:

2
6 45
7 80

Sample Output:

Case 1
10 189999
10 279999
10 369999
10 459999
10 549999
10 639999
10 729999
10 819999
10 909999
Case 2
No Solution

思路

1. 解决英语问题

"the greatest common divisor" --- 最大公约数

"a prime number" --- 素数/质数

2. 符合条件的A的特点

A一定是以连续的9结尾的数,假设结尾有x个连续的9,那么A+1的各位和n就是m-x*9+1

所以对于x的取值可以在1~k-1之间遍历;

而当确定了x后,A中除了结尾连续的9之外的前面几位的确定,要靠枚举,而这个枚举可以依靠递归去实现;

递归的过程见代码

3. vector删除最后一个元素的函数

vec.pop_back();

代码

#include <iostream>
#include <cmath>
#include <vector>using namespace std;// 判断n是否是素数
bool isPrime(int n) {if(n==1) return false;for(int i=2;i<=sqrt(n);i++) {if(n%i==0) return false;} return true;
} // 判断x1和x2的最大公约数是否是大于2的质数
bool judge(int x1,int x2) {int i=(x1<=x2)?x1:x2;for(;i>=1;i--){if(x1%i==0&&x2%i==0) break;}// 判断i是否是大于2的素数 if(isPrime(i)&&i!=2) return true;else return false;
}int n,x;
int leftDigitNum,leftDigitSum;
bool flag;/* 递归实现枚举* leftDigitIndex---当前正在枚举的位index;* tempSum---leftDigitIndex前面几位的和;* tempLeftDigit---前面几位分别是哪些数字。
**/
void dg(int leftDigitIndex,int tempSum,vector<int> tempLeftDigit) {// 当前要枚举的位index超过了所需要枚举的位index时 if(leftDigitIndex>=leftDigitNum) return;// 确定枚举的范围 int left=(leftDigitIndex==0)?1:0;int right=(leftDigitIndex==leftDigitNum-1)?8:9;// 开始枚举 for(int i=left;i<=right;i++) {// 发现取i超过了所需要的位数和时 if(tempSum+i>leftDigitSum) return;else if(tempSum+i==leftDigitSum) {// 发现取i刚好等于所需要的位数和时,不用往后枚举了,后面未确定值的位都取0 flag=true;// 表示这个case是可以找到A的 tempLeftDigit.push_back(i);cout<<n<<" ";for(int digit:tempLeftDigit) cout<<digit;for(int j=0;j<leftDigitNum-leftDigitIndex-1;j++) cout<<0;for(int j=0;j<x;j++) cout<<9;cout<<endl;return;} else {// 发现取i小于所需要的位数和时,继续枚举后面几位 tempLeftDigit.push_back(i);dg(leftDigitIndex+1,tempSum+i,tempLeftDigit);tempLeftDigit.pop_back();}}
}int main(int argc, char** argv) { int N;cin>>N;for(int i=1;i<=N;i++) {cin.get();int k,m;// k-A的位数;m-A的各位数和cin>>k>>m;cout<<"Case "<<i<<endl;flag=false;// 标识当前case下是否存在这样的A for(x=k-1;x>=1;x--) {// x-A后面连续的9的个数n=m-9*x+1;// 判断m和n的最大公约数是否是大于2的质数if(!judge(m,n)) continue; leftDigitSum=m-9*x;// A除后面连续的9外的位数和 leftDigitNum=k-x;// A除后面连续的9外的位数个数 if(leftDigitSum<1||leftDigitSum>leftDigitNum*9-1) continue;// leftDigitNum位必须要能表示出leftDigitSum// 递归 vector<int> leftDigit;dg(0,0,leftDigit);}if(!flag) cout<<"No Solution"<<endl;}return 0;
}

【PTA Advanced】1160 Forever(C++)相关推荐

  1. 【图像分割应用】设备自动化(一)——自动驾驶

    这是专栏<图像分割应用>的第5篇文章,本专栏主要介绍图像分割在各个领域的应用.难点.技术要求等常见问题. 这是本专栏的第二个板块设备自动化的第1篇文章.通勤是我们日常生活的一个重要组成部分 ...

  2. 【图像分割应用】医学图像分割(三)——肿瘤分割

    这是专栏<图像分割应用>的第3篇文章,本专栏主要介绍图像分割在各个领域的应用.难点.技术要求等常见问题. 肿瘤的分割是医学图像分析领域的一个重要内容,相比较前面提到过的脑区域分割和心脏分割 ...

  3. 【图像分割应用】医学图像分割(二)——心脏分割

    这是专栏<图像分割应用>的第2篇文章,本专栏主要介绍图像分割在各个领域的应用.难点.技术要求等常见问题. 相比较脑区域分割,医学图像中的心脏分割问题要更复杂,因为心脏是一个不停运作的器官, ...

  4. 【计算机系统设计】实践笔记(2)数据通路构建:第一类R型指令分析(2)

    待办事项 时钟频率高,取指周期长,远大于执行周期,如何处理? 不可综合逻辑的处理 接上一篇 [计算机系统设计]实践笔记(2)数据通路构建:第一类R型指令分析(1) 8.2 ALU运算器 `timesc ...

  5. 【计算机系统设计】实践笔记(2)数据通路构建:第一类R型指令分析(1)

    0 回顾 上一次实践笔记(0)我们实现了一个最简单的,能够每个上升沿+4的PC. 我们最需要关注的就是器件功能的独立性,避免内外功能混杂,同时一定要注意脑中有电路(RTL级描述的抽象电路而不是实际的门 ...

  6. 【炼丹技巧】指数移动平均(EMA)【在一定程度上提高最终模型在测试数据上的表现(例如accuracy、FID、泛化能力...)】

    本文中心: 1.指数移动平均(Exponential Moving Average)EMA作用: ema不参与实际的训练过程,是用在测试过程的,相比对变量直接赋值而言,移动平均得到的值在图像上更加平缓 ...

  7. 【Vue2.0】— TodoList案例(十七)

    [Vue2.0]- TodoList案例(十七) <template><div id="root"><div class="todo-con ...

  8. 【Vue2.0】—props 配置(十三)

    [Vue2.0]-props 配置(十三) <template><div class="demo"><h1>{{ msg}}</h1> ...

  9. 【Vue2.0】—键盘事件(三)

    [Vue2.0]-键盘事件(三)

最新文章

  1. 揽胜 android auto,增新动力系统 新款路虎揽胜家族官图发布
  2. HTML基本介绍与操作
  3. TCP通信粘包问题分析和解决
  4. redux进一步优化
  5. 竞赛奇葩队名,学编程的人都是隐藏的段子手 | 今日最佳
  6. 单片机STM8S测量电压电路_单片机电路设计中的10个难点
  7. php查找存储引擎,php-如何找到MySQL临时表存储引擎
  8. ExtJS之Store
  9. 特斯拉CEO马斯克再卖4套房 挂牌价6250万美元
  10. semi-global matching 算法总结
  11. 20190825 On Java8 第十二章 集合
  12. python中%的用法_python中%的用法
  13. 数学篇--初中数学知识
  14. 容器内部使用docker命令时报错:缺少libcrypto.so.10
  15. 各种编程语言的适用范围
  16. 闭环系统的零极点图判定稳定性_零极点与系统稳定关系 拉氏变换的收敛域...
  17. JS日期、年月日、时分秒
  18. XML编程经验――LIBXML2库使用指南
  19. python模拟手写笔迹_原笔迹手写实现平滑和笔锋效果之:笔迹的平滑(一)
  20. 深入理解Golang之Map

热门文章

  1. VS2017 DLL中调用_beginthreadex创建线程
  2. matlab的许可证文件路径,网络许可证文件 - MATLAB Simulink - MathWorks 中国
  3. 7-24 树种统计 (25 分)
  4. 华为matebook13笔记本 右侧type-c接口 没反应,失灵 ,失效,不通电,无法识别usb设备,设备管理器Unknown usb device
  5. 挂载详解mount命令的使用
  6. c盘扩展卷选项是灰的怎么办?win10系统c盘扩展卷灰色
  7. Windows 扩展 C 盘扩展卷按钮显示灰色怎么办
  8. 如何快速解决Android Studio中的HAMX安装失败问题
  9. 《京韵大鼓——祭晴雯》(骆玉笙)(唱词文本)
  10. source tree各历史版本