美文网首页
PAT乙级习题C语言实现(VS13环境下)

PAT乙级习题C语言实现(VS13环境下)

作者: Jornathon | 来源:发表于2017-09-26 16:53 被阅读0次

    1002 写出这个数字

        /*1002 写出这个数字*/
        #include <string.h>
    
        int main()
        {
            int sum = 0;
            char input[101];
            char output[900];
            char chNum[10][5] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
            scanf_s("%s", input, 101);
            int i;
            for (i = 0; i < strlen(input); i++)
                sum += input[i] - '0';
            i = 0;
            while (sum)
            {
                output[i] = sum % 10;
                sum /= 10;
                i++;
            }
    
            for (i -= 1; i > 0; i--)
            {
                printf("%s ", chNum[output[i]]);
            }
            printf("%s", chNum[output[i]]);
            return 0;
        }
    

    1004成绩排名

        /*1004成绩排名*/
        int main()
        {
            int n = 0, i, ma, mi;
            scanf_s("%d", &n);
            typedef struct student
            {
                char name[20];
                char id[20];
                int score;
                int *next;
            }Student;
    
            Student studs[101];
    
            for (i = 0; i < n; i++)
            {
                printf("Please Enter the name:\n");
                scanf_s("%s", studs[i].name, 20);
                printf("Please Enter the id:\n");
                scanf_s("%s", studs[i].id, 20);
                printf("Please Enter the score:\n");
                scanf_s("%d", &(studs[i].score));
            }
    
            printf("Scanf complete!\n");
            ma = mi = 0;
            for (i = 1; i < n; i++)
            {
                ma = studs[ma].score > studs[i].score ? ma : i;
                mi = studs[mi].score < studs[i].score ? mi : i;
            }
            printf("%d-%d\n", ma, mi);
            printf("Rank Complete!\n");
    
            printf("%s %s\n", studs[ma].name, studs[ma].id);
            printf("%s %s\n", studs[mi].name, studs[mi].id);
            return 0;
        }
    

    1005继续(3n+1)猜想

        /*1005继续(3n+1)猜想*/
        void icmp(int *b, int *a)
        {
            //printf("The initial b:%d a:%d\n", *b, *a);
            int t = *a;
            while (*a != 1)
            {
                //printf("b:%d a:%d\n", *b, *a);
                if (*a % 2)
                    *a = (3 * (*a) + 1) / 2;
                else
                    *a /= 2;
                if (*a == *b)
                {
                    *b = t;
                    break;
                }
            }
            *a = t;
            //printf("The ultimate b:%d a:%d\n", *b, *a);
        }
    
        int delCopy(int *arr ,int *newArr, int n)
        {
            int flags[101] = { 0 };
            int k = 0;
            for (int i = 0; i < n; i++)
            {
                if (flags[arr[i]] == 0)
                {
                    newArr[k] = arr[i];
                    k++;
                    flags[arr[i]] = 1;
                }
            }
            return k--;
        }
    
        void iSort(int *a, int n)
        {
            int i, t;
            for (i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (a[i] < a[j])
                    {
                        t = a[i];
                        a[i] = a[j];
                        a[j] = t;
                    }
                }
            }
        }
    
        int main()
        {
            int i, j, n, *a;
            //printf("Please enter the aber:\n");
            scanf_s("%d", &n);
            a = (int *)malloc(sizeof(int)* n);
            //printf("Please scanf the abers:\n");
            for (i = 0; i < n; i++)
            {
                scanf_s("%d", &a[i]);
            }
            for (i = 0; i < n; i++)
            {
                for ( j = 0; j < n; j++)
                {
                    icmp(&a[i], &a[j]);
                    //printf("-%d-\n", i);
                }
            }
            //for (i = 0; i < n; i++)
            //{
            //  printf("%d ", a[i]);
            //}
            int b[101] = { 0 };
            iSort(a, n);
            int k =delCopy(a, b, n);
            for ( i = 0; i < k; i++)
            {
                printf("%d", b[i]);
                if (i == k-1)
                    continue;
                printf(" ");
            }
            return 0;
        }
    

    1006换个格式输出整数

        /*1006换个格式输出整数*/
        int main()
        {
            int i = 0, j, k, n, t[4] = { 0 };
            scanf_s("%d", &n);
            while (n)
            {
                t[i] = n % 10;
                n /= 10;
                i++;
            }
            for (j = i - 1; j >= 0; j--)
            {
                for (k = 0; k < t[j]; k++)
                {
                    if (j == 2)
                        printf("B");
                    if (j == 1)
                        printf("S");
                    if (j == 0)
                        printf("%d", k+1);
                }
            }
            return 0;
        }
    

    1007素数对猜想

        /*1007素数对猜想*/
        int iJudge(int n)
        {
            int i;
            for (i = 2; i < n/2; i++)
            {
                if (n % i == 0)
                {
                    return 0;
                }
            }
            return 1;
        }
        int main()
        {
            int n, i, t = 0;
            scanf_s("%d", &n);
            for (i = 3; i <= n - 2; i += 2)
            {
                if (iJudge(i) && iJudge(i + 2))
                    t++;
            }
            printf("%d\n", t);
            return 0;
        }

    相关文章

      网友评论

          本文标题:PAT乙级习题C语言实现(VS13环境下)

          本文链接:https://www.haomeiwen.com/subject/nxteextx.html