第五屆全國信息水平設計大賽C語言程序設計A卷答案
#include <stdio.h> #include <conio.h> /* 1、編程實現:有二維數組a[3][3]={{5.4,3.2,8},{6,4,3.3},{7,3,1.3}}, 將數組a的每一行元素均除以該行上的主對角元素(第1行同除以a[0][0], 第2行同除以a[1][1],...),按行輸出新數組。(20分) */ int main() { double a[3][3] = { { 5.4, 3.2, 8}, { 6, 4, 3.3 }, { 7, 3, 1.3 } }; int i, j; double tmp; //---------------輸出原數組------------------------------ puts("原數組內容:"); for (i = 0; i < 3; i ++) { for (j = 0; j < 3; j++) { printf("%lf ", a[i][j]); } putchar(10); } for (i = 0; i < 3; i++) { tmp = a[i][i]; for (j = 0; j < 3; j++) { a[i][j] /= tmp; } } //---------------輸出新數組------------------------------ puts("新數組內容:"); for (i = 0; i < 3; i ++) { for (j = 0; j < 3; j++) { printf("%lf ", a[i][j]); } putchar(10); } getch(); return 0; } #include <stdio.h> #include <conio.h> #include <math.h>/* 3、 編程:設x取值為區間[1,20]的整數,求函數f(x)=x-sin(x)- cos(x) 的最大值,要求使用自定義函數實現f(x)功能。(20分) */ double f(int x);int main() { int i, j; double maxFx = 0.0, tmp = 0.0; for (i = 1; i <= 20; i++) { tmp = f(i); if (maxFx - tmp < 1e-7) maxFx = tmp; } printf("maxF(x) = %lf\n", maxFx); getch(); return 0; }double f(int x) { return (double)(x - sin(x) - cos(x)); } #include <stdio.h> #include <conio.h> #include <string.h>/* 4.編寫函數fun,通過指針實現將一個字符串反向。要求主函數輸入 字符串,通過調用函數fun實現輸入字符串反向。(20分) */ void fun(char str[]) { int len = strlen(str); int i, j; char tmp; for (i = 0, j = len-1; i < j; i++, j--) { tmp = str[i]; str[i] = str[j]; str[j] = tmp; } }int main() { char str[100]; gets(str); //輸入字符串 puts("字符串反向前:"); puts(str); fun(str); puts("字符串反向後:"); puts(str); getch(); return 0; } #include <stdio.h> #include <conio.h> #include <string.h>/* 5、已知學生三門課程基本信息如下。請使用結構體編程,計算學生三門 課程平均成績後,列表輸出學生的姓名、數學、英語、計算機、平均分信 息,並按平均分排序。(20分) 姓名 數學 英語 計算機 Mary 93 100 88 Jone 82 90 90 Peter 91 76 71 Rose 100 80 92 */ /*------------聲明結構-------------------*/ typedef struct Student { char name[20]; float math; float english; float computer; float avg; }STU;int cmp(const void *lhs, const void *rhs) { STU *f = (STU *)lhs; STU *s = (STU *)rhs; return f->avg - s->avg > 1e-7; }int main() { STU s[4] = {{"Mary", 93, 100, 88, 0.0}, {"Jone", 82, 90, 90, 0.0}, {"Peter", 91, 76, 71, 0.0}, {"Rose", 100, 80, 92, 0.0}}; float average = 0.0f; int i, j; for (i = 0; i < 4; i++) { s[i].avg += s[i].math + s[i].english + s[i].computer; s[i].avg /= 3; } //從小到大排序 qsort(s, 4, sizeof(s[0]), cmp); for (i = 0; i < 4; i++) { printf("%-8s%-5.0f%-5.0f%-5.0f%-5.3f\n", s[i].name, s[i].math, s[i].english, s[i].computer, s[i].avg); } return 0; } #include <stdio.h> #include <conio.h> #include <string.h> typedef struct ARTICLE { char word[15]; int counter; }Article; //判斷是否已經存在 int isExist(Article *art, int size, char *tmp); int main() { Article article[200]; char tmp[15]; int i = 0, size, index; for (i = 0; i < 200; i++)//初始化 { article[i].counter = 1; } i = 0;//不能少這一步 scanf("%s", article[i].word); size = 1; while ( scanf("%s", tmp) && strcmp(tmp, "000") != 0) { index = isExist(article, size , tmp); //printf("index = %d\n", index); if (index != -1) { article[index].counter++; } else { strcpy(article[++i].word, tmp); size += 1; } } for (i = 0; i < size; i++) { printf("%s\t", article[i].word); } putchar(10); for (i = 0; i < size; i++) { printf("%d\t", article[i].counter); } putchar(10); return 0; } int isExist(Article *art, int size, char *tmp) { int i; for (i = 0; i < size; i++) { if (strcmp(tmp, art[i].word) == 0) return i;//返回下標 } return -1; }
最後更新:2017-04-02 15:15:14