Description
A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. One night she adds up the street numbers of the houses she passes (excluding her own). The next time she walks the other way she repeats this and finds, to her astonishment, that the two sums are the same. Although this is determined in part by her house number and in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses should exhibit it.
Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number):
6 8
35 49
Input
There is no input for this program.
Output
Output will consist of 10 lines each containing a pair of numbers, in increasing order with the last number, each printed right justified in a field of width 10 (as shown above).
Sample Input
Sample Output
6 8
35 49
题意:一条街道上有n户人家,按顺序编号,程序员家编号为m,他向左走到头和向右走到头的编号和相等,求前十个m,n,按规定格式输出
思路:
1到m-1和m+1到n分别为两个等差序列,它两个的和相等化简后得
2m^2= n ^2+n;
配方得:8m^2 = (2n+1) ^2-1;
移项得: (2n+1) ^ 2+8 m^2 =1;
由此看出是佩尔方程,x1=3,y1=1;
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int x1=3,y1=1;
void go(int x,int y,int n)
{
if(n<=0)
return;
int a,b;
a=x*x1+8*y*y1;
b=x*y1+y*x1;
printf("%10d%10d\n",b,(a-1)/2);
go(a,b,n-1);
}
int main()
{
go(3,1,10);
}