Solution of 11614 - Etruscan Warriors Never Play Chess

Problem Description
source:https://uva.onlinejudge.org/external/116/11614.html

A troop of Etruscan warriors is organized as follows. In the first row, there is only one warrior; then, the second row contains two warriors; the third row contains three warriors, and so on. In general, each row i contains i warriors. 
         We know the number of Etruscan warriors of a given troop. You have to compute the number of rows in which they are organized. 
        Please note that there may be some remaining warriors (this could happen if they are not enough to form the next row). For example, 3 warriors are organized in 2 rows. With 6 warriors you can form 3 rows; but you can also form 3 rows with 7, 8 or 9 warriors. 

Input 

The first line of the input contains an integer indicating the number of test cases. 
     For each test case, there is a single integer, n, indicating the number of Etruscan warriors. You can assume that 0 ≤ n ≤ 1018 . 

Output 

For each test case, the output should contain a single integer indicating the number of rows that can be formed. 

Sample Input 




7


10 

Sample Output 






4


Solution:
#include<stdio.h>
#include<math.h>
int main()
{
     long int n,i,warriors,row;
     while(scanf("%ld",&n)==1)
     {
        for(i=1;i<=n;i++)
        {
            scanf("%ld",&warriors);
            row=(sqrt(1+8*warriors)-1)/2;
            printf("%ld\n",row);
         }
         return 0;
    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience