zoukankan      html  css  js  c++  java
  • 一道面试题

    有一个正整数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)+"种");
      }
    }

    }
    }

  • 相关阅读:
    [Leetcode]-- Median of Two Sorted Arrays
    Implement strStr()
    [Leetcode]-- Remove Element
    3Sum
    推荐一个跨平台内存分配器
    ACE的缺陷
    详谈高性能UDP服务器的开发
    vi查找替换命令详解
    gcc多版本切换
    Centos 5.5升级Python到2.7版本
  • 原文地址:https://www.cnblogs.com/cnwgy/p/3455646.html
Copyright © 2011-2022 走看看