• 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
    没有梦想,何谈远方
  • 相关阅读:
    插入排序法
    二分查找
    排序算法
    牛客网 猜数游戏
    决策树及随机森林(笔记)
    knn的缺陷及改进
    区块链、比特币简易PYTHON实现版笔记
    B树,B+树,以及它们和数据库索引之间的关系
    Balanced Binary Tree
    Advantages & Disadvantages of Recursion
  • 原文地址:https://www.cnblogs.com/zyue/p/3115741.html
Copyright © 2020-2023  润新知