PAT基础题参考答案
6-1简单输出整数
本题要求实现一个函数,对给定的正整数N,打印从1到N的全部正整数。
函数接口定义:
void PrintN ( int N );
其中N是用户传入的参数。该函数必须将从1到N的全部正整数顺序打印出来,每个数字占1行。
裁判测试程序样例:
#include <stdio.h>
void PrintN ( int N );
int main ()
{
int N;
scanf("%d", &N);
PrintN( N );
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
输出样例:
1
2
3
答案
void PrintN(int N){
int i;
for(i=1;i<=N;i++)
printf("%d\n",i);
}
6-2多项式求值
本题要求实现一个函数,计算阶数为n,系数为a[0] … a[n]的多项式f(x)=∑ (a[i]×xi) 在x点的值。
函数接口定义:
double f( int n, double a[], double x );
其中n是多项式的阶数,a[]中存储系数,x是给定点。函数须返回多项式f(x)的值。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
double f( int n, double a[], double x );
int main()
{
int n, i;
double a[MAXN], x;
scanf("%d %lf", &n, &x);
for ( i=0; i<=n; i++ )
scanf(“%lf”, &a[i]);
printf("%.1f\n", f(n, a, x));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
2 1.1
1 2.5 -38.7
输出样例:
-43.1
答案
double f( int n, double a[], double x )
{
int i;
double mutlipy=1.0,sum=0;
for(i=0;i<=n;i++)
{
sum+=a[i]*mutlipy;
mutlipy*=x;
}
return sum;
}
6-3简单求和
本题要求实现一个函数,求给定的N个整数的和。
函数接口定义:
int Sum ( int List[], int N );
其中给定整数存放在数组List[]中,正整数N是数组元素个数。该函数须返回N个List[]元素的和。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
int Sum ( int List[], int N );
int main ()
{
int List[MAXN], N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%d", &List[i]);
printf("%d\n", Sum(List, N));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
12 34 -5
输出样例:
41
答案
int Sum( int List[], int N ){
int sum=0,i;
for(i=0;i<N;i++){
sum+=List[i];
}
return sum;
}
6-4求自定类型元素的平均 (10 分)
本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType。
函数接口定义:
ElementType Average( ElementType S[], int N );
其中给定集合元素存放在数组S[]中,正整数N是数组元素个数。该函数须返回N个S[]元素的平均值,其值也必须是ElementType类型。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Average( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Average(S, N));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
12.3 34 -5
输出样例:
13.77
答案
ElementType Average( ElementType S[], int N ){
int i;
ElementType sum;
for(i=0;i<N;i++){
sum+=S[i];
}
return sum/N;
}
6-5 求自定类型元素的最大值 (10 分)
本题要求实现一个函数,求N个集合元素S[]中的最大值,其中集合元素的类型为自定义的ElementType。
函数接口定义:
ElementType Max( ElementType S[], int N );
其中给定集合元素存放在数组S[]中,正整数N是数组元素个数。该函数须返回N个S[]元素中的最大值,其值也必须是ElementType类型。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Max( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Max(S, N));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
12.3 34 -5
输出样例:
34.00
答案
ElementType Max( ElementType S[], int N ){
int i;
ElementType m=S[0];
for(i=0;i<N;i++){
if(m<S[i]){
m=S[i];
}
}
return m;
}
6-6 求单链表结点的阶乘和 (15 分)
本题要求实现一个函数,求单链表L结点的阶乘和。这里默认所有结点的值非负,且题目保证结果在int范围内。
函数接口定义:
int FactorialSum( List L );
其中单链表List的定义如下:
typedef struct Node *PtrToNode;
struct Node {
int Data; /* 存储结点数据 */
PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Data; /* 存储结点数据 */
PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
int FactorialSum( List L );
int main()
{
int N, i;
List L, p;
scanf("%d", &N);
L = NULL;
for ( i=0; i<N; i++ ) {
p = (List)malloc(sizeof(struct Node));
scanf("%d", &p->Data);
p->Next = L; L = p;
}
printf("%d\n", FactorialSum(L));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
5 3 6
输出样例:
846
答案
int FactorialSum( List L ){
int sum=0,i;
List p=L;
while(p){
int ji=1;
for(i=1;i<=p->Data;i++){
ji*=i;
}
sum+=ji;
p=p->Next;
}
return sum;
}
6-7 统计某类完全平方数 (20 分)
本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144、676等。
函数接口定义:
int IsTheNumber ( const int N );
其中N是用户传入的参数。如果N满足条件,则该函数必须返回1,否则返回0。
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
int IsTheNumber ( const int N );
int main()
{
int n1, n2, i, cnt;
scanf("%d %d", &n1, &n2);
cnt = 0;
for ( i=n1; i<=n2; i++ ) {
if ( IsTheNumber(i) )
cnt++;
}
printf("cnt = %d\n", cnt);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
105 500
输出样例:
cnt = 6
答案
int IsTheNumber ( const int N ){
int a,temp[1000],count=0,i,m=N;
if(N<=0){
return 0;
}
a=(int)sqrt(N);
if (a*a==N){
while(m>0){
temp[count]=m%10; //取个位数字
m=m/10;//去掉个位数字
for(i=0;i<count;i++){
if(temp[count]==temp[i]){ //存在与之前取出的数字相同的即返回1
return 1;
}
}
count+=1;
}
return 0;
}
return 0;
}