关于PAT1004和1005的看法

来源:互联网 发布:宏观经济基础数据库 编辑:程序博客网 时间:2024/06/06 06:52

1004是用max和min存放最大成绩和最小成绩及其相关信息,不难。
1005是要求寻找“覆盖的数”,其关键是在完成判断之后按顺序输出。我认为在开头就排序比较好。
PAT1004代码:

#include<stdio.h>#include<string.h> int main(){    char tempName[15], tempClass[15], maxName[15], maxClass[15], minName[15], minClass[15];    int tempScore, maxScore = 0, minScore = 100, n;    scanf("%d", &n);    for(int i = 0; i < n; i++){        scanf("%s %s %d", tempName, tempClass, &tempScore);        if(tempScore > maxScore){            maxScore = tempScore;            strcpy(maxName,tempName);            strcpy(maxClass,tempClass);        }        if(tempScore < minScore){            minScore = tempScore;            strcpy(minName,tempName);            strcpy(minClass,tempClass);        }    }    printf("%s %s\n", maxName, maxClass);    printf("%s %s\n", minName, minClass);}

PAT1005代码:

#include<stdio.h>#include<string.h>int main(){    int k;    scanf("%d", &k);    int a[k];    for(int i = 0; i < k; i++){        scanf("%d", &a[i]);    }    for(int i = k-1; i > -1; i--){              //冒泡排序         for(int j = k-1; j > -1; j--){            int t;            if(a[i] > a[j]){                t = a[i];                a[i] = a[j];                a[j] = t;            }        }    }    for(int i = 0; i < k; i++){         int temp, a1;        a1 = a[i];        temp = a[i];        while(temp != 1 && temp != 0){            if(a1 % 2 == 0){                temp = a1 / 2;            }else{                temp = (3 * a1 + 1) / 2;            }            for(int j = 0; j < k; j++){                if(a[j] == temp){                    a[j] = 0;                    break;                }            }            a1 = temp;        }       }    int total = 0;    int cnt = 1;    for(int i = 0; i < k; i++){        if(a[i] != 0){            total++;        }    }        for(int i = k-1; i >= 0; i--){        if(a[i] != 0){            if(cnt != total){                printf("%d ", a[i]);                cnt++;            }else{                printf("%d", a[i]);            }        }    }}

希望各位大大批评指正!

0 0