本帖最后由 jackz007 于 2024-10-26 09:08 编辑
答案是 13
【代码1】:#include <stdio.h>
#include <stdlib.h>
int main(void)
{
typedef struct chain {
int data ;
struct chain * next ;
} CHAIN , * PCHAIN ;
PCHAIN head , p , q ;
int c , i , k , n = 13 , r ;
for(r = 1 , i = 0 ; i < n ; i ++) {
if (p = (PCHAIN) malloc(sizeof(CHAIN))) {
p -> data = i + 1 ;
p -> next = NULL ;
if(! i) {
head = p ;
p -> next = p ;
} else {
q -> next = p ;
p -> next = head ;
}
q = p ;
} else {
fprintf(stderr , "malloc() failure.\n") ;
r = 0 ;
}
}
if(r) {
for(k = n , c = 0 , p = q = head ; k > 1 ; p = q -> next) {
c ++ ;
if(! (c % 3)) {
q -> next = p -> next ;
k -- ;
free(p) ;
} else {
q = p ;
}
}
printf("%d\n" , p -> data) ;
free(p) ;
}
}
【代码2】:#include <stdio.h>
#include <stdlib.h>
int main(void)
{
typedef struct chain {
int data ;
struct chain * next ;
} CHAIN , * PCHAIN ;
PCHAIN head , p , q ;
int c , f , i , k , n = 13 , r ;
for(r = 1 , i = 0 ; i < n ; i ++) {
if (p = (PCHAIN) malloc(sizeof(CHAIN))) {
p -> data = i + 1 ;
if(! i) {
head = p ;
p -> next = p ;
} else {
q -> next = p ;
p -> next = head ;
}
q = p ;
} else {
fprintf(stderr , "malloc() failure.\n") ;
r = 0 ;
}
}
if(r) {
for(k = n , c = 0 , p = head ; k > 1 ; p = p -> next) {
if(p -> data) {
c ++ ;
if(! (c % 3)) {
p -> data = 0 ;
k -- ;
}
}
}
for(p = head ; ! p -> data ; p = p -> next) ;
printf("%d\n" , p -> data) ;
}
if(head) {
for(p = head , f = 1 ; f ; p = q) {
if(p -> next == head) f = 0 ;
q = p -> next ;
free(p) ;
}
}
}
|