序言
pta的c语言练习(自己找的,仅供学习)
本文不会提供所有的题目与代码
网址 L1-001
Hello World - 团体程序设计天梯赛-练习集 (pintia.cn)
L1-005 考试座位号
每个 PAT
考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。
输入格式:
输入第一行给出一个正整数 N(≤1000),随后 N
行,每行给出一个考生的信息:准考证号 试机座位号 考试座位号。其中准考证号由
16 位数字组成,座位从 1 到 N
编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。
考生信息之后,给出一个正整数 M(≤N),随后一行中给出 M
个待查询的试机座位号码,以空格分隔。
输出格式:
对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用
1 个空格分隔。
输入样例:
1 2 3 4 5 6 7 4 3310120150912233 2 4 3310120150912119 4 1 3310120150912126 1 3 3310120150912002 3 2 2 3 4
输出样例:
1 2 3310120150912002 2 3310120150912119 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <stdio.h> struct student { char id[17 ]; int tryseat; int examseat; }; int main (void ) { int n=0 ; scanf ("%d" ,&n); struct student students [n ]; for (int i=0 ;i<n;i++) { scanf ("%s %d %d" ,students[i].id,&students[i].tryseat,&students[i].examseat); } int m=0 ; scanf ("%d" ,&m); int arr[m]; for (int i=0 ;i<m;i++) { scanf ("%d" ,&arr[i]); } for (int i=0 ;i<m;i++) { for (int j=0 ;j<n;j++) { if (students[j].tryseat==arr[i]) { printf ("%s %d\n" ,students[j].id,students[j].examseat); break ; } } } }
L1-006 连续因子
一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为
3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数
N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。
输入格式:
输入在一行中给出一个正整数 N(1<N<231)。
输出格式:
首先在第 1 行输出最长连续因子的个数;然后在第 2 行中按
因子1*因子2*……*因子k
的格式输出最小的连续因子序列,其中因子按递增顺序输出,1 不算在内。
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> #include <math.h> int main () { long long n; scanf ("%lld" , &n); long long i, j, len = 0 , start = 0 , tempLen, tempStart; long long sqrtN = sqrt (n); for (i = 2 ; i <= sqrtN; i++) { long long temp = 1 ; for (j = i; j <= sqrtN; j++) { temp *= j; if (n % temp!= 0 ) break ; if (j - i + 1 > len) { len = j - i + 1 ; start = i; } } } if (len == 0 ) { printf ("1\n%lld" , n); } else { printf ("%lld\n" , len); for (i = start; i < start + len - 1 ; i++) { printf ("%lld*" , i); } printf ("%lld" , start + len - 1 ); } return 0 ; }
L1-010 比较大小
本题要求将输入的任意3个整数从小到大输出。
输入格式:
输入在一行中给出3个整数,其间以空格分隔。
输出格式:
在一行中将3个整数从小到大输出,其间以“->”相连。
输入样例:
输出样例:
我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <stdio.h> void bubbleSort (int arr[], int n) { int i, j; for (i = 0 ; i < n - 1 ; i++) { for (j = 0 ; j < n - i - 1 ; j++) { if (arr[j] > arr[j + 1 ]) { int temp = arr[j]; arr[j] = arr[j + 1 ]; arr[j + 1 ] = temp; } } } } int main () { int arr[3 ]; for (int i=0 ;i<3 ;i++) { scanf ("%d" ,&arr[i]); } bubbleSort(arr,3 ); for (int i=0 ;i<2 ;i++) { printf ("%d->" ,arr[i]); } printf ("%d" ,arr[2 ]); }
AI的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> void bubbleSort (int arr[], int n) { int i, j; for (i = 0 ; i < n - 1 ; i++) { for (j = 0 ; j < n - i - 1 ; j++) { if (arr[j] > arr[j + 1 ]) { int temp = arr[j]; arr[j] = arr[j + 1 ]; arr[j + 1 ] = temp; } } } } int main () { int arr[3 ]; int i; scanf ("%d %d %d" , &arr[0 ], &arr[1 ], &arr[2 ]); bubbleSort(arr, 3 ); printf ("%d->%d->%d" , arr[0 ], arr[1 ], arr[2 ]); return 0 ; }
L1-023 输出GPLT
给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
输入样例:
1 pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <stdio.h> #include <ctype.h> int main () { char str[10001 ]; scanf ("%s" , str); int counts[4 ] = {0 }; for (int i = 0 ; str[i]; i++) { char c = tolower (str[i]); if (c == 'g' ) counts[0 ]++; else if (c == 'p' ) counts[1 ]++; else if (c == 'l' ) counts[2 ]++; else if (c == 't' ) counts[3 ]++; } while (counts[0 ] > 0 || counts[1 ] > 0 || counts[2 ] > 0 || counts[3 ] > 0 ) { if (counts[0 ] > 0 ) { putchar ('G' ); counts[0 ]--; } if (counts[1 ] > 0 ) { putchar ('P' ); counts[1 ]--; } if (counts[2 ] > 0 ) { putchar ('L' ); counts[2 ]--; } if (counts[3 ] > 0 ) { putchar ('T' ); counts[3 ]--; } } putchar ('\n' ); return 0 ; }