4.2 KiB
4.2 KiB
title | date | tags |
---|---|---|
第七次作业(答案) | 2020-11-27 10:53:14 |
交换宏
#include <stdio.h>
#define SWAP(a, b) \
{ \
typeof(a) _t = a; \
a = b; \
b = _t; \
}
int main()
{
int a, b;
printf("请输入a和b:\n");
printf("a = ");
scanf("%d", &a);
printf("b = ");
scanf("%d", &b);
SWAP(a, b);
printf("\n交换后的结果:\na = %d\nb = %d\n", a, b);
return 0;
}
排序与查找
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LEN 10
void swap(int *a, int i, int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
void bubble_sort(int *a, int n)
{
int done = 0;
for (int i = 0; i < n - 1; ++i) {
if (done)
return;
done = 1;
for (int j = 0; j < n - 1 - i; ++j) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
done = 0;
}
}
}
}
void rand_array(int *a, int n)
{
for (int i = 0; i < n; ++i) {
int tmp = rand() % 20 + 1;
a[i] = tmp;
}
}
void print_array(int *a, int n)
{
for (int i = 0; i < n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
void insert(int *a, int n, int insIndex, insValue)
{
for (int j = n - 1; j >= insIndex; ++j) {
a[j + 1] = a[j];
}
a[insIndex] = insValue;
}
void binary_search(int *a, int n, int toSearch)
{
int found = 0;
int l = 0;
int r = n - 1;
while (l <= r) {
int c = l + (r - l) / 2;
if (toSearch < a[c]) {
r = c - 1;
} else if (toSearch > a[c]) {
l = c + 1;
} else {
found = 1;
}
}
if (!found) {
insert(a, n, l, toSearch);
}
}
int main()
{
srand(time(0));
int a[LEN];
printf("使用随机数填充数组:\n");
rand_array(a, LEN);
print_array(a, LEN);
bubble_sort(a, LEN);
printf("排序过后的数组:\n");
print_array(a, LEN);
return 0;
}
P219-T4
#include <stdio.h>
void LeftRotate(int* a, int n, int m)
{
int j = 0;
int oldValue = a[0];
int newValue;
for (int i = 0; i < n; ++i) {
int k = (j + m) % n;
newValue = a[k];
a[k] = oldValue;
oldValue = newValue;
j = k;
}
}
int main()
{
int a[100];
int m, n;
printf("请输入n: \nn = ");
scanf("%d", &n);
printf("请输入m: \nm = ");
scanf("%d", &m);
printf("请输入n个数:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
}
printf("经过移动后的数组为:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
P291-T5
#include <stdio.h>
int find_next(int* a, int n, int i)
{
for (int j = i + 1; j < n; ++j) {
if (a[j] == 1) {
return j;
}
}
for (int j = 0; j < i; ++j) {
if (a[j] == 1) {
return j;
}
}
return -1;
}
int Search(int* a, int n)
{
int num = 0;
int count = 0;
int j = 0;
while (count < n - 1) {
if (num == 2) {
a[j] = 0;
num = 0;
count++;
} else {
num++;
}
j = find_next(a, n, j);
}
return j + 1;
}
int main()
{
int a[100];
int n;
printf("请输入n:\nn = ");
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
a[i] = 1;
}
printf("剩下的是第%d个人。\n", Search(a, n));
return 0;
}
P291-T9
#include <stdio.h>
void Trans(int (*a)[3])
{
for (int i = 1; i < 3; ++i) {
for (int j = 0; j < i; ++j) {
int tmp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tmp;
}
}
}
int main()
{
int a[3][3];
printf("请输入3x3矩阵的9个元素:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
scanf("%d", &a[i][j]);
}
}
Trans(a);
printf("转置后:\n");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}