《算法笔记》2.7小节——C/C++快速入门->指针

问题 A: C语言10.1

题目

题目描述

输入 a 和 b 两个整数,按先大后小的顺序输出 a 和 b。注意请使用指针变量的方式进行比较和输出。

输入

两个用空格隔开的整数 a 和 b。

输出

按先大后小的顺序输出 a 和 b,用空格隔开。 请注意行尾输出换行。

样例输入

1
5 9

样例输出

1
9 5

题解

思路

跟着书上的代码来就好,先声明指针变量,然后给指针变量赋值,再交换就好。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

int main()
{
int a, b, *p, *q;
scanf("%d %d", &a, &b);
p = &a, q = &b;
int temp = *p;
*p = *q;
*q = temp;
printf("%d %d\n", a, b);

system("pause");
return 0;
}

问题 B: C语言10.2

题目

题目描述

输入 a、b、c 三个整数,按先大后小的顺序输出 a、b 和 c。注意请使用指针变量的方式进行比较和输出。

输入

三个用空格隔开的整数 a、b和 c。

输出

按先大后小的顺序输出 a、b 和 c,用空格隔开。 请注意行尾输出换行。

样例输入

1
9 0 10

样例输出

1
10 9 0

题解

思路

思路跟上一题差不多。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

int main()
{
int a, b, c, temp, *p1, *p2, *p3;
scanf("%d %d %d", &a, &b, &c);
p1 = &a, p2 = &b, p3 = &c;

if (c <= b)
{
swap(*p3, *p2);
}
if (c <= a)
{
swap(*p1, *p3);
}
if (b <= a)
{
swap(*p1, *p2);
}

printf("%d %d %d", c, b, a);

system("pause");
return 0;
}

问题 C: C语言10.10

题目

题目描述

给定字符串定义char *a = “I love China!”,读入整数 n,输出在进行了a = a + n这个赋值操作以后字符指针 a 对应的字符串。

输入

一个整数 n,保证 0 <= n < 13.

输出

输出进行了题目描述中赋值操作之后 a 对应的字符串。 请注意行尾输出换行。

样例输入

1
7

样例输出

1
China!

题解

思路

直接a = a + n即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

int main()
{
char *a = "I love China!";
int n;
scanf("%d", &n);
a = a + n;
printf("%s\n", a);

system("pause");
return 0;
}

问题 D: C语言10.15

题目

题目描述

输入 3 个字符串,按从小到大的顺序输出。要求使用指针的方法进行处理。

输入

3 行,每行一个用字符串。保证每个字符串的长度不超过 20。

输出

按从小到大的顺序输出这 3 个字符串,每个字符串一行。 请注意行尾输出换行。

样例输入

1
2
3
China
CLOCK
deal

样例输出

1
2
3
CLOCK
China
deal

题解

思路

思路很常规。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

int main()
{
char s1[100], s2[100], s3[100];

cin.getline(s1, 100);
cin.getline(s2, 100);
cin.getline(s3, 100);

if (strcmp(s1, s2) > 0)
{
swap(s1, s2);
}
if (strcmp(s1, s3) > 0)
{
swap(s1, s3);
}
if (strcmp(s2, s3) > 0)
{
swap(s2, s3);
}
printf("%s\n", s1);
printf("%s\n", s2);
printf("%s\n", s3);

system("pause");
return 0;
}

问题 E: C语言10.16

题目

题目描述

输入 10 个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。要求用 3 个函数实现,分别为输入 10 个数、进行处理、输出 10 个数。要求使用指针的方法进行处理。

输入

用空格隔开的 10 个整数。

输出

输出进行题目描述操作之后的 10 个整数,每个整数之后输出一个空格。 请注意行尾输出换行。

样例输入

1
1 5 2 4 9 0 3 8 7 6

样例输出

1
0 5 2 4 6 1 3 8 7 9 

题解

思路

很简单的代码,使用上方的交换即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>

using namespace std;

void input(int a[])
{
for (int i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
}

void operate(int a[])
{
int min = INT16_MAX, max = INT16_MIN, *p1, *p2;
p1 = &max, p2 = &min;
for (int i = 0; i < 10; i++)
{
if (a[i] > *p1)
{
p1 = &a[i];
}
if (a[i] < *p2)
{
p2 = &a[i];
}
}
int temp = *p1;
*p1 = a[9];
a[9] = temp;
temp = *p2;
*p2 = a[0];
a[0] = temp;
}

void output(int a[])
{
for (int i = 0; i < 10; i++)
{
i == 9 ? printf("%d\n", a[i]) : printf("%d ", a[i]);
}
}

int main()
{
int a[10];
input(a);
operate(a);
output(a);

system("pause");
return 0;
}

《算法笔记》2.7小节——C/C++快速入门->指针
https://excelius.xyz/《算法笔记》2-7小节——c-c-快速入门-指针/
作者
Ther
发布于
2021年11月2日
许可协议