描述:
输入1个数字和多个字符,中间均以空格隔开。假设数字取值为m(范围1~9),后面字符个数为n。假设n个字符围成一圈,从第一个字母开始循环报数,当数到m以后,第m个字母就出列,直到这n个字母全部出列。最后,按照出列的顺序输出这些字母,中间仍以空格隔开。取值范围:m为1到9, 字符个数n大于1小于20。
运行时间限制: 无限制
内存限制: 无限制
输入:
第一个为数字,后面为多个字符,均以空格隔开
输出:
输出重新排列后的字符,以空格隔开
样例输入:
3 a b c d e f g h i
样例输出:
c f i d h e b g a
答案提示:
建议采用循环链表
// ojTest06.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define N 42
typedef struct LNode{
char ch;
struct LNode * next;
}LNode,*LinkList;
int main(int argc, char* argv[])
{
int m,t,i;
// char c;
int n=0;
char a[N]="";
//char *p=a;
LinkList head,p1,p2;
//LNode node;
head=NULL;
p1=(LinkList)malloc(sizeof(LNode));
head=p1;
//scanf("%s",p);
gets(a);
for(i=0;i<N&&a[i]!='\0';i++){
if(i==0){
m=a[i]-48;
}else if(a[i]!=' '){
p2=(LinkList)malloc(sizeof(LNode));
p2->ch=a[i];
//p2=(LinkList)malloc(sizeof(LNode));
p1->next=p2;
p1=p2;
n++;
}
}
p1->next=head->next;
p1=head->next;
free(head);
for(i=0;i<n;i++){
t=m;
while(--t) {
p2=p1;
p1=p1->next;
}
printf("%c",p1->ch);
if(i!=n-1) printf(" ");
p2->next=p1->next;
free(p1);
p1=p2->next;
}
return 0;
}