问题描述
约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。通常解决这类问题时我们把编号从0~n-1,最后 [1] 结果+1即为原问题的解。
数学上的一元多项式的表示是p(x) = p0 + p1 * x + p2 * x^2 + p3 * x^3 + … + pn * x^n;
用链表来表示就是p = (p0, p1, p2, … , pn);
所谓的多项式相加就是同类项的合并,也就是两条链表的合并。
采用单链表保存多项式,链表的每个结点表示多项式的每一非零项,链表应该按有序排列。
直接上源码
#include<stdio.h>
#include<stdlib.h>
typedef struct round{
int data ;//成员持有的密码数据
int id ;
int flag ;//标志是否已经出队
struct round* next ;
}round_t;
void input_data(round_t * h , int n) ;
void add_in(round_t * pro , round_t *q);
void free_node(round_t * h,int n);
void print_outqueue(round_t *h ,int number , int n);
int main(){
int n , number ;
round_t * h ;
h = (round_t *)malloc(sizeof(round_t)) ;
printf("\n------------------------------------\n");
printf("input number of members:");
scanf("%d" , &n);
printf("\n");
printf("input start number:");
scanf("%d",&number);
input_data(h , n);
print(h);
print_outqueue(h , number ,n);
free_node( h , n);
}
//根据输入初始密码出队
void print_outqueue(round_t * h , int number , int n){
int i = 1 ;
round_t *temp ;
temp = h ;
printf("\nthe member out queue is here");
printf("\n------------------------------------\n");
while(1){
if(temp->flag == 0){
temp = temp->next ;
continue ;
}
if(i == number){
printf("the No.%d member out,password:%d\n",temp->id , temp->data);
number = temp->data ;
i = 0 ;
temp->flag = 0 ;
n -- ;
if(n == 0)
break ;
}
i ++ ;
temp = temp->next ;
}
}
//将新节点加入到链表中
void add_in(round_t * pro , round_t * newnode){
newnode->next = pro->next;
pro->next= newnode ;
}
//销毁创建的链表
void free_node(round_t * h, int n ){
int i = 0 ;
round_t * temp, * q;
temp = h ;
q = temp->next ;
while(1){
i++ ;
if(i == n)break ;
free(temp) ;
temp = q ;
q = temp->next ;
}
}
//往节点中录入数据
void input_data(round_t * h , int n){
int i = 0 ;
round_t *p, *q ;
p = h ;
h->id = i ;
printf("\n------------------------------------\n");
printf("input No.%d member's password:",h->id);
scanf("%d",&(h->data));
h->flag = 1 ;
h->next = p ;
while(1){
i++ ;
if(i == n)break ;
q=(round_t *)malloc(sizeof(round_t));
q->id = i ;
q->flag = 1 ;
printf("input No.%d member's password:",q->id);
scanf("%d",&(q->data));
add_in(p ,q );
p = p->next;
}
}
//打印所有数据
void print(round_t * h){
round_t *temp ;
temp = h ;
int i = 0 ;
printf("all members is here\n");
printf("---------------------------------------\n");
while(1){
printf("the No.%d member password:",temp->id);
printf("%d\n",temp->data);
temp = temp->next ;
i++ ;
if(temp == h){
break ;
}
}
}
运行截图:
#include<stdio.h>
#include<stdlib.h>
typedef struct duoxiang{
int xishu ;
int zhishu ;
struct duoxiang* next ;
}mem_t;
void input_data(mem_t * h);
void print_data(mem_t *h);
void add_two(mem_t *h1 ,mem_t *h2);
void free_node(mem_t * h) ;
void add_node(mem_t* pro , mem_t * newnode);
void sort(mem_t *h) ;
int main(){
mem_t * h1,*h2 ;
h1= (mem_t *)malloc(sizeof(mem_t)) ;
h2= (mem_t *)malloc(sizeof(mem_t)) ;
input_data(h1);
input_data(h2);
printf("\n多项式1..... :) \n");
printf("----------------------------------------------------------------------\n");
sort(h1) ;
printf("p1 = ");
print_data(h1);
printf("\n多项式2..... :) \n");
printf("----------------------------------------------------------------------\n");
sort(h2) ;
printf("p2 = ") ;
print_data(h2);
add_two(h1 ,h2);
free_node(h1) ;
free_node(h2) ;
}
//往新节点录入数据
void input_data(mem_t * h){
mem_t *q ,*p ;
int x , z ;
h->next = NULL ;
p = h ;
printf("\n每次分别输入系数,指数两组数据,若输入的两个输同时为0,则表示输入结束\n");
printf("----------------------------------------------------------------------\n");
while(1){
scanf("%d%d",&x ,&z);
if(x == 0 && z == 0){
break ;
}
q = (mem_t *)malloc(sizeof(mem_t)) ;
q->xishu = x ;
q->zhishu = z ;
add_node(p , q) ;
}
}
//给链表传入新节点
void add_node(mem_t * pro , mem_t * newnode ){
newnode->next = pro->next ;
pro->next = newnode ;
}
//打印多项式
void print_data(mem_t * h){
mem_t *temp ;
temp = h->next ;
if(temp != NULL){
if(temp->xishu < 0){
printf("-");
}
}
while(temp != NULL){
if(temp->xishu < 0){
if(temp->xishu != -1)printf("%d",-temp->xishu);
}
else{
if(temp->xishu != 1){
printf("%d",temp->xishu) ;
}
}
if(temp->zhishu != 0){
if(temp->zhishu != 1){
printf("X^(%d)",temp->zhishu);
}
else{
printf("X");
}
}
temp = temp->next ;
if(temp != NULL){
if(temp->xishu > 0) printf(" + ") ;
else printf(" - ");
}
}
printf("\n");
}
//释放链表空间
void free_node(mem_t * h){
mem_t * temp , *q;
temp = h->next ;
while(temp != NULL){
h->next = temp->next ;
free(temp) ;
temp = h->next ;
}
free(h) ;
}
//对链表中指数进行从小到大排序
void sort(mem_t *h){
mem_t *temp , *q , *p;
int flag ;
p =(mem_t *)malloc(sizeof(mem_t));
temp = h->next ;
while(temp != NULL){
flag = 0 ;
q= temp->next ;
while(q !=NULL){
if(temp->zhishu > q->zhishu){
flag = 1 ;
p->xishu = temp->xishu ;
p->zhishu = temp->zhishu ;
temp->xishu = q->xishu ;
temp->zhishu = q->zhishu ;
q->xishu = p->xishu ;
q->zhishu = p->zhishu ;
}
q = q->next ;
}
if(flag == 0)break ;
temp = temp->next ;
}
}
//多项式相加,并显示结果
void add_two(mem_t * h1, mem_t *h2){
mem_t * h3 ,*q , *p ;
h3 = (mem_t *)malloc(sizeof(mem_t)) ;
mem_t * tp1 ,*tp2 ;
tp1 = h1->next ;
tp2 = h2->next ;
h3->next = NULL ;
q = h3 ;
while(tp1 != NULL && tp2 !=NULL){
p = (mem_t*)malloc(sizeof(mem_t)) ;
if(tp1->zhishu < tp2->zhishu){
p->zhishu = tp1->zhishu ;
p->xishu = tp1->xishu ;
add_node(q , p);
q= q->next ;
tp1 = tp1->next ;
}
else if(tp1->zhishu > tp2->zhishu){
p -> zhishu = tp2 -> zhishu ;
p -> xishu = tp2-> xishu ;
add_node(q , p) ;
q= q->next ;
tp2 = tp2->next ;
}
else{
p -> xishu = tp1 -> xishu + tp2 -> xishu ;
p -> zhishu = tp1->zhishu ;
add_node(q , p);
q = q->next ;
tp1 = tp1-> next ;
tp2 = tp2 -> next ;
}
}
mem_t *temp = NULL ;
if(tp1 != NULL){
temp = tp1 ;
}
if(tp2 !=NULL){
temp = tp2 ;
}
if(temp != NULL){
while(1){
p = (mem_t* )malloc(sizeof(mem_t)) ;
p->xishu = temp->xishu ;
p->zhishu = temp->zhishu ;
add_node(q , p);
q =q-> next ;
temp = temp->next ;
if(temp == NULL)break ;
}
}
printf("\n两多项式相加结果\n");
printf("----------------------------------------------------------------------\n");
sort(h3);
print_data(h3) ;
free_node(h3);
}
运行截图: