| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 803 人关注过本帖
标题:一道ACM关于Triangles的题
取消只看楼主 加入收藏
jackylin
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-9-5
收藏
 问题点数:0 回复次数:2 
一道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
jackylin
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-9-5
收藏
得分:0 
回复 3# StarWing83 的帖子
能不能讲解一下?谢谢
2008-09-17 18:41
快速回复:一道ACM关于Triangles的题
数据加载中...
 
   



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

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