ZCMU-1133- 第九章:致我们终将逝去的青春

1133: 第九章:致我们终将逝去的青春

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 18   Solved: 12
[ Submit][ Status][ Web Board]

Description

                      

/青 春 是 用 来 追 忆 的

/当 你 怀 揣 着 她 时

/她 一 文 不 值

/只 有 将 她 耗 尽 后

/再 回 过 头 看

/一 切 才 有 了 意 义

/爱 过 我 们 的 人 和 伤 害 过 我 们 的 人

/都 是 我 们 青 春 存 在 的 意 义

         

在ACM中找寻 青春的意义:

Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.

Input

The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1, ..., xn. If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x1, ..., xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.

Output

For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.

Sample Input

4 6 4 3 2 2 1 1
5 3 2 1 1
400 12
50 50 50 50 50 50 25 25 25 25 25 25
0 0

Sample Output

Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25

【解析】
题目的意思就是就是给出我们数字的总和n,再给你m个数,然后叫你输出这n个数能组合成这个数的方案的总和.其实
这道题就算能想到是深搜..不过也还是有点懵,所以此处参考的网上代码。这个地方怎么样可以不出现重复的方案数
就算当我们遇到在某一层搜索的时候遇到了同样的数字我们就跳过。具体看代码解析吧。
#include<iostream>
#include<cstdio>
#include<cstring>
int n,flag,m;
int a[10001],b[10001];
using namespace std;
void dfs(int deep,int sum,int length)
{
    int i;
    if(sum>n)
        return;//表示数字的和比规定的数字大了返回上一层
    else if(sum==n)
    {
        flag=1;
        for(i=0;i<length;i++)
        {
            if(i==0)
            printf("%d",b[i]);//输出方案
            else
                printf("+%d",b[i]);
        }
        printf("\n");
    }
    for(i=deep;i<m;i++)
    {
        b[length]=a[i];//把可以进行方案组合的数组一个一个的给它
        dfs(i+1,sum+a[i],length+1);//向下搜索,这里的总和是sum+a[i]是需要注意的
        while(a[i]==a[i+1])//遇到同样的数需要继续往下走
            i++;
    }
}
int main()
{
  int i;
  while(~scanf("%d",&n))
  {
      flag=0;
      memset(a,0,sizeof(a));
      memset(b,0,sizeof(b));
      scanf("%d",&m);
      if(n==0&&m==0)
        break;
      for(i=0;i<m;i++)
      {
          scanf("%d",&a[i]);
      }
      printf("Sums of %d:\n",n);
      dfs(0,0,0);
      if(flag==0)
      {
        printf("NONE\n");
      }

  }
return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: zcmu 1093 简单计算器是一道编程题目,要求实现一个简单的计算器,能够进行加、减、乘、除四种基本运算。该题目主要考察编程基础能力和算法思维能力,需要熟练掌握基本的运算符和控制语句,能够设计合理的算法实现计算器功能。 ### 回答2: zcmu 1093 简单计算器是一种基于计算机技术的工具,用于进行基本算术运算,如加减乘除等。它能够简化我们在日常生活中的计算工作,提高计算效率,减少出错率。 使用zcmu 1093 简单计算器非常简单,只需输入需要计算的数字和符号,就能够得到计算结果。它可以进行多个数字之间的复杂运算,同时还支持小数、百分数、平方根等复杂运算。另外,zcmu 1093 简单计算器还可以存储中间计算结果,方便我们进行多步计算或调整计算过程。 除了日常的计算工作,zcmu 1093 简单计算器还可用于科学计算、工程设计等领域。许多专业软件都是基于简单计算器原理设计的,它们具有更高的计算精度和更复杂的运算能力,能够支持更高级别的科学计算和技术分析。 总之,zcmu 1093 简单计算器在日常生活中有着广泛的应用,它使我们的计算工作变得更加高效、准确。并且,随着科技的不断发展,这种计算工具也在不断地更新和改进,为我们的计算工作提供更加便捷、多样化的选择。 ### 回答3: ZCMU 1093 简单计算器是一道基础的算法题目,需要实现一个简单的计算器程序,支持加、减、乘、除四种基本运算,可以对两个整数进行运算并输出结果。 要实现这道题目,首先需要根据输入的运算符来判断应该进行的运算类型,并根据运算符的不同,执行不同的计算操作。同时,应注意除数不能为零的情况,避免程序出现异常。 在编写程序的过程中,可以使用 switch case 语句来判断不同的运算类型,并执行相应的计算操作。同时,为了能有效地判断输入的运算符,可以使用输入字符串的方式进行处理,提取出运算符进行比较。 此外,在程序中还需要进行合法性判断,确保输入的数字均为整数且在合理的范围内,以避免程序运行出现异常情况。同时,还需要考虑输入格式的问题,应确保输入的数字和运算符符合题目要求。 综上所述,ZCMU 1093 简单计算器是一道基础的算法题目,需要实现一个简单的计算器程序,支持加、减、乘、除四种基本运算,注意程序的合法性判断和输入格式的处理,能够熟练地运用 switch case 等语句完成程序的编写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值