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

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

1269 Problem A C语言10.1

#include <stdio.h>
#include <string.h>

int main() {
	int a, b;
	int *p1=&a, *p2=&b;

	scanf("%d %d", p1,p2);
	if (*p1 > *p2) printf("%d %d\n", *p1, *p2);
	else printf("%d %d\n", *p2, *p1);
//	printf("%d %d\n", *p1 > *p2 ? *p1 : *p2, *p1 > *p2 ? *p2 : *p1);

	return 0;
}

1270 Problem B C语言10.2

#include <stdio.h>
#include <string.h>

int main() {
	int a, b, c;
	int *p1 = &a, *p2 = &b, *p3 = &c;

	scanf("%d %d %d", p1, p2, p3);
	if (*p1 < *p2) {
		int *temp = p1;
		p1 = p2;
		p2 = temp;
	}
	if (*p2 < *p3) {
		int *temp = p2;
		p2 = p3;
		p3 = temp;
	}
	if (*p1 < *p2) {
		int *temp = p1;
		p1 = p2;
		p2 = temp;
	}

	printf("%d %d %d\n", *p1, *p2, *p3);
	printf("%d %d %d\n", (*p1 > *p2 ? *p1 : *p2) > *p3 ? (*p1 > *p2 ? *p1 : *p2) : *p3, (*p1 > *p2 ? *p1 : *p2) > *p3 ? ((*p1 > *p2 ? *p2 : *p1) > *p3 ? (*p1 > *p2 ? *p2 : *p1) : *p3) : (*p1 > *p2 ? *p1 : *p2), (*p1 > *p2 ? *p2 : *p1) > *p3 ? *p3 : (*p1 > *p2 ? *p2 : *p1));

	return 0;
}

1278 Problem C C语言10.10

#include <cstdio>
#include <cstring>

int main() {
	char *a = "I love China!";
	int n;
	scanf("%d", &n);
	for (char *p = a + n; p < a + strlen(a); p++) {
		printf("%c", *p);
	}

	return 0;
}

1283 Problem D C语言10.15

#include <cstdio>
#include <cstring>
 
//方法一:指针交换要用引用!用指针别名来交换,否则指针地址交换无效 
/*void swap(char * &a, char * &b)
{
	//交换地址 
	char *temp = a;
	a = b;
	b = temp;
}
*/ 
 
//方法二:交换指针指向的单元的数据
void swap(char *a, char *b)
{
	char temp[20];
	strcpy(temp,a);
	strcpy(a,b);
	strcpy(b,temp);
}
int main()
{
	char a[20],b[20],c[20];
	gets(a);gets(b);gets(c);
	char *s1=a, *s2=b, *s3=c;
	
	//先 1 与 3 比 
	if(strcmp(s1,s3)>0) swap(s1,s3);
	//再 2 3 比 
	if(strcmp(s2,s3)>0) swap(s2,s3);
	//确定s3最大,再判断s1 s2
	if(strcmp(s1,s2)>0) swap(s1,s2); 
	
	puts(s1);puts(s2);puts(s3);
 	
	return 0;
}
#include <cstdio>
#include <cstring>

int main() {
	char a[20], b[20], c[20];
	//gets_s(a); gets_s(b); gets_s(c);
	scanf("%s %s %s", a,b,c);
	char *p1 = a, *p2 = b, *p3 = c;

	if (strcmp(p1,p2)>0) {
		char *temp = p1;
		p1 = p2;
		p2 = temp;
	}
	if (strcmp(p2,p3) > 0) {
		char *temp = p2;
		p2 = p3;
		p3 = temp;
	}
	if (strcmp(p1, p2) > 0) {
		char *temp = p1;
		p1 = p2;
		p2 = temp;
	}
	//puts(p1); puts(p2); puts(p3);
	printf("%s\n%s\n%s", p1, p2, p3);
	return 0;
}

1284 Problem E C语言10.16

#include <cstdio>
#include <cstring>

void getnum(int *);
void trans(int *);
void putnum(int*);
void swap(int *a, int *b);

int main() {
	int p[10];
	getnum(p);
	trans(p);
	putnum(p);

	return 0;
}

void getnum(int *p) {
	for (int i = 0; i < 10; i++) {
		scanf("%d", p+i);
	}
}

void trans(int *p) {
	int max = 0, min = 0;
	for (int i = 1; i < 10; i++) {
		if (*(p+max) < *(p+i)) max = i;
		if (*(p+min) > *(p+i)) min = i;
	}
	swap((p+max), (p+9));
	swap((p+min), p);
}

void putnum(int *p) {
	for (int i = 0; i < 10; i++) {
		printf("%d\n", *(p+i));
	}
}

void swap(int * a, int * b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值