欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > C练手题--Two Oldest Ages 【7 kyu】

C练手题--Two Oldest Ages 【7 kyu】

2025/12/15 12:30:10 来源:https://blog.csdn.net/HECHENNIU/article/details/141759118  浏览:    关键词:C练手题--Two Oldest Ages 【7 kyu】

一、原题

链接:Training on Two Oldest Ages | Codewars

The two oldest ages function/method needs to be completed. It should take an array of numbers as its argument and return the two highest numbers within the array. The returned value should be an array in the format [second oldest age, oldest age].

二、解题

1、分析

找出年龄最大的和年龄第二大的

2.思路

1)遍历数组,将年龄最大值存入max

2)遍历数组,逐个比较,将年龄大但与max不同的值,存入min

3)按要求将值存入results【results非函数内创建,函数调度完不会消失】

三、Myway

#include <stdlib.h>//result is an output buffer which has to be filled with the solution
void two_oldest_ages(size_t n, const int ages[n], int result[2]) {//  <----  hajime!int min=-100000,max=-100000;for(int i=0;i<n;i++){if(max<ages[i]) max=ages[i];}for(int i=0;i<n;i++){if(min<ages[i] & ages[i]!=max) min=ages[i];}result[0]=min;result[1]=max;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词