哈工大 专业课 复试 2016机试附加题(作为统一回复)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Write By 木木
原本没觉得会有多少人会看附加题,毕竟大佬不需要,跨考要了也没用。
但是事实是很多私聊我要附加题,由于确实没去AC,就索性没有回各位。
现在有时间了,把附加题AC了一下,放在blog上,有人反映404,应该是审核还没通过所以写了这个文档作为统一回复。
最后一点,求求各位女装大佬或女大佬别加我好友,问问题@我或者临时会话就行了。
上次那个谁谁谁就不点名了(说话好好说,别嗲嗲的),搞得我回去就是跪键盘。。。宝宝心里苦啊。。。
试卷:2016-HITCS
附加改错题(20分)
1.(10分)下面程序的功能是:从键盘读入某门课程的成绩,然后根据输入选项将其排序,输入1则按升序排序,输入2则降序排序;最后输出排序结果。下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。
#include
#include
#define N 20
int ReadScore(int score[]);
void PrintScore(int score[], int n);
void SelectionSort(int score[], int n, int (*compare)(int a, int b));
void Swap(int *x, int *y);
int Ascending(int a, int b);
int Descending(int a, int b);
int main()
{
int score[N],n;
int order;
n = ReadScore(score);
printf("Total students are %d\n",n);
printf("Enter 1 to sort in ascending order\n");
printf("Enter 2 to sort in descending order\n");
scanf("%d",&order);
if(order == 1)
{
SelectionSort(score, n, Ascending);
printf("Data items in asending order\n");
}
else
{
SelectionSort(score, n, Descending);
printf("Data items in descending order\n");
}
PrintScore(score, n);
return 0;
}
/*读入成绩以负数结束,返回实际输入的成绩个数*/
int ReadScore(int score[])
{
int i=-1;
printf("Input score:\n");
do
{
i++;
scanf("%d",score[i]);
}while (score[i]>=0);
return i;
}
void PrintScore(int score[], int n)
{
int i;
for(i=0; i printf("%4d",score[i]); printf("\n"); } void SelectionSort(int score[], int n, int *compare(int a, int b)) { int i, j, k; for(i=0; i { k = i; for(j = i+1; j< n; j++) { if((*compare)(score[i],score[k])) { k = j; } } if(k!= i) { Swap(score[i],score[k]); } } } int Ascending(int a, int b) { return a < b; } int Descending(int a, int b) { return a > b; } void Swap(int *x, int *y) { int * temp; *temp = *x; *x = *y; *y = *temp; } 注意: (1)请将修改正确后的完整源程序拷贝粘贴到答题区内。 (2)对于没有错误的语句,请不要修改。 (3)当且仅当错误全部改正,且程序运行结果调试正确,才给加10分,部分正确不得分。(4)改错时不能改变程序原有的意图,也不要改变代码的输入输出格式。 运行示例: Input score: 98 97 88 78 99 57 -1 Total students are 6 Enter 1 to sort in ascending order Enter 2 to sort in descending order 2 Data items in descending order 99 98 97 88 78 57 1 格式化代码运行 修改的地方都使用红色标记了 #include #include #define N 20 int ReadScore(int score[]); void PrintScore(int score[], int n); void SelectionSort(int score[], int n, int (*compare)(int a, int b));