**第一题:**大家去电影院看电影,总共有 n 人来看电影,其中年龄不低于 18 岁的成年 人的座位号为奇数,不满 18 岁的未成年人的座位号为偶数。现在请统计成年人 与未成年的数目,以及他们在总人数里的比例。n<=1000。
以下是输入输出实例:
样例输入:5 2 3 6 7 11 //这里的整数都是座位号
样例输出:3 0.60 2 0.40
接下来是代码:
#include<cstdio>
using namespace std;
int main()
{
int n, count1, count2, a[1001];
double per1, per2;
count1 = count2 = 0;
cin >> n;
while (n)
{
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] % 2 == 0)
count1++;
else
count2++;
}
per1 = (double)count1/n;
per2 = (double)count2/n;
printf("%d ", count1);
printf("%.2lf ", per1);
printf("%d ", count2);
printf("%.2lf\n", per2);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
第二题:``(也是力扣leetcode上面的一题)给定n个非负整数a1,a2,…,an,每个数代表坐标中的一个点(i,ai) 。在坐标 内画n 条垂直线,垂直线 i的两个端点分别为(i,ai) 和 (i,0)。找出其中的两条线, 使得它们与x轴共同构成的容器可以容纳最多的水。 说明:你不能倾斜容器,且n的值至少为 2。 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。
在此情况下,容器能够容纳水(表 示为蓝色部分)的最大值为49。
#include<cstdio>
using namespace std;
int main()
{
int n, i, a[1001], j, s, max;
cin >> n;
while (n && n <= 1000)
{
for (i = 0; i < n; i++)
cin >> a[i];
max = 0;
for(i=0;i<n;i++)
for (j = i + 1; j < n; j++)
{
if (a[i] > a[j])
s = a[j] * (j - i);
else s = a[i] * (j - i);
if (s > max)
max = s;
}
printf("%d\n", max);
}
return 0;
}
此处仅会暴力算法
//////////////////////////////////////////////////////////////////////////////////////////////////////
**第四题:**有个班级,里面有 N 个学生,他们之中有些是朋友有些不是,比如如果 A 是 B 的朋友,B 是 C 的朋友,那么 A 就是 C 的间接朋友,我们定义所谓的朋友圈就是 由直系和间接朋友所组成的群体。N 的范围为 [1,200].
例子 1:
输入:
3
1 1 0
1 1 0
0 0 1
输出:2
#include<cstdio>
using namespace std;
int main()
{
int a[205][205];
int i, j, count, max, n;
while (cin >> n)
{
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> a[i][j];
max = 0;
for (i = 0; i < n; i++)
{
count = 0;
for (j = 0; j < n; j++)
{
count += a[i][j];
}
if (count > max)
max = count;
}
printf("%d\n", n - max + 1);
}
return 0;
}
//这一段代码是不会图的时候想的,虽然答案也可以运行出来,但也可能有bug,贴在此处的目的是想找人一起探讨此代码是否可行。
接下来是图的代码:
#include <iostream>
using namespace std;
#include<string.h>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include <sstream>
#include<queue>
int main() {
int i, j, n, num;
int a[201][201], vis[201];
while (cin >> n)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
cin >> a[i][j];
vis[i] = 0;//初始化节点数组
}
num = 0;
for (i = 0; i < n; i++)
{
if (vis[i] == 0)
{
num++;
queue<int> q;
vis[i] = 1;
q.push(i);
while (!q.empty())
{
int p = q.front();
q.pop();
for (j = i+1; j < n; j++)//学长给的答案是j从0开始,其实我觉得可以从i+1开始,因为是对称矩阵
{
if (vis[j] == 0 && a[p][j] == 1)
{
vis[j] = 1;
q.push(j);
}
}
}
}
}
printf("%d\n", num);
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
19年复试题目记录完成,这篇文章的目的是为了记录自己的学习过程,还有想沟通学习。希望这篇文章可以帮到需要的人。另外,学长的博客是:逃离地球的小小呆