输入题数n 建立两个大小为n的数组ytr和ac,分别存储每道题的验题人数量和AC队伍数量,即ytr[0]为第一题验题人数量,ac[i]为第i+1题AC队伍数量 再建立两个大小为n的指针数组ytrb和acb,分别存储指向每道题验题人代码长度和AC队伍代码长度的数组的首元素的指针。 循环n次,每次输入一个ytr[]和ac[]后再输入ytr[]个验题人代码长度数据和ac[]个AC队伍代码长度数据(分别存入ytrb[][]和acb[][])。 按所求输出格式逐题输出对应ytrb[][]和acb[][]的最小值即可。(写一个最小值函数,参数为数组起点指针和终点指针(最后一个元素的指针)) 最小值函数算法:开一个新变量赋值为首元素,逐个检查后面的元素有无比新变量值小的,有就赋给新变量,没有就什么也不做。 P.S.不要因为函数功能简单就太自信了!!!被最小值函数的“循环漏了个等号”的细节错误卡了一个晚上!!!查错时最好把函数都测试一遍
#include<iostream>
using namespace std;
int min(int* begin, int* end)//找最小值函数,参数是首元素指针和最后一个元素的指针
{
int res=*begin;
for (int i = 1; i <= end - begin; i++)
{
if (res > *(begin + i))
res = *(begin + i);
}
return res;
}
int main()
{
int n;
cin >> n;//输入赛题数量
int* ytr = new int[n];
int* ac = new int[n];
int** ytrb = new int*[n];//一个ytrb[]表示一题验题人代码字节数数组的首元素指针
int** acb = new int*[n];
for (int i = 0; i < n; i++)
{
cin >> ytr[i] >> ac[i];
ytrb[i] = new int[ytr[i]];
for (int j = 0; j < ytr[i]; j++)//输入验题人代码字节数数组
cin >> ytrb[i][j];
if (ac[i] != 0)
{
acb[i] = new int[ac[i]];
for (int j = 0; j < ac[i]; j++)
cin >> acb[i][j];
}
}
for (int i = 0; i < n; i++)
{
cout << "Problem " << i + 1001 << ":\n";
cout << "Shortest judge solution: " << min(&ytrb[i][0], &ytrb[i][ytr[i] - 1]) << " bytes.\n";
if (ac[i] == 0)
cout << "Shortest team solution: N/A bytes.\n";
else
cout << "Shortest team solution: " << min(&acb[i][0], &acb[i][ac[i] - 1]) << " bytes.\n";
}
return 0;
}