PKU openjudge 1046 Square Number 解题报告

题意:给定正整数b,求最大的整数a,满足a*(a+b) 为完全平方数

解题思路:假设  a^2+a*b = (a+t)^2    -> a^2 + a*b = a^a + 2*a*t +t^2   ->  a*b = 2*a*t +t^2 -> a = t^2/(b-2*t);

因为 a = t^2/(b-2*t);  易知( t >= 0 && t < b/2)

假如 可知  在t的范围内 a 随 t 的增大而增大 ,又因为 a 必须是 整数

所以(1) b为奇数的时候  t = (b-1)/2;

(2) b 为 偶数 且 (b-2)/2 为偶数  ,那么t = (b-2)/2

(3)b 为 偶数且(b-2)/2 为奇数,那么 t = (b - 4)/2

解题代码:

// File Name: a.c
// Author: darkdream
// Created Time: 2013年06月03日 星期一 16时04分55秒

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

int main(){

   //freopen("/home/plac/problem/input.txt","r",stdin);
   //freopen("/home/plac/problem/output.txt","w",stdout);
   int n ;
   scanf("%d",&n);
   while(n--)
   {
     long long b; 
     scanf("%lld",&b);
     if(b %2 == 1 )
     {
        printf("%lld\n",(b-1)/2*(b-1)/2);
     }
     else
     {
        if((b-2)/2 %2 == 1)
        {
          printf("%lld\n",(b-4)/4*(b-4)/4);
        }
        else
            printf("%lld\n",(b-2)/2*(b-2)/2/2);
     
     }
   }
return 0 ;
}
View Code

 

 

 

 

转载于:https://www.cnblogs.com/zyue/archive/2013/06/03/3115741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值