码迷,mamicode.com
首页 > 其他好文 > 详细

Street Numbers

时间:2015-08-03 10:05:32      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

                     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).

 

题意:对于连续的数,从有一个数开始,他的前面数字只和与他后面数字只和相等,6,,8是1+2+3+4+5=7+8;

tip:可以根据等差数列化简公式,成x*x-2*y*y=1;再由佩尔方程解

 

 

 1 #include<iostream>
 2 #include<iomanip>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     for(int i=0,x0=3,y0=2,x=x0,y=y0,t;i<10;i++,x=t)
 9     {
10         t=x*x0+2*y*y0;
11         y=x*y0+y*x0;
12         cout<<setw(10)<<y/2<<setw(10)<<(t-1)/2<<endl;
13     }
14     return 0;
15 }

 

Street Numbers

标签:

原文地址:http://www.cnblogs.com/moqitianliang/p/4698554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!