杭电 OJ2050-2059
写在前面
本文记录了刷杭电 OJ2050-2059 的过程和一些想法,代码仅供参考!
2050 折线分割平面
Problem Description
我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是 n 条折线分割平面的最大数目。比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成 7 部分,具体如右所示。
Input
输入数据的第一行是一个整数 C,表示测试实例的个数,然后是 C 行数据,每行包含一个整数 n(0<n<=10000),表示折线的数量。
Output
对于每个测试实例,请输出平面的最大分割数,每个实例的输出占一行。
Sample Input
2 1 2
Sample Output
2 7
解题思路
先来看看 n 条相交的直线最多能把平面分割成几块:当添加第 n 条直线时,为了使平面最多,则第 n 条直线要与前面 n-1 条直线都相交
对折线来说,分割平面的个数 = 交点个数 顶点个数 1 令 f (n-1) 为前 n-1 条折线分割的平面数,当添加第 n 条折线时。 因为每一条边与前 n-1 条折线的两条边都相交,故增加的交点数为 22(n-1),顶点增加 1,故 f(n) = f(n - 1) 4(n - 1) 1 可以得出公式 f (n) = 2n2 - n 1(直接用上面的递归式计算也可)
参考源码
代码语言:javascript复制#include <iostream>
using namespace std;
int main() {
int c, n;
cin >> c;
while (c--) {
cin >> n;
cout << 2 * n * n - n 1 << endl;
}
return 0;
}
2051 Bitset
Problem Description
Give you a number on base ten,you should output it on base two.(0 < n < 1000)
Input
For each case there is a postive number n on base ten, end of file.
Output
For each case output a number on base two.
Sample Input
1 2 3
Sample Output
1 10 11
解题思路
题目大意:将十进制转换成二进制 整除取余
参考源码
代码语言:javascript复制#include <iostream>
using namespace std;
int main() {
int n, i, r[10];
while (cin >> n) {
for (i = 0; n != 0; i ) {
r[i] = n % 2;
n /= 2;
}
for (i = i - 1; i >= 0; i--) cout << r[i];
cout << endl;
}
return 0;
}
2052 Picture
Problem Description
Give you the width and height of the rectangle,darw it.
Input
Input contains a number of test cases.For each case ,there are two numbers n and m (0 < n,m < 75) indicate the width and height of the rectangle.Iuput ends of EOF.
Output
For each case,you should draw a rectangle with the width and height giving in the input. after each case, you should a blank line.
Sample Input
3 2
Sample Output
±– | | | | ±–
解题思路
题目大意:打印矩形 按照要求按行打印即可
参考源码
代码语言:javascript复制#include <iostream>
using namespace std;
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
cout << " ";
for (int i = 0; i < n; i ) cout << "-";
cout << " " << endl;
for (int i = 0; i < m; i ) {
cout << "|";
for (int j = 0; j < n; j ) {
cout << " ";
}
cout << "|" << endl;
}
cout << " ";
for (int i = 0; i < n; i ) cout << "-";
cout << " " << endl << endl;
}
return 0;
}
2053 Switch Game
Problem Description
There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ). 有一些灯排成一条直线。所有的灯在刚开始都是关闭的,在对灯进行一系列操作后:在第 i 次操作的时候, 调整所有标号是 i 的倍数的灯的状态(原本打开的灯将它关闭,原本关闭的将它打开)。
Input
Each test case contains only a number n ( 0< n<= 10^5) in a line.
Output
Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on).
Sample Input
1 5
Sample Output
1 0
Hint hint Consider the second test case: The initial condition : 0 0 0 0 0 … After the first operation : 1 1 1 1 1 … After the second operation : 1 0 1 0 1 … After the third operation : 1 0 0 0 1 … After the fourth operation : 1 0 0 1 1 … After the fifth operation : 1 0 0 1 0 … The later operations cannot change the condition of the fifth lamp any more. So the answer is 0.
解题思路
题目大意:有一些灯排成一条直线。所有的灯在刚开始都是关闭的,在对灯进行一系列操作后:在第 i 次操作的时候,调整所有标号是 i 的倍数的灯的状态(原本打开的灯将它关闭,原本关闭的将它打开)。 可以数变化的次数,然后奇偶数判断 另外也可以判断输入的数是否为完全平方数
参考源码
代码语言:javascript复制// 方法一
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
int c = 0;
for (int i = 1; i <= n; i ) {
if (n % i == 0) {
c ; //变化的次数
}
}
cout << (c & 1) << endl; //奇数次变化则为1,偶数次变化即为2
}
return 0;
}
// 方法二
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
if ((int)sqrt(n) * (int)sqrt(n) == n)
cout << "1" << endl;
else
cout << "0" << endl;
}
return 0;
}
2054 A == B ?
Problem Description
Give you two numbers A and B, if A is equal to B, you should print “YES”, or print “NO”.
Input
each test case contains two numbers A and B.
Output
for each case, if A is equal to B, you should print “YES”, or print “NO”.
Sample Input
1 2 2 2 3 3 4 3
Sample Output
NO YES YES NO
解题思路
题目大意:比较两个数是否相等 注意数字可能很大,需要用字符串存储,另外注意小数点后无效 0 的处理
参考源码
代码语言:javascript复制#include <cstring>
#include <iostream>
using namespace std;
void cut(char a[]) {
int len = strlen(a);
if (strchr(a, '.')) { //是否有小数点
for (int i = len - 1; a[i] == '0'; i--) { //去除多余0
a[i] = '