PTA C语言期末考试(2021/7/6)
一. 编程题
7.1 简单求阶乘问题
7.2 求n以内最大的k个素数以及它们的和
7.3 求整型数组前n个数中正数之和
7-4 学分统计
7.1 简单求阶乘问题
# include <stdio.h>
int main ( )
{
int n, i;
int s= 1 ;
scanf ( "%d" , & n) ;
if ( n== 0 || n== 1 ) {
s= 1 ;
}
else if ( n>= 2 ) {
for ( i= 1 ; i<= n; i++ ) {
s*= i;
}
}
printf ( "%d" , s) ;
}
7.2 求n以内最大的k个素数以及它们的和
# include <stdio.h>
# include <math.h>
# define N 10
int isPrime ( int p) ;
int main ( ) {
int n, k, i, count, sum= 0 ;
int maxPrimes[ N] ;
scanf ( "%d%d" , & n, & k) ;
i = n;
count = 0 ;
while ( i> 0 ) {
if ( count< k) {
if ( isPrime ( i) ) {
maxPrimes[ count] = i;
sum += i;
count++ ;
}
}
else {
break ;
}
i-- ;
}
for ( i= 0 ; i< count- 1 ; i++ ) {
printf ( "%d+" , maxPrimes[ i] ) ;
}
printf ( "%d=%d" , maxPrimes[ count- 1 ] , sum) ;
return 0 ;
}
int isPrime ( int p) {
int isPrime= 1 ;
int i;
if ( p< 2 ) {
isPrime = 0 ;
}
else {
for ( i= 2 ; i<= ( int ) sqrt ( p) ; i++ ) {
if ( p% i == 0 ) {
isPrime = 0 ;
break ;
}
}
}
return isPrime;
}
7.3 求整型数组前n个数中正数之和
# include <stdio.h>
int main ( )
{
int n, m, i, s= 0 ;
scanf ( "%d" , & n) ;
if ( n>= 1 && n<= 10 ) {
for ( i= 0 ; i< n; i++ ) {
scanf ( "%d" , & m) ;
if ( m> 0 ) {
s+= m;
}
}
printf ( "%d" , s) ;
}
else {
printf ( "error" ) ;
}
return 0 ;
}
7-4 学分统计(结构体)
# include <stdio.h>
typedef struct
{
char number[ 50 ] ;
char name[ 50 ] ;
int score;
} Stu;
Stu stu[ 100 ] ;
int main ( ) {
int n, i, num= 0 ;
scanf ( "%d" , & n) ;
for ( i= 0 ; i< n; i++ ) {
scanf ( "%s %s %d" , stu[ i] . number, stu[ i] . name, & stu[ i] . score) ;
}
for ( i= 0 ; i< n; i++ ) {
if ( stu[ i] . score>= 85 )
{
printf ( "%s %s A\n" , stu[ i] . number, stu[ i] . name) ;
}
if ( stu[ i] . score>= 70 && stu[ i] . score<= 84 )
{
printf ( "%s %s B\n" , stu[ i] . number, stu[ i] . name) ;
}
if ( stu[ i] . score>= 60 && stu[ i] . score<= 69 )
{
printf ( "%s %s C\n" , stu[ i] . number, stu[ i] . name) ;
}
if ( stu[ i] . score< 60 && stu[ i] . score>= 0 )
{
printf ( "%s %s D\n" , stu[ i] . number, stu[ i] . name) ;
num++ ;
}
}
printf ( "%d\n" , num) ;
return 0 ;
}
函数题
6-1 日期 - 9. 输出星期名(中文)
6-3 自然数各位数字之和(递归版)
6-4 求出二维数组的最大元素及其所在的坐标
6-5 数组循环右移
6-1 日期 - 9. 输出星期名(中文)
# include <stdio.h>
void ShowNameDayWeek ( int dow) ;
int main ( )
{
int n;
scanf ( "%d" , & n) ;
ShowNameDayWeek ( n) ;
putchar ( '\n' ) ;
return 0 ;
}
void ShowNameDayWeek ( int dow)
{
switch ( dow)
{
case 0 :
printf ( "日" ) ;
break ;
case 1 :
printf ( "一" ) ;
break ;
case 2 :
printf ( "二" ) ;
break ;
case 3 :
printf ( "三" ) ;
break ;
case 4 :
printf ( "四" ) ;
break ;
case 5 :
printf ( "五" ) ;
break ;
case 6 :
printf ( "六" ) ;
break ;
default :
break ;
}
}
6-3 自然数各位数字之和(递归版)
# include <stdio.h>
int SumDigit ( int number) ;
int main ( )
{
int n;
scanf ( "%d" , & n) ;
printf ( "%d\n" , SumDigit ( n) ) ;
return 0 ;
}
int SumDigit ( int number)
{
int a, b, c, sum= 0 ;
do {
a= number% 10 ;
sum+= a;
number= number/ 10 ;
} while ( number!= 0 ) ;
return sum;
}
6-4 求出二维数组的最大元素及其所在的坐标
# include <stdio.h>
# define N 4
# define M 3
int Row, Col;
int fun ( int array[ N] [ M] ) ;
int main ( )
{
int a[ N] [ M] , i, j, max, row, col;
for ( i= 0 ; i< N; i++ )
for ( j= 0 ; j< M; j++ )
scanf ( "%d" , & a[ i] [ j] ) ;
for ( i= 0 ; i< N; i++ )
{ for ( j= 0 ; j< M; j++ )
printf ( "%5d" , a[ i] [ j] ) ;
printf ( "\n" ) ;
}
max= fun ( a) ;
printf ( "max=%d,row=%d,col=%d" , max, Row, Col) ;
return 0 ;
}
int fun ( int array[ N] [ M] )
{
int max, i, j, row, col;
max= array[ 0 ] [ 0 ] ;
row= 0 ;
col= 0 ;
for ( i= 0 ; i<= N; i++ )
for ( j= 0 ; j<= M; j++ )
if ( array[ i] [ j] > max) {
max= array[ i] [ j] ;
row= i;
col= j;
}
}
6-5 数组循环右移
# include <stdio.h>
# define MAXN 10
void ArrayShift ( int a[ ] , int n, int m ) ;
int main ( )
{
int a[ MAXN] , n, m;
int i;
scanf ( "%d %d" , & n, & m) ;
for ( i = 0 ; i < n; i++ ) scanf ( "%d" , & a[ i] ) ;
ArrayShift ( a, n, m) ;
for ( i = 0 ; i < n; i++ ) {
if ( i != 0 ) printf ( " " ) ;
printf ( "%d" , a[ i] ) ;
}
printf ( "\n" ) ;
return 0 ;
}
void ArrayShift ( int a[ ] , int n, int m )
{
m%= n;
int j= 0 , i, k, g= m;
int b[ n] ;
for ( i= n- m; i< n; i++ , j++ )
{
b[ j] = a[ i] ;
}
for ( k= 0 ; k<= n- m- 1 ; k++ , g++ )
{
b[ g] = a[ k] ;
}
for ( i= 0 ; i< n; i++ )
a[ i] = b[ i] ;
return a[ n] ;
}
6-6 写字母
# include <stdio.h>
# include <stdlib.h>
void WriteLetter ( FILE * f, int n) ;
int main ( )
{
FILE * f;
int n;
f = fopen ( "Letter.txt" , "w" ) ;
if ( ! f)
{
puts ( "文件无法打开!" ) ;
exit ( 1 ) ;
}
scanf ( "%d" , & n) ;
WriteLetter ( f, n) ;
if ( fclose ( f) )
{
puts ( "文件无法关闭!" ) ;
exit ( 1 ) ;
}
puts ( "文件保存成功!" ) ;
return 0 ;
}
void WriteLetter ( FILE * f, int n)
{
int i;
char c[ 27 ] = { ' ' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' } ;
getchar ( ) ;
for ( i= 1 ; i<= n; i++ )
{
fputc ( c[ i] , f) ;
}
}