×

# Problem Triangle creation.What's wrong with my logic.Help

What is wrong with my logic and code.Help please.Thanks.

You are given a stick of length N. You want to break it in three pieces such that it can form a triangle. How many distinct triangles can you make? Two triangles are equal if all the side lengths are same when sorted in ascending order of length. So (1, 3, 2) is same to (3, 1, 2) because their side lengths are same if we sort them, which is (1, 2, 3). But (1, 3, 4) is not same with (1, 2, 3). Suppose the lengths of three pieces are X, Y, Z (X <= Y <= Z) respectively. Following constraints should be maintained: 1. X, Y, Z > 0. 2. X, Y, Z is an integer. 3. X + Y >= Z 4. X + Y + Z = N For example if N = 14, then there are 7 triangles: (1, 6, 7), (2, 5, 7), (2, 6, 6), (3, 4, 7), (3, 5, 6), (4, 4, 6), (4, 5, 5). Input First line will give you the number of test cases, T (T<=100). Then each line will have an integer N (0< N <= 300000). Output For each test case, print the test case number starting from 1 and an integer denoting the number of distinct triangles possible. Sample Input Output for Sample Input 3 3 6 14 Case 1: 1 Case 2: 2 Case 3: 7

# define max 40

void main()

{

int q=0,m=0,i=0,j=0,k=0,n=14,p,count=0,fcount=0,tri[max][3]; //printf("Enter n:"); //scanf("%d",&n);

for(j=1;j<(n-1);j++)

{for(k=1;k<=j;k++)

{

if((j+k)>(n-(j+k))&& n!=(j+k) && (((n-(j+k))+k) >j) && ((n-(j+k))+j)>k)

{p=n-(j+k);

if(p>=j && p>=k)

{tri[i][0]=p;tri[i][1]=j;tri[i][2]=k;}

else if(p<=j && p<=k)

{tri[i][0]=j;tri[i][1]=k;tri[i][2]=p;} else if(p<=j && p>=k)

{tri[i][0]=j;tri[i][1]=p;tri[i][2]=k;}

i++; count++;

}

}

}

fcount=count;

for(m=0;m<(count);m++) for(q=(count-1);q>m && q!=m;q--){ { if(tri[m][0]==tri[q][0]) { if(tri[m][1]==tri[q][1]) { if(tri[m][2]==tri[q][2]) {fcount--;} } }

} }
printf("%d\n%d",count,fcount); }

11111716
accept rate: 0%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,919
×1,491
×690
×191
×181

question asked: 01 Aug '13, 21:23

question was seen: 2,491 times

last updated: 01 Aug '13, 21:23