C语言基础练习16


1.有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数

#include "stdafx.h"
#include

int main()
{
	void move(int[20], int, int);
	int number[20], n, m, i;
	printf("how many numbers?\n");
	scanf_s("%d", &n);
	printf("input %d numbers:\n", n);
	for (i = 0; i < n; i++)
		scanf_s("%d", &number[i]);
	printf("how many place you want to move?\n");
	scanf_s("%d", &m);
	move(number, n, m);
	printf("Now,they are:\n");
	for (i = 0; i < n; i++)
		printf("%d ", number[i]);
	printf("\n");
    return 0;
}

void move(int array[20], int n, int m)
{
	int*p, array_end;
	array_end = *(array + n - 1);
	for (p = array + n - 1; p > array; p--)
		*p = *(p - 1);
	*array = array_end;
	m -- ;
	if (m > 0) move(array, n, m);
}
运行结果:

C语言基础练习16_第1张图片


2.编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3]

#include "stdafx.h"
#include
#define N 5

struct student
{
	char num[6];
	char name[8];
	int score[3];
}stu[N];

int main()
{
	void print(struct student stu[6]);
	int i, j;
	for (i = 0; i < N; i++)
	{
		printf("\ninput score of student %d:\n", i + 1);
		printf("No.:");
		scanf_s("%s", stu[i].num,6);
		printf("name:");
		scanf_s("%s", stu[i].name,8);
		printf("score:");
		scanf_s("%d%d%d", &stu[i].score[0], &stu[i].score[1], &stu[i].score[2],3);
		printf("\n");
	}
	print(stu);
    return 0;
}

void print(struct student stu[6])
{
	int i, j;
	printf("\n  No.      name     score1     score2     score3\n");
	for (i = 0; i < N; i++)
	{
		printf("%5s%10s", stu[i].num, stu[i].name);
		for (j = 0; j < 3; j++)
			printf("%9d", stu[i].score[j]);
		printf("\n");
	}
}
运行结果:

C语言基础练习16_第2张图片



3.13个人围成一圈,从第1个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子中的人原来的序号。

#include "stdafx.h"
#include
#define N 13

struct person
{
	int number;
	int nextp;
}link[N+1];

int main()
{
	int i, count, h;
	for (i = 1; i <= N; i++)
	{
		if (i == N)
			link[i].nextp = 1;
		else
			link[i].nextp = i + 1;
		link[i].number = i;
	}
	printf("\n");
	count = 0;
	h = N;
	printf("sequence that person leave the circle:\n");
	while (count < N - 1)
	{
		i = 0;
		while (i != 3)
		{
			h = link[h].nextp;
			if (link[h].number)
				i++;
		}
		printf("%4d", link[h].number);
		link[h].number = 0;
		count++;
	}
	printf("\nThe last one is: ");
	for (i = 1; i <= N; i++)
		if (link[i].number)
			printf("%3d", link[i].number);
	printf("\n");
    return 0;
}
运行结果:
C语言基础练习16_第3张图片


你可能感兴趣的:(c语言)