Medium

Find the number of ways to express n as sum of some (at least two) consecutive positive integers.

Example

  • For n = 9, the output should be
    isSumOfConsecutive2(n) = 2.

    There are two ways to represent n = 9: 2 + 3 + 4 = 9 and 4 + 5 = 9.

  • For n = 8, the output should be
    isSumOfConsecutive2(n) = 0.

    There are no ways to represent n = 8.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    A positive integer.

    Guaranteed constraints:
    1 ≤ n ≤ 10^4.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int isSumOfConsecutive2(int n)
{
	int count=0;

	for(int i=1;i<=n/2;i++)
	{
		int sum=0;

		for(int j=i;sum<n;j++)
			sum+=j;
		count+=sum==n;
	}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Pages Numbering With Ink  (0) 2020.04.16
<Codesignal> Square Digits Sequence  (0) 2020.04.15
<Codesignal> Is Power?  (0) 2020.04.15
<Codesignal> Make Array Consecutive 2  (0) 2020.04.15
<Codesignal> Replace Middle  (0) 2020.04.14

+ Recent posts