| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 801 人关注过本帖
标题:一道ACM关于Triangles的题
只看楼主 加入收藏
jackylin
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-9-5
收藏
 问题点数:0 回复次数: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-09-17 00:38
jackylin
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-9-5
收藏
得分:0 
题目是这样的
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 &frac12; 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-09-17 00:39
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
程序代码:
#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-09-17 01:58
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
回复 1# jackylin 的帖子
有没有问题你在OJ上submit一次不就知道了
2008-09-17 10:22
jackylin
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-9-5
收藏
得分:0 
回复 3# StarWing83 的帖子
能不能讲解一下?谢谢
2008-09-17 18:41
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
回复 5# jackylin 的帖子
看懂了题目肯定会做。这题目数学方法显然可以快很多,我是O(1),你是O(n)

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-09-17 21:40
快速回复:一道ACM关于Triangles的题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012095 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved