Street Numbers
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 and Output
There is no input for this program. Output will consist of 10 lines each containing a pair of numbers, each printed right justified in a field of width 10 (as shown above).
题意: 简单来说, 求出10组k,n, 满足 1 + 2 +... + k = k + 1 + k + 2 + ... + n;
思路: pell方程的应用,利用前一个解求下一个解求出10组即可。
代码:
#include <stdio.h> #include <string.h> int x, y, x0, y0, t, n; int main() { x = x0 = 3; y = y0 = 2; n = 2; for (int i = 0; i < 10; i ++) { t = x * x0 + n * y * y0; y = x * y0 + y * x0; printf("%10d%10d\n", y / 2, (t - 1) / 2); x = t; } return 0; }
查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索this
, number
, and
The
139、138美容人才网、ip138、138是联通还是移动、138战歌网,以便于您获取更多的相关知识。