Greatest Number

Greatest Number

Time Limit: 1000MS Memory limit: 65536K

题目描述

Saya likes math, because she think math can make her cleverer.
One day, Kudo invited a very simple game:
Given N integers, then the players choose no more than four integers from them (can be repeated) and add them together. Finally, the one whose sum is the largest wins the game. It seems very simple, but there is one more condition: the sum shouldn’t larger than a number M.
Saya is very interest in this game. She says that since the number of integers is finite, we can enumerate all the selecting and find the largest sum. Saya calls the largest sum Greatest Number (GN). After reflecting for a while, Saya declares that she found the GN and shows her answer.
Kudo wants to know whether Saya’s answer is the best, so she comes to you for help.
Can you help her to compute the GN?

输入

The input consists of several test cases.
The first line of input in each test case contains two integers N (0<N≤1000) and M(0
 1000000000), which represent the number of integers and the upper bound.
Each of the next N lines contains the integers. (Not larger than 1000000000)
The last case is followed by a line containing two zeros.

输出

For each case, print the case number (1, 2 …) and the GN.
Your output format should imitate the sample output. Print a blank line after each test case.

示例输入

2 10
100
2

0 0

示例输出

Case 1: 8

提示

 给出n个数,从中找到4个或4个以内的数相加的和接近m(可以重复)
定义两个相同的数组,第一部分 a[0] = 0 ; 第二部分n个数,第三部分n个数任意两个相加的和,然后遍历其中一个,用二分的办法从第二个中找出和最接近m的 (保留和),最后得到最大的和

来源

 2010年山东省第一届ACM大学生程序设计竞赛

示例程序
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <algorithm>  
  4. using namespace std;  
  5. int a[2000001];  
  6. int cmp(int x, int y)  
  7. {  
  8.     return x < y;  
  9. }  
  10. int main()  
  11. {  
  12.     int n, m, i, j, mid, l, h, k, max1, x, num=0, g, r, z;  
  13.     while(scanf("%d%d",&n,&m)!=EOF&&n)  
  14.     {  
  15.         k=0;  
  16.         num++;  
  17.         a[0]=0;  
  18.         for(i=1; i<=n; i++)  
  19.         {  
  20.             scanf("%d",&a[i]);  
  21.             {  
  22.                 if(a[i]>m)  
  23.                 {  
  24.                     i--;  
  25.                     n--;  
  26.                 }  
  27.             }  
  28.         }  
  29.         k=n+1;  
  30.         for(i=1; i<=n; i++)  
  31.         {  
  32.             for(j=i; j<=n; j++)  
  33.             {  
  34.                 if(a[i]+a[j]<=m)  
  35.                 {  
  36.                     a[k++]=a[i]+a[j];  
  37.                 }  
  38.             }  
  39.         }  
  40.         sort(a,a+k,cmp);  
  41.         r=k-1;  
  42.         g=k-1;  
  43.         max1=0;  
  44.         for(i=0; i<k; i++)  
  45.         {  
  46.             z=0;  
  47.             l=i;  
  48.             while(l<=r)  
  49.             {  
  50.                 mid=(l+r)/2;  
  51.                 x=a[i]+a[mid];  
  52.                 if(x>m)  
  53.                     r=mid-1;  
  54.                 else if(x==m)  
  55.                 {  
  56.                     max1=m;  
  57.                     z=1;  
  58.                     break;  
  59.                 }  
  60.                 else  
  61.                 {  
  62.                     l=mid+1;  
  63.                     if(max1<x)  
  64.                     {  
  65.                         g=mid;  
  66.                         max1=x;  
  67.                     }  
  68.                 }  
  69.             }  
  70.             if(z)  
  71.                 break;  
  72.             r=g;  
  73.         }  
  74.         printf("Case %d: %d\n\n",num, max1);  
  75.     }  
  76.     return 0;  
  77. }  

内容概要:《2024年中国城市低空经济发展指数报告》由36氪研究院发布,指出低空经济作为新质生产力的代表,已成为中国经济新的增长点。报告从发展环境、资金投入、创新能力、基础支撑和发展成效五个维度构建了综合指数评价体系,评估了全国重点城市的低空经济发展状况。北京和深圳在总指数中名列前茅,分别以91.26和84.53的得分领先,展现出强大的资金投入、创新能力和基础支撑。低空经济主要涉及无人机、eVTOL(电动垂直起降飞行器)和直升机等产品,广泛应用于农业、物流、交通、应急救援等领域。政策支持、市场需求和技术进步共同推动了低空经济的快速发展,预计到2026年市场规模将突破万亿元。 适用人群:对低空经济发展感兴趣的政策制定者、投资者、企业和研究人员。 使用场景及目标:①了解低空经济的定义、分类和发展驱动力;②掌握低空经济的主要应用场景和市场规模预测;③评估各城市在低空经济发展中的表现和潜力;④为政策制定、投资决策和企业发展提供参考依据。 其他说明:报告强调了政策监管、产业生态建设和区域融合错位的重要性,提出了加强法律法规建设、人才储备和基础设施建设等建议。低空经济正加速向网络化、智能化、规模化和集聚化方向发展,各地应找准自身比较优势,实现差异化发展。
### MySQL 和 Oracle 中 `GREATEST()` 函数处理 NULL 值的方法 #### MySQL 中 `GREATEST()` 函数与 NULL 值 当 `GREATEST()` 函数接收到任何参数为 `NULL` 时,该函数会返回 `NULL`[^2]。这意味着即使其他参数中有有效数值,只要存在一个 `NULL` 参数,整个表达式的计算结果即为 `NULL`。 为了应对这种情况,在实际开发过程中通常采用两种策略: 1. **使用 COALESCE() 函数** 可以通过嵌套的方式先用 `COALESCE()` 将可能存在的 `NULL` 替换成合理的默认值再传递给 `GREATEST()` 进行比较操作。 ```sql SELECT GREATEST(COALESCE(column_a, 0), COALESCE(column_b, 0)) FROM table_name; ``` 2. **IFNULL 或 IF 表达式** 对于某些特定版本的数据库管理系统来说,还可以考虑利用条件判断语句如 `IFNULL()` 来实现相似的效果。 ```sql SELECT GREATEST(IFNULL(column_a, 0), IFNULL(column_b, 0)) FROM table_name; ``` #### Oracle 数据库中 `GREATEST()` 函数与 NULL 值 Oracle 的行为与此类似,当输入列表中含有 `NULL` 时也会导致最终的结果变成 `NULL`[^3]。因此同样推荐采取预防措施来规避此问题的发生。 一种常见的做法是在调用之前对潜在为空的数据项做预处理,比如设置缺省值或者跳过那些不确定是否有效的记录。 ```plsql DECLARE v_max_value NUMBER := GREATEST( NVL(value_1, 0), NVL(value_2, 0), ... ); BEGIN -- 继续执行后续逻辑... END; ``` 这里使用了 PL/SQL 特有的 `NVL()` 函数作为替代方案之一,它可以有效地防止因 `NULL` 而引起的错误情况发生。 综上所述,无论是哪种类型的 SQL 数据库环境里,面对 `GREATEST()` 面临到 `NULL` 输入的情形下都应该提前做好充分准备并合理运用辅助工具来进行妥善管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值