2010.计算机院.Problem A.比较奇偶数个数
链接: 牛客网 - 比较奇偶数个数
题目大意(回忆版):
第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。
Sample:
Input:
5
1 2 3 4 5
Output:
YES
参考代码
#include <iostream>
using namespace std;
int main()
{
int n, num;
cin >> n;
int odd = 0, even = 0;
while( n-- )
{
cin >> num;
if( num%2 )
odd++;
else
even++;
}
if( even <= odd )
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
2010.计算机院.Problem B.找最小数
链接: 牛客网 - 找最小数
题目大意(回忆版)
第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x y。输出一组x y,该组数据是所有数据中x最小,且在x相等的情况下y最小的。
Sample:
Input:
5
3 3
2 2
5 5
2 1
3 6
Output:
2 1
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1010;
struct number
{
int x;
int y;
};
number A[maxn];
bool cmp( number a, number b )
{
if( a.x != b.x )
return a.x < b.x;
else
return a.y < b.y;
}
int main()
{
int n;
cin >> n;
for( int i = 0; i < n; i++ )
cin >> A[i].x >> A[i].y;
sort( A, A+n, cmp );
cout << A[0].x << " " << A[0].y;
return 0;
}
2010.计算机院.Problem C.翻转
链接: 牛客网 - 翻转
题目大意(回忆版)
该题是要翻转数据。首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作数据为以x y为左上角的那几个数据。
操作类型有四种:
1 2 表示:90度,顺时针,翻转4个数
1 3 表示:90度,顺时针,翻转9个数
2 2 表示:90度,逆时针,翻转4个数
2 3 表示:90度,逆时针,翻转9个数
Sample:
Input:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
1 3 1 1
Output:
11 6 1 4 5
12 7 2 9 10
13 8 3 14 15
16 17 18 19 20
21 22 23 24 25
参考代码
#include <iostream>
using namespace std;
int x, y;
int A[5][5];
int temp[3][3];
//4个数 顺时针
void fanzhuan1()
{
temp[0][0] = A[x+1][y];
temp[0][1] = A[x][y];
temp[1][0] = A[x+1][y+1];
temp[1][1] = A[x][y+1];
}
//9个数 顺时针
void fanzhuan2()
{
temp[0][0] = A[x+2][y];
temp[0][1] = A[x+1][y];
temp[0][2] = A[x][y];
temp[1][0] = A[x+2][y+1];
temp[1][1] = A[x+1][y+1];
temp[1][2] = A[x][y+1];
temp[2][0] = A[x+2][y+2];
temp[2][1] = A[x+1][y+2];
temp[2][2] = A[x][y+2];
}
//4个数 逆时针
void fanzhuan3()
{
temp[0][0] = A[x][y+1];
temp[0][1] = A[x+1][y+1];
temp[1][0] = A[x][y];
temp[1][1] = A[x+1][y];
}
//9个数 逆时针
void fanzhuan4()
{
temp[0][0] = A[x][y+2];
temp[0][1] = A[x+1][y+2];
temp[0][2] = A[x+2][y+2];
temp[1][0] = A[x][y+1];
temp[1][1] = A[x+1][y+1];
temp[1][2] = A[x+2][y+1];
temp[2][0] = A[x][y];
temp[2][1] = A[x+1][y];
temp[2][2] = A[x+2][y];
}
void fanzhuan( int op2 )
{
if( op2 == 2 )
for( int i = 0; i < 2; i++ )
for( int j = 0; j < 2; j++ )
A[x+i][y+j] = temp[i][j];
else
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 3; j++ )
A[x+i][y+j] = temp[i][j];
}
int main()
{
for( int i = 0; i < 5; i++ )
for( int j = 0; j < 5; j++ )
cin >> A[i][j];
int op1, op2;
cin >> op1 >> op2 >> x >> y;
x--;
y--;
if( op1 == 1 && op2 == 2 )
fanzhuan1();
else if( op1 == 1 && op2 == 3 )
fanzhuan2();
else if( op1 == 2 && op2 == 2 )
fanzhuan3();
else if( op1 == 2 && op2 == 3 )
fanzhuan4();
fanzhuan( op2 );
for( int i = 0; i < 5; i++ )
{
bool firstCase = true;
for( int j = 0; j < 5; j++ )
{
if( firstCase )
firstCase = false;
else
cout << " ";
cout << A[i][j];
}
cout << endl;
}
return 0;
}
2010.计算机院.Problem D.哈夫曼树
链接: 牛客网 - 哈夫曼树
题目大意(回忆版)
哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。
Sample:
Input:
5
1 2 2 5 9
Output:
37
解释:即生成如下图哈夫曼树,结点1的权值为4,结点2的权值为4,结点2的权值为3,
参考代码
#include<stdio.h>
#include<queue>
using namespace std;
priority_queue<int , vector<int>, greater<int> > Q; //建立一个小顶堆
int main()
{
int n,x;
while( scanf("%d",&n) != EOF )
{
while(Q.empty()==false)
Q.pop();
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
Q.push(x);
}
int ans=0;
while(Q.size()>1)
{
int a=Q.top();
Q.pop();
int b=Q.top();
Q.pop();
ans+=a+b;
Q.push(a+b);
}
printf("%d\n",ans);
}
return 0;
}