1007: Triangles
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 5736 | 2163 | Standard |
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. * ** ***
#include<stdio.h>
int main()
{ void draw(int n);
int i,k,m,n;
while(scanf("%d",&n),n)
{ k=0;
for(i=1;i<=70;i++)
if((i+1)*i/2==n) {k=1;m=i;break;}
if(k)
{printf("%d is a triangular number./n",n);
draw(m);
}
else
printf("%d is not a triangular number./n",n);
}
return 0;
}
void draw(int n)
{int i,j;
for( i=1;i<=n;i++)
{ for( j=1;j<=i;j++)
printf("*");
printf("/n");
}
}