0
点赞
收藏
分享

微信扫一扫

@实验三引用、结构体、指针

追梦人的自留地 2022-04-01 阅读 62
c++
#include<iostream>
using namespace std;
 
void swap (int &x, int &y) 
{
	int t = x;
	x = y;
	y = t;
	
}
 
int main ()
{
	int t;
	cin >> t;
	while(t--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		
		if ( a <= b) swap ( a, b);
		if ( a <= c) swap ( a, c);
		if ( b <= c) swap ( b, c);
		
		cout << a << " " << b << " " << c << " " << endl;
	}
	
}

 

#include<iostream>
#include<algorithm>
using namespace std;
 
struct student
{
	int year;
	int month;
	int day;
};

bool compare (const student&a, const student&b)
{
	int bth1 = a.year * 10000 + a.month * 100 + a.day;
	int bth2 = b.year * 10000 + b.month * 100 + b.day;
	return bth1 < bth2;
}

int main ()
{
	int t;
	cin >> t;
	student *p = new student [t];
	for(int i = 0; i < t; i++)
	{
		cin >> p[i].year >> p[i].month >> p[i].day;
	}
	sort(p, p + t, compare);
	
	cout << p[1].year << '-' << p[1].month <<'-' <<  p[1].day << endl;
}
#include<iostream>
#include<algorithm>
using namespace std;

struct test 
{
	int num;
	char ans1[100];
	char ans2[100];
	char ans3[100]; 
};

char check (char * pa, char * pb)
{
	int same = 0, all = 0;
	while (*pa != '\0' && *pb != '\0')
	{
		all++;
		if( *pa == *pb)  same++;//=============!!!!!!!!!!!!!!!!!!!!!!
		pa++;
		pb++;
	}
	return same >= 0.9*all;//如果return后面的表达式为真,则函数返回1,反之返回0 (这样用函数的话函数不能是void 
} 
int cmp (test *a, test *b)//因为传入的是结构体,所以是test
{
	//定义指针*pa指引每个学生的三个答案 
	char *pa = a ->ans1, *pb = b ->ans1; //a->ans1相当于 test.ans1,只不过是用指针*a访问罢了
	if ( check ( pa, pb) ) return 1;//把答案的第一个字符传入函数 
	pa = a ->ans2, pb = b ->ans2;
	if ( check ( pa, pb) ) return 2; 
	pa = a ->ans3, pb = b ->ans3;
	if ( check ( pa, pb) ) return 3; 
	return 0;
}

int main ()
{
	int t;
	cin >> t;
	test *p = new test [t];//一个指针操作多个数组 
	for (int i = 0; i < t; i++)
	{
		cin >> p[i].num >> p[i].ans1 >> p[i].ans2 >> p[i].ans3;
	}
	
	for (int i = 0; i < t - 1; i++) //嵌套循环:第1个学生分别和2、3、4、5学生比较第一个循环结束,再换第二个学生 
	{
		for (int j = i + 1; j < t; j++)
		{
			int tihao = cmp (p + i, p + j);//将学生对应的结构体传入 
			if ( tihao != 0) cout << p[i].num << ' ' << p[j].num << ' ' << tihao << endl;
		}
	}
}

 

举报

相关推荐

0 条评论