2023.3.12上机练习

1.201312-1原题网址
2023.3.12上机练习_第1张图片
(我的80分代码)

#include
#include
using namespace std;
int num[10000] = { 0 };
int main()
{
	int n;
	cin >> n;
	int count = 1;
	for (int i = 1; i <= n; i++)
		cin >> num[i];
	sort(num, num + n);
	int max = 0,c;
	for (int i = 2; i <= n; i++)
	{
		if (num[i] == num[i - 1])
		{
			count++;
			if (max < count)
			{
				max = count;
				c = num[i];
			}
		}
		else
			count = 1;
	}
	cout << c << endl;
	return 0;
}

(满分代码)

#include
using namespace std;
int a[1000], b[1000];
int main()
{
    int n;
    cin >> n;
    int i;
    for (i = 0; i < n; i++)
    {
        cin>>a[i];
    }
    int count = 0, j, max = 0, min = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (a[i] == a[j])//记录该数出现次数
                count++;
        }
        if (max < count) {//找出出现次数最多的数
            max = count;
            min = a[i];
        }
        if (max == count && a[i] < min)
        {//出现次数相同的数,找出其中最小的数
            min = a[i];
        }
        count = 0;//记录完重置
    }
    printf("%d\n", min);
    return 0;
}

2.201312-2原题网址
2023.3.12上机练习_第2张图片

#include
#include
using namespace std;
int main()
{
	string a;
	getline(cin, a);
	int num = (a[0] - '0') * 1 + (a[2] - '0') * 2 + (a[3] - '0') * 3 + (a[4] - '0') * 4 + (a[6] - '0') * 5 + (a[7] - '0') * 6 + (a[8] - '0') * 7 + (a[9] - '0') * 8 + (a[10] - '0') * 9;
	num = num % 11;
	if (num == 10&&a[12]=='X')
	{
		printf("Right");
	}
	else if(num==10)
	{
		a[12] = 'X';
		for (int i = 0; i < 13; i++)
			cout << a[i];
	}
	else if (num == (a[12] - '0'))
		printf("Right");
	else
	{
		a[12] = num + '0';
		for (int i = 0; i < 13; i++)
			cout << a[i];
	}
	return 0;
}

3.201312-3原题网址
2023.3.12上机练习_第3张图片

#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	int max = 0;
	int num[1000] = { 0 };
	for (int i = 1; i <= n; i++)
	{
		cin >> num[i];
		if (num[i] > max)
			max = num[i];
	}

	for (int i = 1; i <= n; i++)
	{
		int min = num[i], count = 1;
		for (int j = i + 1; j <= n; j++)
		{
			count++;
			if (num[j] < min)
				min = num[j];
			if (count * min > max)
				max = count * min;
		}
	}
	cout << max;
	return 0;
}

4.201403-1原题网址
2023.3.12上机练习_第4张图片

#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	int num[500] = { 0 };
	for (int i = 1; i <= n; i++)
		cin >> num[i];
	int count = 0;
	for (int i = 1; i <= n; i++)
	{
		for (int j = i + 1; j <= n; j++)
		{
			if (num[i] + num[j] == 0)
				count++;
		}
	}
	cout << count << endl;
	return 0;
}

5.201409-1原题网址
2023.3.12上机练习_第5张图片

#include
#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	int num[1000] = { 0 };
	for (int i = 0; i < n; i++)
		cin >> num[i];
	sort(num,num + n);
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (num[j] - num[i] == 1)
				count++;
		}
	}
	cout << count << endl;
	return 0;
}
  1. 201409-2原题网址
    2023.3.12上机练习_第6张图片
#include 
#include 
#include 
using namespace std;
const int N = 110;
int n;
bool m[N][N];
int main()
{
    cin >> n;
    while (n--)
    {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        for (int i = x1; i < x2; i++)
        {
            for (int j = y1; j < y2; j++)
            {
                m[i][j] = true;
            }
        }
    }
    int res = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            res += m[i][j];
        }
    }
    cout << res;
    return 0;
}

你可能感兴趣的:(算法,c++,图论)