惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
A
About on SuperTechFans
G
Google Developers Blog
B
Blog RSS Feed
F
Fortinet All Blogs
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Jina AI
Jina AI
IT之家
IT之家
P
Proofpoint News Feed
美团技术团队
量子位
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog
有赞技术团队
有赞技术团队
U
Unit 42

博客园 - 王国营的博客

asp.net Handler中的IsReusable属性及在Handler中使用Session 关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理 多层循环的递归实现 约瑟夫环问题 不通过第三个变量实现两个整型变量的交换 将PHP文件生成静态文件源码 PHP Warning: date(): It is not safe to rely on the system's timezone settings EcShop 如何制作一张windows sp3启动安装盘iso? win7下xp mode IP在同一网段实现网络共享 sql server 2005附加数据库错误 无法打开物理文件 解决114啦源码(114la)不能生成地方房产和地方报刊问题4级页面0字节 菜鸟学PHP之Smarty入门(组图) 弹出一次“设为主页”和“加入收藏”代码 EMS SQL Manager中文显示乱码的解决方法 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 CSS和JS链接地址中带的问号是做什么的 IIS7/IIS7.5下轻松配置PHP利器(微软PHP Manager for IIS 7) iframe异步加载技术及性能 新开网址导航网站,欢迎各位朋友捧场
一道面试题
王国营的博客 · 2013-12-03 · via 博客园 - 王国营的博客

有一个正整数x,和一个正整数集y,要求从y中取固定的个数z (可以重复),相加之后的结果和为x?编程实现
假如x=15 ,y集合为{1,2,3,4,5,6,7,8,9}, z=5
求出满足所有的结果


1+2+3+4+5=15
1+1+3+4+6=15

public static void Main(String[] args) {
  target = 15;
  int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  int count = 2;
  getNumber(target, array, count,"");
}

private static void getNumber(int target, int[] array, int count,String perfix) {
  for (int i = 0; i < array.length; i++) {
  if (count > 1) {
    if (array[i] < target) {
      Console.WriteLine(perfix+array[i]+"+" );
      getNumber(target - array[i], array, count - 1,perfix+array[i]+"+");
    }
  } else {
  if (target == array[i]) {
  Console.Write(perfix+array[i] + "=");
  Console.WriteLine(TestItractor.target+"这是第"+(++TestItractor.count)+"种");
  }
}

}
}