zoukankan      html  css  js  c++  java
  • Counting Triangles(hd1396)

    Counting Triangles

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2506    Accepted Submission(s): 1184


    Problem Description
    Given an equilateral triangle with n the length of its side, program to count how many triangles in it.



    Input
    The length n (n <= 500) of the equilateral triangle's side, one per line.

    process to the end of the file
     
    Output
    The number of triangles in the equilateral triangle, one per line.
     
    Sample Input
    1
    2
    3
     
    Sample Output
    1
    5
    13
      分正三角形和倒三角形,
      当长度为n时,在最低边上长度为x的正三角形,个数为n-x+1,所以总的个数为(n-x+1+1)*(n-x+1)/2
              在最低边上长度为x的倒三角形,个数为n-x*2+1(2*x<=n),所以总的个数为(n-2*x+1+1)*(n-2*x+1)/2;
     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int n,i,j;
     6     long long a[501]={0};
     7     for(i=1;i<501;i++)
     8     {
     9         for(j=1;j<=i;j++)
    10         {
    11             a[i]+=(i-j+1+1)*(i-j+1)/2;
    12             if(j*2<=i)
    13                 a[i]+=(i-2*j+1+1)*(i-2*j+1)/2;
    14         }
    15     }
    16     while(cin>>n)
    17         cout<<a[n]<<endl;
    18 }
     
  • 相关阅读:
    bower一个强大的前端依赖包管理工具
    Nodejs中的this
    探讨Nodejs中的作用域问题。
    初了解NodeJS
    小心情。
    关于数组方面的算法分析
    JS算法总结
    JS Event事件
    封装Js事件代理方法
    SqlServer 操作 JSON
  • 原文地址:https://www.cnblogs.com/a1225234/p/4669943.html
Copyright © 2011-2022 走看看