| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 173 人关注过本帖
标题:一道ACM关于Triangles的题
收藏  订阅  推荐  打印 
jackylin
Rank: 1
等级:新手上路
帖子:15
积分:266
注册:2008-9-5
一道ACM关于Triangles的题

请看下我的算法是否有问题谢谢
#include<stdio.h>
void main()
{
  int i;
  int j;
  int a[2000]={0};
  int num=0;
  for(i=0;i<2000;i++)
  {
      scanf("%d",&a[i]);
      while(a[i]!=0)
      {
          int flag=0;
          for(j=1;;j++)
          {
              int m=0,sum=0;
              if(a[i]==(j*(j+1))/2)
              {
                  flag=1;
                  break;
              }
              if(a[i]>(j*(j+1))/2)
              {
                  break;
              }
          }
          if(flag)
          {
              int m=0,sum=0;
              int k;
              printf("%d is a triangular number.\n",a[i]);
              for(k=1;;k++)
              {
                  sum+=k;
                  if(sum<a[i])
                     m++;
                  if(sum==a[i])
                     break;
              }
              for(k=0;k<=m;k++)
              {
                  printf("*");
              }
              printf("\n");
              break;

          }
          else
              printf("%d is not a triangular number.\n",a[i]);
          break;
          
      }
      break;
  }  
}
搜索更多相关主题的帖子: ACM  Triangles  
2008-9-17 00:38
jackylin
Rank: 1
等级:新手上路
帖子:15
积分:266
注册:2008-9-5

题目是这样的
A triangle can be made up out of dots, like the ones shown below:



The number of dots in each one is called a triangular number. More precisely, a number is said to be triangular if it is of the form ½ n(n+1). The numbers 1, 3, 6, 10, and 15 are all triangular.

Given a number, determine if it is triangular. If it is, draw the corresponding right triangle, if it is not, say so.

Input
A positive integer less than 2000. Each case will be on a separate line. A zero (0) denotes the end of input.

Output
The phrase “<num> is a triangular number.” followed by a right triangle of “*” characters, or the phrase “<num> is not a triangular number.” The triangle should be oriented with the hypotenuse on the right and the right angle in the lower left.

Sample Input
3
4
6
0
Sample Output
3 is a triangular number.
*
**
4 is not a triangular number.
6 is a triangular number.
*
**
***
2008-9-17 00:39
StarWing83
Rank: 12Rank: 12Rank: 12
来自:湖北工业大学
等级:版主
威望:9
帖子:2483
积分:26219
注册:2007-11-16


程序代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        int r = sqrt(n = 8 * n + 1);
        if ((r * r == n) && (r & 1))
            printf("Yes,%d\n", (r - 1) / 2);
        else printf("No\n");
    }
    return 0;
}

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-9-17 01:58
leeco
Rank: 4
等级:高级会员
威望:8
帖子:870
积分:9662
注册:2007-5-10
回复 1# jackylin 的帖子

有没有问题你在OJ上submit一次不就知道了
2008-9-17 10:22
jackylin
Rank: 1
等级:新手上路
帖子:15
积分:266
注册:2008-9-5
回复 3# StarWing83 的帖子

能不能讲解一下?谢谢
2008-9-17 18:41
StarWing83
Rank: 12Rank: 12Rank: 12
来自:湖北工业大学
等级:版主
威望:9
帖子:2483
积分:26219
注册:2007-11-16
回复 5# jackylin 的帖子

看懂了题目肯定会做。这题目数学方法显然可以快很多,我是O(1),你是O(n)

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-9-17 21:40
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.153332 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved