December 4 2006

 int scandir(const char *dir, struct dirent ***namelist,
 int(*filter) (const struct dirent *),
 int(*compar) (const struct dirent **, const struct dirent **));

  There is the prototype of scandir().  The scandir() function scans the directory "dir",
calling "filter()" on each directory entry.  Entries for which "filter()" return non-zero
are stored in strings allocated via malloc(), stored using qsort() with the comparison
function "compar()", and collected in array "namelist" which is allocated via malloc().
If "filter" is NULL, all entries are selected.

  The alphasort() and versionsort() functions can be used as the comparison function "compar()".
The former sorts directory entries using "strcoll", the latter using "strverscmp" on the
strings "(*a)->d_name" and "(*d)->d_name".

  I wrote a example to test this function.  The code is following.

#include <dirent.h>

#include <stdio.h>

 

int selectAll(const struct dirent * d){

 if (strcmp(d->d_name, ".")!=0 && strcmp(d->d_name, "..")!=0){

  fprintf(stderr, "The directory entry [ %s ] is selected./n",

    d->d_name);

  return 1;

 }else

  return 0;

}

 

int main(int argc, char * argv[]){

 struct dirent **namelist;

 int n;

 

 n = scandir(argv[1], &namelist, selectAll, alphasort);

 if (n<0)

  perror("scandir");

 else{

  while(n--){

   printf("%s/n", namelist[n]->d_name);

   free(namelist[n]);

  }

  free(namelist);

 }


 return 0;

}

  From the above example, we can see the "scandir()" is a high order functor.  There are two
functions can be plugged into the "scandir()" inside.  Do you remember that (map proc ...) in
scheme.  When I began to study programming algorithm in C, I had seen the technique.  How wonderful
was I at that time!  However, after learning lambda calculus, I known "the technique" is just
a rule in lambda calculus, called beta substitutes.  Because scheme is a implementation of
lambda calculus, it is so natural to pass a function as parameter into another function.  That
is its nature.  But it is not the nature of C language in that C is a programming language which
need compile.  In other words, its runtime environment is not full dynamic, some information
were changed and fixed into the binary program when product out binary codes.  So, C programmer
has only way to pass a function pointer as parameter into another function.

### 回答1: USACO 2021年12月是美国计算机奥林匹克竞赛的一次比赛,是为了选拔美国高中生中最优秀的计算机科学家而举办的。比赛包括四个不同难度级别的编程题目,参赛者需要在规定时间内完成这些题目。这次比赛的题目难度较高,需要参赛者具备较强的编程能力和算法思维能力。 ### 回答2: usaco是一个很受欢迎的美国高中生和大学生参加的计算机竞赛,每年会有四场不同的赛事,分为铜组、银组、金组和白银组等4个不同级别。2021年12月的usaco比赛题目难度中等,共4道算法题,主要涉及搜索算法、贪心算法、动态规划和图论算法等方面内容。 题目一是“Convenience Store”,给定一个城市地图,其中包含n个建筑,每个建筑位置有xy坐标,建筑之间可能有道路连通,每个建筑中包含一个便利店和售货员,售货员需要把货物直接送到顾客的门口并统计物资,问售货员需要访问哪些建筑才能完成任务。 题目二是“Longest Path Game”,给定一个有向无环图和起点和终点,每次可以走向图中指向该点的其他点,每走一步需要支付一定的代价,问从起点到终点最少需要支付多少代价并给出最少花费路径。 题目三是“Escape”,给定一个大小为n*m的迷宫,其中包含空地和只能通过特殊方式通过的障碍物,新增一个道具可以消除障碍物,在指定时间内到达终点即可获胜,问是否存在可行解。 题目四是“New Year Travel”,给定一个n个城市构成的圆环和m条单向道路,初次n个城市顺时针排列,经过m次之后重排该圆环,问是否存在一条路径可以在经过圆环的最小路程的前提下遍历所有的城市。 ### 回答3: USACO 2021 12月是一次由美国计算机科学奥林匹克联赛(USACO)组织的编程竞赛。USACO是美国高中生最具影响力的计算机竞赛之一,每年分为四个季度,包含铜组、银组、金组和白金组,涵盖了算法设计、数据结构、图论、搜索、动态规划、计算几何、图像处理等多个领域和真实场景。 本场比赛共有铜组、银组和金组三个组别,其中铜组和银组为在线比赛,金组为24小时比赛。比赛难度较高且时间较紧,需要选手在有限时间内完成一系列复杂的程序设计题目。在比赛期间,选手需要具备良好的分析问题、设计算法和编写程序的能力,同时还需要有过硬的数学基础、英语阅读理解能力和编程实践经验。 对于铜组选手来说,需要能够熟练使用基本算法和数据结构来解决题目,如模拟、暴力搜索、递推等;对于银组选手,则需要有更深入的算法思考和程序实现能力,如分治、贪心、二分、动态规划等;而针对金组选手,需要更高的算法挑战和程序优化能力,如图论、网络流、计算几何等。 此次USACO 2021 12月比赛题目难度较大,需要选手们具备扎实的算法基础和良好的程序设计习惯。要想在比赛中取得好成绩,需要选手们克服紧张心理,在比赛前加强对算法知识和代码实践的学习,时刻保持冷静,清晰思路,在有限时间内充分展现自己的编程才能和思维能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值