zoukankan      html  css  js  c++  java
  • uva 138 Street Numbers

    感觉就是求1~ n 然后家在中间的k位置,求1~ k-1 的和等于k+1~n

    题目:

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

    代码:

     1 //Thu Jan  2 22:20:48 2014
     2 //Author:Minshik
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <cstdio>
     7 #include <string>
     8 #include <vector>
     9 #include <set>
    10 #include <stack>
    11 #include <queue>
    12 #include <deque>
    13 #include <memory.h>
    14 #include <cctype>
    15 #include <iomanip>
    16 #include <cmath>
    17 using namespace std;
    18 
    19 // k^2 = (n+n^2)/2
    20 int main()
    21 {
    22 
    23     int cnt = 0;
    24     long long n=2;
    25     while(cnt<10)
    26     {
    27         long long d = (n+pow(n,2))/2;
    28         long long c = sqrt(d);
    29         if(  c*c == d  )
    30         {
    31             cout<<setw(10)<<c << setw(10)<<n<<endl;
    32             cnt++;
    33         }
    34         n++;
    35     }
    36 
    37     return 0;
    38 }
  • 相关阅读:
    页面笔记
    RestTemplate
    Spring中使用HibernateCallback
    工作中的小零碎
    hibernate核心思想 体系结构(转)
    db2备份和导入单个表操作
    MSB-STRUTS-课堂笔记
    设计模式:策略模式(转)
    java中的匿名内部类总结(转)
    struts2.0中Action的对象生命周期详解!!(转)
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3502764.html
Copyright © 2011-2022 走看看