zoukankan      html  css  js  c++  java
  • 1079 三角形

    1079 三角形

    时间限制:500MS  内存限制:65536K
    提交次数:283 通过次数:82

    题型: 编程题   语言: G++;GCC

     

    Description

    著名的数学家毕达哥拉斯可能从来都不曾想过有人居然会问他这样的一个问题:给出一个整数,存在多少个直角三角形,
    它的某一条边的长度等于这个整数,而且其他边的长度也是整数。既然毕达哥拉斯不可能预见到有计算机的出现,
    如果他回答不出来,那谁又能责怪他呢?但是现在既然你有了计算机,那么回答不出来就说不过去了。




    输入格式

     第一行有一个整数n,代表有多少个数据(1<=n<=20)。接下来有n行,每行代表一个数据。一个数据就是一个整数ai(a<=i<=n,1<=ai<=100)。



    输出格式

    每个数据都必须有相应的输出。两个数据的输出之间有一个空行。最后一个测试数据的输出后不要加空行。
    对于每一个数据,如果找不到解,则输出一个空行。如果找到解,就把符合条件的所有直角三角形输出。每个三角形占一行,输出该三角形的另外两条边,必须先输出长边,然后一个逗号,再输出短边。两个三角形之间不能有空行,而且必须按照长边降序排列。
    



     

    输入样例

    2
    20
    12
    



     

    输出样例

    101,99
    52,48
    29,21
    25,15
    16,12
    
    37,35
    20,16
    15,9
    13,5
    



     

    作者

     admin

      SCAU-1079 三角形—暴力枚举。 当输入的边a为直角三角形的斜边时,大家应该都想的到,但当a为直角边时可能就没那么简单了,因为斜边可以无限长。题目中给出了一个限制条件:所有边都必须为整数。所以,这就是突破口。限制斜边的枚举次数的就是这个条件,但这个过程需要一点点的数学推导。

    由勾股定理得:a^2=y^2-x^2;  所以有:a^2=(y-x)*(y+x); 于是得:a^2/(y-x)=(y+x);因为y和x都必须为整数,则y-x>=1; 故有:y+x<=a^2;  这个就是斜边y的限制条件:将x从1开始枚举,用a和x求出y;随着x增长,y也是逐渐增长的  所以每次的迭代里都判断一次x+y是否比a^2大。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cctype>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <stack>
    12 #include <utility>
    13 #include <vector>
    14 #define ll long long
    15 #define inf 0x3f3f3f3f
    16 using namespace std;
    17 
    18 typedef struct node
    19 {
    20     int changbian;
    21     int duanbian;
    22 }node;
    23 node m[5000];
    24 int len;
    25 bool cmp(node a,node b)//sort()的算子。另changbian大的排前面
    26 {
    27     return a.changbian>b.changbian;
    28 }
    29 int main()
    30 {
    31     //freopen("input.txt","r",stdin);
    32     int n;
    33     scanf("%d",&n);
    34     while(n--)
    35     {
    36         double ty;
    37         int a,x,y;
    38         scanf("%d",&a);
    39         //下面是当a为斜边的时候
    40         len=0;
    41         for(x=1;x<=sqrt(a*a/2);x++)
    42         {
    43             ty=sqrt((double)a*a-(double)x*x);
    44             y=(int)ty;
    45             if(y==ty)
    46             {
    47                 m[len].changbian=y;
    48                 m[len++].duanbian=x;
    49             }
    50         }
    51         //当a为直角边的时候
    52         for(x=1;;x++)
    53         {
    54             ty=sqrt((double)a*a+(double)x*x);
    55             if(ty+x>a*a)  //超过条件y+x<=a^2的上界,退出循环
    56                 break;
    57             y=(int)ty;
    58             if(y==ty)
    59             {
    60                 m[len].changbian=y;
    61                 m[len++].duanbian=x;
    62             }
    63         }
    64         //将存储的边按长边进行排序
    65         sort(m,m+len,cmp);
    66         for(int i=0;i<len;i++)
    67             printf("%d,%d
    ",m[i].changbian,m[i].duanbian);
    68         putchar('
    ');
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    UVA
    UVA
    模板——扩展欧几里得算法(求ax+by=gcd的解)
    UVA
    模板——2.2 素数筛选和合数分解
    模板——素数筛选
    Educational Codeforces Round 46 (Rated for Div. 2)
    Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
    Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
    Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
  • 原文地址:https://www.cnblogs.com/geek1116/p/5551407.html
Copyright © 2011-2022 走看看