1.
n个人围成一圈,顺序编号。从第一个人开始从1到m报数,凡报到m的人退出圈子,编程求解最后留下的人的初始编号。 样例输入:(第一行输入) 6 3(两个输入数据之间有空格) 样例输出:(换行输出) 1
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int main (void)
{
int n;
int m;
scanf ("%d%d", &n, &m);
int queue[MAXN + 10] = {0};
int i;
for (i = 0; i < n; i ++)
{
queue[i] = i + 1;
}
int index = 0;
for (i = 0; i < n; i ++)
{
int cnt = 0;
while (cnt < m)
{
while (queue[index] == 0)
{
index = (index + 1) % n;
}
index = (index + 1) % n;
cnt ++;
}
index --;
if (index < 0)
{
index = n - 1;
}
if (i == n - 1)
{
printf ("%d\n", queue[index]);
}
queue[index] = 0;
}
return 0;
}
2.
建立一个链表,使链表中从头到尾的结点数据域依次是一个数组的各个元素的值。程序先建立链表然后再遍历输出(假定链表和数组均有6个整型元素)。 程序运行示例如下: 输入数组6个元素的值。 1 3 5 7 9 11 此链表各个结点的数据域为:1 3 5 7 9 11
#include <stdio.h>
#include <stdlib.h>
#define MAXN 6
struct Node
{
int data;
struct Node *next;
};
struct Head
{
int length;
struct Node *next;
};
typedef struct Node *pNode;
typedef struct Head *pHead;
pHead Create (void);
void Insert (pHead pH, int pos, int val);
void Print (pHead pH, pNode p);
int main (void)
{
printf ("输入数组6个元素的值。\n");
pHead pH = Create ();
int num;
int i;
for (i = 0; i < MAXN; i ++)
{
scanf ("%d", &num);
Insert (pH, 0, num);
}
printf ("此链表各个结点的数据域为:");
Print (pH, pH -> next);
return 0;
}
pHead
Create (void)
{
pHead pH = (pHead)malloc (sizeof (struct Head));
pH -> length = 0;
pH -> next = NULL;
return pH;
}
void
Insert (pHead pH, int pos, int val)
{
if (pH == NULL || pos < 0 || pos > pH -> length)
{
printf ("Error");
return;
}
pNode p = (pNode)malloc (sizeof (struct Node));
p -> data = val;
p -> next = NULL;
pNode pCur = pH -> next;
if (pos == 0) // else 部分可不写,此处锻炼个人熟练度
{
pH -> next = p;
p -> next = pCur;
}
else
{
int i;
for (i = 1; i < pos; i ++)
{
pCur = pCur -> next;
}
p -> next = pCur -> next;
pCur -> next = p;
}
pH -> length ++;
}
void
Print (pHead pH, pNode p)
{
if (p -> next != NULL)
{
Print (pH, p -> next);
}
printf ("%d ", p -> data);
}
3.
在一个有序(按非递减顺序)的链表中插入一个元素为x的结点,使插入后的链表仍然有序(链表数据域为整型数,初始时输入6个元素)。 程序运行示例如下: 输入数组6个元素的值。 12 23 34 45 56 67 此链表各个结点的数据域为:12 23 34 45 56 67 输入要插入的数据x:36 插入后链表各个结点的数据域为:12 23 34 36 45 56 67
#include <stdio.h>
#include <stdlib.h>
#define MAXN 6
struct Node
{
int data;
struct Node *next;
};
struct Head
{
int length;
struct Node *next;
};
typedef struct Node *pNode;
typedef struct Head *pHead;
pHead Create (void);
void Insert (pHead pH, int pos, int val);
int Search (pHead pH, int val);
void Print (pHead pH, pNode p);
int main (void)
{
printf ("输入数组6个元素的值。\n");
pHead pH = Create ();
int num;
int i;
for (i = 0; i < MAXN; i ++)
{
scanf ("%d", &num);
Insert (pH, 0, num);
}
printf ("此链表各个结点的数据域为:");
Print (pH, pH -> next);
int val;
printf ("\n输入要插入的数据x:");
scanf ("%d", &val);
int flag = Search (pH, val);
printf ("插入后链表各个结点的数据域为:");
Insert (pH, flag, val);
Print (pH, pH -> next);
return 0;
}
pHead
Create (void)
{
pHead pH = (pHead)malloc (sizeof (struct Head));
pH -> length = 0;
pH -> next = NULL;
return pH;
}
void
Insert (pHead pH, int pos, int val)
{
if (pH == NULL || pos < 0 || pos > pH -> length)
{
printf ("Error");
return;
}
pNode p = (pNode)malloc (sizeof (struct Node));
p -> data = val;
p -> next = NULL;
pNode pCur = pH -> next;
if (pos == 0)
{
pH -> next = p;
p -> next = pCur;
}
else
{
int i;
for (i = 1; i < pos; i ++)
{
pCur = pCur -> next;
}
p -> next = pCur -> next;
pCur -> next = p;
}
pH -> length ++;
}
int
Search (pHead pH, int val)
{
if (pH == NULL)
{
printf ("Error");
return 0;
}
int pos = 1;
pNode p = pH -> next;
while (p -> next != NULL)
{
if (p -> data >= val && p -> next -> data <= val)
{
return pos;
}
p = p -> next;
pos ++;
}
return pH -> length;
}
void
Print (pHead pH, pNode p)
{
if (p -> next != NULL)
{
Print (pH, p -> next);
}
printf ("%d ", p -> data);
}