答案见下方截图,其他pta题目见我的专栏,有sduwh的全套C语言pta题目
考试公告
Chapter 5 Pointers and Arrays
分数 2
作者 Sun Jun
单位 武汉理工大学
An array can store many different types of values.
T
F
分数 2
作者 Sun Jun
单位 武汉理工大学
If there are fewer initializers than the number of elements in the array, C automatically initializes the remaining elements to the last value of the list of initializers.
T
F
分数 2
作者 Sun Jun
单位 武汉理工大学
It’s an error if an initializer list contains more initializers than there are elements in the
array.
T
F
分数 2
作者 sun jun
单位 武汉理工大学
If there is declaration int a[] = {2,4,6,8,10}, *p= a;
a 's value is the first address of the array, then *(p++)
's value is 4.
T
F
分数 2
作者 sun jun
单位 武汉理工大学
Statement int * p,q,r;
declares three pointer variable
T
F
分数 2
作者 sun jun
单位 武汉理工大学
To declare int a[10] , * p=a
, both statements p = a+1
and a = a+1
are valid.
T
F
分数 2
作者 sun jun
单位 武汉理工大学
When initialize to all element in a 2-dimention array, row number cannot be ommited, but column number could.
,
T
F
分数 2
作者 sun jun
单位 武汉理工大学
If the value of function strcmp(a,b)
is less than 0, it means that the parameters of function might be wrong.
T
F
分数 2
作者 sun jun
单位 武汉理工大学
For initializing a 2-dimension array, if the initializers are complete, the initialization statement could be as following:
int a[][]={{1,2,3,4},{5,6,7,8}};
T
F
分数 2
作者 sun jun
单位 武汉理工大学
For a correctly defined 2-dimensional array, *(a[i]+j)
is same as a[i][j]
.
T
F
分数 2
作者 sun jun
单位 武汉理工大学
Which statement can initialize a one-dimensional array?
A.
int a[10]=(0,0,0,0,0);
B.
int a[2]={0,1,2};
C.
int a[]={0};
D.
int a[]={ };
分数 2
作者 往年试卷
单位 浙江大学
若变量已正确定义并且指针p已经指向某个变量x,则(*p)++相当于____。
A.
p++
B.
x++
C.
*(p++)
D.
&x++
创建提问
分数 2
作者 往年试卷
单位 浙江大学
若p1、p2都是整型指针,p1已经指向变量x,要使p2也指向x, ____是正确的。
A.
p2=p1
B.
p2=**p1
C.
p2=&p1
D.
p2=*p1
创建提问
分数 2
作者 往年试卷
单位 浙江大学
下列程序段的输出是____。
int c[]={1, 3, 5};
int *k=c+1;
printf("%d", *++k);
A.
3
B.
4
C.
5
D.
6
创建提问
分数 2
作者 往年试卷
单位 浙江大学
对于如下说明,语法和语义都正确的赋值是_____。
int c, *s, a[]={1, 3, 5};
A.
c=*s;
B.
s[0]=a[0];
C.
s=&a[1];
D.
c=a;
创建提问
分数 2
作者 往年试卷
单位 浙江大学
If variables are defined and assigned correctly, the expression ______ is wrong.
A.
a&b
B.
a^b
C.
&&x
D.
a, b
创建提问
分数 2
作者 往年试卷
单位 浙江大学
According to the declaration: int a[10], *p=a;
the expression __ is wrong.
A.
a[9]
B.
p[5]
C.
*p++
D.
a++
创建提问
分数 2
作者 往年试卷
单位 浙江大学
Among the following assignments or initializations, __ is wrong.
A.
char str[10]; str="string";
B.
char str[]="string";
C.
char *p="string";
D.
char *p; p="string";
创建提问
分数 2
作者 往年试卷
单位 浙江大学
According to the declaration: int p[5], *a[5];
the expression ______ is correct.
A.
p=a
B.
p[0] = a
C.
*(a+1)=p
D.
a[0]=2
创建提问
分数 2
作者 往年试卷
单位 浙江大学
Among the following assignments or initializations, __ is wrong.
A.
char s[ ]="hello";
B.
char s[10]; s="hello";
C.
char *p="hello";
D.
char *p; p="hello";
创建提问
分数 2
作者 往年试卷
单位 浙江大学
According to the declaration: int (*p)[10];
, p is a(n) __.
A.
pointer
B.
array
C.
function
D.
element of array
创建提问
分数 2
作者 王秀
单位 福州大学
若p1
、p2
都是整型指针,p1
已经指向变量x
,要使p2
也指向x
, ( )是正确的。
A.
p2 = p1;
B.
p2 = **p1;
C.
p2 = &p1;
D.
p2 = *p1;
创建提问
分数 2
作者 王秀
单位 福州大学
根据声明int a[10], *p=a;
,下列表达式错误的是( )。
A.
a[9]
B.
p[5]
C.
a++
D.
*p++
创建提问
分数 2
作者 往年试卷
单位 浙江大学
With regard to the array definition int a[4];
,which expression among the following is wrong?
A.
*a
B.
a[0]
C.
a
D.
a++
创建提问
分数 2
作者 往年试卷
单位 浙江大学
若定义pf
为指向float
类型变量f
的指针,下列语句中__是正确的。
A.
float f, *pf = f;
B.
float f, *pf = &f;
C.
float *pf = &f, f;
D.
float f, *pf =0.0;
创建提问
分数 2
作者 往年试卷
单位 浙江大学
Which one is completely correct among the following program fragments?
A.
int *p; scanf("%d", &p);
B.
int *p; scanf("%d", p);
C.
int k, *p=&k; scanf("%d", p);
D.
int k, *p; *p= &k; scanf("%d", p);
创建提问
分数 2
作者 往年试卷
单位 浙江大学
Among the following statements, __ is equivalent to the declaration: int *p[4];
.
A.
int p[4];
B.
int **p;
C.
int *(p[4]);
D.
int (*p)[4];
创建提问
分数 2
作者 往年试卷
单位 浙江大学
In the following declarations, the correct assignment expression is __.
int *p[3], a[3];
A.
p = a
B.
p = &a[0]
C.
*p = a
D.
p[0] = *a
创建提问
分数 2
作者 往年试卷
单位 浙江大学
For definitions: char s[2][3]={"ab", "cd"}, *p=(char *)s;
the expression __ is correct and its value is equivalent to the element s[1][1]
.
A.
*(s+3)
B.
*s+2
C.
p[1][1]
D.
*++p+2
创建提问
分数 2
作者 周强
单位 青岛大学
以下哪个定义中的p
不是指针,请选择恰当的选项:
A.
char **p;
B.
char (*p)[10];
C.
char *p[6];
D.
给出的三项中,p
都是指针
6-1 5.2-2 swap-correct
分数 5
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
interchange *px and *py .
函数接口定义:
void swap(int *px, int *py);
裁判测试程序样例:
#include<stdio.h>
void swap(int *px, int *py);
int main()
{
int a;
int b;
scanf("%d%d", &a, &b);
printf("a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
/* 请在这里填写答案 */
void swap(int *px, int *py){
int temp;
temp= *px;
*px= *py;
*py=temp;
}
6-2 5.2-3 getint
分数 10
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
getint: get next integer from input into *pn.
函数接口定义:
int getint(int *pn);
裁判测试程序样例:
#include<stdio.h>
#include <ctype.h>
#define SIZE 100
int getch(void); // defined already
void ungetch(int); // deined already
int getint(int *pn);
int main()
{
int n, array[SIZE];
for (n = 0; n < SIZE && getint(&array[n]) != EOF && array[n] != 0; n++)
printf("%d\n", array[n]);
return 0;
}
/* 请在这里填写答案 */
输入样例:
10
11
0
输出样例:
10
11
int getint(int *pn){
int c,sign;
while(isspace(c=getch()))
;
if(!isdigit(c) && c!=EOF && c!='+' && c!='-'){
ungetch(c);
return -1;
}
sign = (c=='-')? -1 : 1;
if(c=='+'||'-')
c=getch();
for(*pn=0;isdigit(c);c=getch())
*pn=10* *pn+(c-'0');
*pn=*pn * sign;
if(c!=EOF)
ungetch(c);
return c;
}
/*int getint(int *pn)
{
int c,sign;
;
if(!isdigit(c)&&c!=EOF&&c!='+'&&c!='-'){
ungetch(c);
return -1;
}
sign= (c=='-') ? -1:1;
if(c=='+'||c=='-'){
c=getch();
if(!isdigit(c)){
ungetch(c);
return -1;
}
}
for(*pn=0;isdigit(c);c=getch()){
*pn=10*(*pn)+(c-'0');
}
*pn *= sign;
if(c!=EOF)
ungetch(c);
return 0;
}
*/
6-3 5.3 strlen-pointer
分数 5
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
strlen: return length of a string.
函数接口定义:
unsigned strlen_my(char *s);
裁判测试程序样例:
#include <stdio.h>
unsigned strlen_my(char *s);
int main()
{
char array[100] = "hello, world";
char *ptr = array;
printf("%d\n", strlen_my("hello, world")); /* string constant */
printf("%d\n", strlen_my(array)); /* char array[100]; */
printf("%d\n", strlen_my(ptr)); /* char *ptr; */
while(~scanf("%s", array)) {
printf("%d\n", strlen_my(array));
}
return 0;
}
/* 请在这里填写答案 */
unsigned strlen_my(char *s){
int n;
for(n=0;*s!='\0';s++)
n++;
return n;
}
6-4 5.5- 1-4 strcpy v1 array subscript
分数 5
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
strcpy: copy t to s.
array subscript version.
函数接口定义:
void strcpy(char *const s, char *const t);
裁判测试程序样例:
#include<stdio.h>
void strcpy(char *const s, char *const t);
char *alloc(int n);
int main()
{
char *from = alloc(1000);
char *to = alloc(1000);
while(~scanf("%s", from)) {
strcpy(to, from);
printf("%s\n", to);
}
return 0;
}
/* 请在这里填写答案 */
输入样例:
abc
输出样例:
abc
void strcpy(char *const s, char *const t){
int i=0;
for(i=0;*(t+i)!='\0';i++)
*(s+i)=*(t+i);
*(s+i)='\0'; //这玩意,必须手动在后面加上'\0';否则的话下一行就会出错
//注意,这个程序是循环读取的,因此上一次的会影响下一次的。
}
6-5 5.5-2 strcpy v2 pointer
分数 5
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
strcpy: copy t to s;
pointer version;
函数接口定义:
void strcpy( char *restrict dest, const char *restrict src );
裁判测试程序样例:
#include<stdio.h>
void strcpy( char *restrict dest, const char *restrict src );
char *alloc(int n);
int main()
{
char *from = alloc(1000);
char *to = alloc(1000);
while(~scanf("%s", from)) {
strcpy(to, from);
printf("%s\n", to);
}
return 0;
}
/* 请在这里填写答案 */
输入样例:
abc
输出样例:
abc
void strcpy( char *restrict dest, const char *restrict src ){
while(*src!='\0'){
*dest=*src;
dest++;
src++;
}
*dest='\0';
}
6-6 5.5-5 strcmp
分数 5
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
Compares the C string str1 to the C string str2.
<0 : the first character that does not match has a lower value in ptr1 than in ptr2.
0 : the contents of both strings are equal.
>0 : the first character that does not match has a greater value in ptr1 than in ptr2.
函数接口定义:
int strcmp ( const char * str1, const char * str2 );
裁判测试程序样例:
#include<stdio.h>
int strcmp ( const char * str1, const char * str2 );
int main()
{
char s[100], t[100];
while(scanf("%s%s", s, t) != EOF) {
printf("%d\n", strcmp(s, t));
}
return 0;
}
/* 请在这里填写答案 */
输入样例:
abca
abca
abcd
abce
输出样例:
0
-1
int strcmp ( const char * str1, const char * str2 ){
/* for(;*str1!='\0';str1++,str2++ ){
if(*str1!=*str2)
return *str1-*str2;
}
if(*str2!='\0')
return *str1-*str2;
return 0;*/
for(;*str1==*str2;str1++,str2++)
if(*str1=='\0')
return 0;
return *str1-*str2;
}
6-7 5.6 TextSort
分数 15
全屏浏览题目
切换布局
作者 sduwh_yh
单位 山东大学(威海)
qsort: sort v[left]...v[right] into increasing order
函数接口定义:
void qsort(char *lineptr[], int left, int right);
qsort: sort v[left]...v[right] into increasing order
裁判测试程序样例:
#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /* max #lines to be sorted */
#define MAXLEN 1000 /* max length of any input line */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int maxlines); //defined
void writelines(char *lineptr[], int nlines); //defined
void qsort(char *lineptr[], int left, int right);
/* sort input lines */
int main()
{
int nlines; /* number of input lines read */
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort(lineptr, 0, nlines-1);
writelines(lineptr, nlines);
return 0;
} else {
printf("error: input too big to sort\n");
return 1;
}
}
/* 请在这里填写答案 */
输入样例:
aaaaaaa
aaaaaa
aaaaa
aaaa
aaa
aa
b
输出样例:
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
b
void swap(char *v[],int i,int j)
{
char *tmp;
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
}
void qsort(char *v[], int left, int right)
{
if(left>=right)
return;
swap(v,left,(left+right)/2);
int last=left;
for(int i=left+1;i<=right;++i)
if(strcmp(v[i],v[left])<0)
swap(v,++last,i);
swap(v,left,last);
qsort(v,left,last-1);
qsort(v,last+1,right);
}