题意就是找出一个长度为k的整数,使得它的root为d,k的可能取值为1-1000。

第一眼看到这个题,无从下手,想到那么长的数,暴力肯定超时。其实不然,题目要求只要输出任何一个满足条件的即可,因为任何数的root都是0-9,所以这样的数还是很多的,算一下枚举次数的期望,大概就是5,不知道算的对不对。

//cf 355A
//2013-10-15-10.48
#include <stdio.h>
#include <string.h> int num[1005];
int k, d;
int tot; int getroot(int x)
{
if (x < 10)
return x;
int sum = 0;
while (x)
{
sum += (x%10);
x /= 10;
}
return getroot(sum);
} void print()
{
for (int i = 1; i <= k; i++)
printf("%d", num[i]);
puts("");
} void add(int deep)
{ if (getroot(tot) == d)
{
print();
return ;
}
else
{
if (num[deep] < 9)
{
tot += 1;
num[deep] += 1;
add(deep);
}
else
add(deep+1);
}
} int main()
{
while (scanf("%d %d", &k, &d) != EOF)
{
if (k == 1)
{
printf("%d\n", d);
continue;
}
if (d == 0 && k > 1)
{
puts("No solution");
continue;
}
for (int i = 1; i <= k; i++)
num[i] = 1;
tot = k;
add(1);
}
return 0;
}

最新文章

  1. Win8.1安装Visual Studio 2015提示需要KB2919355
  2. ExceptionLess新玩法 — 记日志
  3. 字符编码-UNICODE,GBK,UTF-8区别【转转】
  4. HDU 4608 I-number 2013 Multi-University Training Contest 1
  5. UIKit,Core Data , Core Graphics, Core Animation,和OpenGLES框架
  6. 现代程序设计——homework-06
  7. JQuery事件的绑定
  8. spring core源码解读之ASM4用户手册翻译之一asm简介
  9. 常用433MHZ无线芯片性能对比表分享
  10. Redis 缓存 + Spring 的集成示例(转)
  11. 如此高效通用的分页存储过程是带有sql注入漏洞的
  12. 【Android Developers Training】 11. 支持不同语言
  13. LINUX PID 1和SYSTEMD 专题
  14. canvas动画效果新年祝福话语
  15. 文本分类学习 (八)SVM 入门之线性分类器
  16. ubuntu安装nodejs,npm live-server
  17. 基于socketserver实现并发
  18. 两个List循环
  19. spring与mybatis五种整合方法
  20. Effective C++ .14 智能指针的拷贝与deleter函数

热门文章

  1. 【转载】java8中的Calendar日期对象(LocalDateTime)
  2. (持续更新)Qt3D 学习资源
  3. feign服务端出异常客户端处理的方法
  4. 微信小程序全局变量改变监听
  5. 通过字节码分析java中的switch语句
  6. Linux 终端连接工具 XShell v6.0.01 企业便携版
  7. 磁盘大保健 保持你的Linux服务器存储健康
  8. 和朱晔一起复习Java并发(二):队列
  9. stixel_world+Multi_stioxel_world+semantic_stixel_world知识拓展
  10. C#中对EXCEL保存的SAVEAS方法说明