返回最大天数(中兴机试-9.08)
来源:互联网 发布:嘉实优化红利基金净值 编辑:程序博客网 时间:2024/06/07 13:10
题目描述
A在N个城镇(编号从1到N)做生意,每天他到一个城镇售货,任意连续两天选择不同的城镇。某一城镇i最多可选择ci次。
输入
参数1:表示城镇的整数num(N)
参数2:每个城镇可选择次数的正数列表countTown(c)
输出
返回A可以照此执行的最大天数的正数
Input
4 3
[2,2,2,2] [7,2,3]
Output
8 11
分析
设置数组中的第一个数为temp,并定义count=0。如果大于第二个数,那么让第二个数乘2作为count的返回值,接下来第一个数减去第二个数并判断是否大于第三个数,重复上述步骤,直到遍历到最后一个数。如果遍历到最后,temp仍大于0 ,那么count继续加1,否则直接输出count。
代码
#include<stdio.h>
int MaxNum(int num, int* countTown)
{
int i;
int count = 0;
int temp = countTown[0];
for (i = 1; i < num; i++)
{
if (temp == 0)
{
temp = countTown[i];
continue;
}
if (temp >= countTown[i])
{
count = count + countTown[i] * 2;
temp = temp - countTown[i];
}
else if (temp < countTown[i])
{
count = count + temp * 2;
temp = countTown[i] - temp;
}
}
if (temp > 0)
{
count++;
}
return count;
}
int main()
{
int num = 4;
int arr[num] = {2,2,2,2};
int* countTown = arr;
int max = MaxNum(num, countTown);
printf("%d", max);
return 0;
}
- 返回最大天数(中兴机试-9.08)
- 获取本月最大天数
- PHP获取某个月最大天数(最后一天)
- java获取当月最大天数
- JS获取当月最大天数
- 根据年,月返回天数
- 如何取得一个月的最大天数和最小天数
- 中兴
- 中兴
- vb 已经知道年和月,如何得到该月最大天数(最后一天)
- 怎样得到某年某月的最大天数
- 如何获取某年某月的最大天数
- 得到每个月的最大天数
- Perl 获取上月下月和最大天数
- js获得当前年月最大天数
- 获取当前月份的最大天数
- java 获取某年某月的最大天数
- Java取得指定年月的最大天数
- Caused by: java.lang.NoClassDefFoundError
- Python基础之文件读写和列表字典使用 ——《侠客行》文本分析
- vue事件修饰符
- Oracle--ORA-01012问题
- fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
- 返回最大天数(中兴机试-9.08)
- (Basic algorithm学习笔记)《基础算法三》- 枚举
- Android中对RecyclerView的帮助类Snaphelper使用
- 解决win10系统命令提示符添加路由时提示请求的操作需要提升问题
- Hadoop-2.7.2集群的搭建——集群学习日记
- STL nth_element神器
- UML使用2
- IplImage和cv::Mat间的转换
- Redis 宝典 | 基础、高级特性与性能调优