作业要求一
1)C高级第三次PTA作业(1)
2)C高级第三次PTA作业(2)
作业要求二
1)C高级第三次PTA作业(1)
6-1 输出月份英文名
1.设计思路
2.实验代码
'char *getmonth( int n )
{
char *month[13]={"January","February","March","April","May","June","July","August","September","October","November","December"};
if(n>=13||n<=0){
return NULL;
}else{
return *(month+n-1);
}
}'
3.本题调试过程碰到问题及解决办法
6-2 查找星期
1.设计思路
2.实验代码
'int getindex( char *s ) {
char day[7][MAXS]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int i;
for(i=0;i<7;i++) {
if(strcmp(*(day+i),s)==0)
return (i);
}
if(i==7)
return (-1);
}'
6-3 计算最长的字符串长度
1.设计思路
2.实验代码
'int max_len( char *s[], int n )
{
int i,max=0,sum=0;
for(i=0;i<n;i++)
{
sum=strlen(s[i]);
if(max<sum)
{
max=sum;
}
}
return (max);
}'
6-4 指定位置输出字符串
1.设计思路
2.实验代码
'char *match( char *s, char ch1, char ch2 )
{
int i,n;
char *p=NULL;
for(i=0;*(s+i)!='\0';i++)
{
if(*(s+i)==ch1){
p=&s[i];
for(n=i;*(s+n)!='\0';n++){
if(*(s+n)!=ch2){
printf("%c",*(s+n));
}if(*(s+n)==ch2){
printf("%c\n",*(s+n));
return p;
}
}printf("\n");
return p;
}
}if (*(s+i)=='\0')
{
p=&s[i];
}
printf("\n");
return p;
}'
3.本题调试过程碰到问题及解决办法
2)一道编程题
1.设计思路
2.实验代码
3)C高级第三次PTA作业(2)
6-1 奇数值结点链表
1.设计思路
2.实验代码
'struct ListNode *readlist()
{
int data;
struct ListNode *head=NULL,*p=NULL,*tail=NULL;
scanf("%d",&data);
while(data != -1){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = data;
p->next = NULL;
if(head == NULL){
head = p;
tail = p;
}else{
tail->next = p;
tail = p;
}
scanf("%d",&data);
}
return head;
}
struct ListNode *getodd( struct ListNode **L )
{
struct ListNode *p = *L,*m=NULL,*n=NULL,*head1=NULL,*head2=NULL;
head1=(struct ListNode*)malloc(sizeof(struct ListNode));
head2=(struct ListNode*)malloc(sizeof(struct ListNode));
head1->next=NULL;
head2->next=NULL;
m=head1;
n=head2;
while (p) {
if((p->data)%2 == 1){
n->next=p;
n=p;
}else{
m->next=p;
m=p;
}
p = p->next;
}
m->next=NULL;
n->next=NULL;
*L = head1->next;
return head2->next;
}'
3.本题调试过程碰到问题及解决办法
6-2 学生成绩链表处理
1.设计思路
2.实验代码
'struct stud_node *createlist()
{
struct stud_node *tail=NULL,*head=NULL,*p=NULL;
int num=0,score=0;
char name[20];
scanf("%d",&num);
while(num!=0)
{
p=(struct stud_node*)malloc(sizeof(struct stud_node));
p->num=num;
scanf("%s %d",p->name,&p->score);
if(head==NULL)
{
head=p;
}else
{
tail->next=p;
}
tail=p;
scanf("%d",&num);
p->next=NULL;
}
return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
struct stud_node *ptr1=NULL,*ptr2=NULL;
for(;head!=NULL;head=head->next)
{
if(head->score>=min_score)
{
if(ptr1==NULL)
{
ptr1=head;
}else
{
ptr2->next=head;
}
ptr2=head;
}
}
if(ptr1==NULL)
{
return NULL;
}else
{
ptr2->next=NULL;
}
return ptr1;
}'
3.本题调试过程碰到问题及解决办法
6-3 链表拼接
1.设计思路
2.实验代码
'struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
int list[100],i=0,j=0,swap=0,count=0;
while(list1!=NULL)
{
list[i]=list1->data;
i++;
list1=list1->next;
count++;
}
while(list2!=NULL)
{
list[i]=list2->data;
i++;
list2=list2->next;
count++;
}
for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(list[i]>list[j])
{
swap=list[i];list[i]=list[j];list[j]=swap;
}
}
}
struct ListNode *p=NULL,*head=NULL,*tail=NULL;
for(i=0;i<count;i++)
{
p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->data=list[i];
p->next=NULL;
if(head==NULL)
{
head=p;
}else
{
tail->next=p;
}
tail=p;
}
return head;
}'