PAT (Basic Level) Practice (中文)C/C++练习(二)20分整理

PTA(Basic Level) Practice (中文):https://pintia.cn/problem-sets/994805260223102976/problems

目录

1002 写出这个数 (20 分)                    1004 成绩排名 (20 分)                    1023 组个最小数 (20 分)

1092 最好吃的月饼 (20 分)                1093 字符串A+B (20 分)


1002 写出这个数 (20 分)

#include

using namespace std;

#define MAX 100

int main()
{
	int i = 0, sum = 0;
	int Ge = 0, Shi = 0, Bai = 0;
	char num[MAX + 1];
	char PingYing[][5] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
	cin >> num;
	while (num[i] != '\0')
	{
		sum += (int)(num[i]-'0');
		i++;
	}
	if (sum >= 100)	//三位数
	{
		Bai = sum / 100;
		Shi = sum / 10 % 10;
		Ge = sum % 10;
		cout << PingYing[Bai] << " " << PingYing[Shi] << " " << PingYing[Ge];
	}
	else if (sum >= 10)	//两位数
	{
		Shi = sum / 10;
		Ge = sum % 10;
		cout << PingYing[Shi] << " " << PingYing[Ge];
	}
	else if (sum >= 0)	//个位数		
	{
		Ge = sum % 10;
		cout << PingYing[Ge];
	}
	else
		return -1;
	

	return 0;
}

1004 成绩排名 (20 分)

#include
using namespace std;

#define MAX 1000
typedef struct
{
	char name[11];
	char id[11];
	int grade;
}Student;
int main()
{
	Student stu[MAX];
	int n, max = 0, min = 0, Imax = 0, Imin = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> stu[i].name;
		cin >> stu[i].id;
		cin >> stu[i].grade;
	}
	min = max = stu[0].grade;
	for (int i = 0; i < n; i++)
	{
		if (max < stu[i].grade)
		{
			max = stu[i].grade;
			Imax = i;
		}
		if (min > stu[i].grade)
		{
			max = stu[i].grade;
			Imin = i;
		}
	}

	cout << stu[Imax].name << " " << stu[Imax].id << endl;
	cout << stu[Imin].name << " " << stu[Imin].id;

	return 0;
}

1023 组个最小数 (20 分)

#include
#include
#define N 10
int main()
{
	int num[11] = { 0 };
	int finalnum[51] = { 0 };
	int t = 0, count = 0, sum = 0;
	int i = 0, j = 0, k = 0;
	for (i = 0; i < N; i++)
	{
		scanf("%d", &num[i]);
		sum += num[i];
		for (k = 0; k < num[i]; k++)
		{
			finalnum[count] = i;
			count++;
		}
	}

	for (i = 0; i < sum - 1; i++)
	{
		for (j = i + 1; j < sum; j++)
		{
			if (finalnum[i] > finalnum[j])
			{
				t = finalnum[i];
				finalnum[i] = finalnum[j];
				finalnum[j] = t;
			}
		}
	}
	for (i = 0; i < sum; i++)
	{
		if (finalnum[i] != 0)
		{
			t = finalnum[i];
			finalnum[i] = finalnum[0];
			finalnum[0] = t;
			break;
		}
	}
	for (i = 0; i < sum; i++)
	{
		printf("%d", finalnum[i]);
	}
	return 0;
}

1092 最好吃的月饼 (20 分)

#include

#define MAXSIZE 1005
int main(void)
{
	int M = 0, N = 0, q = 0;
	long count[MAXSIZE] = { 0 }, max = 0, t = 0;
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++)
	{
		for (int j = 1; j <= N; j++)
		{
			scanf("%ld", &t);
			count[j] += t;
		}
	}
	max = count[0];
	for (int i = 1; i <= N; i++)
	{
		if (max < count[i])
		{
			max = count[i];
		}
	}
	printf("%ld\n", max);
	for (int i = 1; i <= N; i++)
	{
		if (max == count[i])
		{
			if (q == 0)printf("%d", i);
			else printf(" %d", i);
			q++;
		}
	}
	return 0;
}

1093 字符串A+B (20 分)

#include
#include
#include

#define MAXSIZE 3000000

int main()
{
	char str1[MAXSIZE];
	char str2[MAXSIZE];
	int flag[200] = { 0 };
	gets(str1);
	gets(str2);

	strcat(str1, str2);

	for (long i = 0; str1[i] != '\0'; i++)
	{
		if (flag[str1[i]] == 0)
		{
			putchar(str1[i]);
			flag[str1[i]] = 1;
		}
	}
	return 0;
}

/*参考浙大学姐柳婼代码:https://www.liuchuo.net/
https://www.liuchuo.net/archives/category/code
#include 
using namespace std;
int main() {
	string s1, s2, s;
	int hash[200] = { 0 };
	getline(cin, s1);
	getline(cin, s2);
	s = s1 + s2;
	for (int i = 0; i < s.size(); i++) {
		if (hash[s[i]] == 0) cout << s[i];
		hash[s[i]] = 1;
	}
	return 0;
}
*/
/*自己最开始写的,最后一条测试时运行超时
#include
#include
#include

#define MAXSIZE 3000000

int main()
{
	char str1[MAXSIZE];
	char str2[MAXSIZE];
	char replace = 1;
	gets(str1);
	gets(str2);

	strcat(str1, str2);
	//puts(str1);
	for (long i = 0; str1[i] != '\0'; i++)
	{
		for (long j = 0; j < i; j++)
		{
			if (str1[i] == str1[j])
			{
				str1[i] = replace;
				break;
			}
		}
		if (str1[i] != replace)
		{
			putchar(str1[i]);
		}
	}
	return 0;
}
*/

你可能感兴趣的:(C/C++)