poj_1320_Street Numbers

本文介绍了一个有趣的数学和程序设计问题,即寻找一对数,使得从某个数开始,街道两边的房屋编号之和相等。通过观察和分析,作者发现了一种规律,并使用C++实现了这一算法,输出了前几组满足条件的数对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题意 k号房子分开n栋房子,前k-1栋和等于k+1到第n栋。输出k,n。
网上题解大多瞎XX扯X,证明不完整,推导胡说八道直接给出佩尔方程。我直接暴力打印前几组找到了规律
      6         8
        35        49
       204       288
      1189      1681
到这里我就发现了,
204=35*6-6  288=204+35+49
1189=204*6-35  1681=1189+204+288
a[i]=a[i-1]*6-a[i-2]
b[i]=a[i]+a[i-1]+b[i-1]


#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define N 1000010
using namespace std;
typedef long long ll;
int a[11];
int b[11];
int main()
{
   // freopen("dataout.txt","w",stdout);
    a[0]=6;
    a[1]=35;
    b[0]=8;
    b[1]=49;
    printf("%10d%10d\n",6,8);
    printf("%10d%10d\n",35,49);
    for(int i=2;i<10;i++)
    {
        a[i]=a[i-1]*6-a[i-2];
        b[i]=a[i]+a[i-1]+b[i-1];
        int x=a[i],y=b[i];
        printf("%10d%10d\n",a[i],b[i]);
    }

}



转载于:https://www.cnblogs.com/ygtzds/p/7900152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值