当前位置:编程学习 > C/C++ >>

UVA 10790 How Many Points of Intersection?

  How Many Points of Intersection? 

We have two rows. There are a dots on the top row and b dots on the bottom row. We draw line segments connecting every dot on the top row with every dot on the bottom row. The dots are arranged in such a way that the number of internal intersections among the line segments is maximized. To achieve this goal we must not allow more than two line segments to intersect in a point. The intersection points on the top row and the bottom are not included in our count; we can allow more than two line segments to intersect on those two rows. Given the value of a and b, your task is to compute P(a, b), the number of intersections in between the two rows. For example, in the following figure a = 2 and b = 3. This figure illustrates that P(2, 3) = 3.

 

\epsfbox{p10790.eps}


Input
Each line in the input will contain two positive integers a ( 0 < a20000) and b ( 0 < b20000). Input is terminated by a line where both a and b are zero. This case should not be processed. You will need to process at most 1200 sets of inputs.


Output
For each line of input, print in a line the serial of output followed by the value of P(a, b). Look at the output for sample input for details. You can assume that the output for the test cases will fit in 64-bit signed integers.


Sample Input

2 2
2 3
3 3
0 0

Sample Output

Case 1: 1
Case 2: 3
Case 3: 9

有两条线,输入第一条线上点n个,和第二条线上点m个。 输出如果把n,m上每两点都相连,交点有几个。。

解法:加入n有3个,m有3个。假设从m这条线找点去连接m, 第一点连过去3条一个交点都没。第二点连过去。m2连n1的时候将会交第一点连过去的m1n2、m1n3,m2连n2的时候将会交第一点和第二点连过去的m1n3。。为1 + 2;第三点连过去将会交第一点和第二点连过去的几条。为 2 * (1 + 2)。。

找到规律可以推出。。交点个数x为,求出一个sum = (1 + 2 + ... + n - 1)。 然后x = sum * 1 + sum * 2 + ... + m - 1;

输出x。

代码:

 

#include <stdio.h>
#include <string.h>

int a, b;

int main()
{
    int  t = 1;
    while (scanf("%d%d", &a, &b) != EOF && a || b)
    {
	long long num1 = 0;
	long long sum = 0;
	for (int i = 1; i < a; i ++)
	    num1 += i;
	for (int i = 1; i < b; i ++)
	    sum += num1 * i;
	printf("Case %d: %lld\n", t ++, sum);
    }
    return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,