简体   繁体   中英

I don't know how to solve this

After receiving the k value from the user, find the largest value and the n value at that time without (1+2+3+…+n) exceeding k.

在此处输入图像描述

How do I solve this?

Since I am Korean, I don't speak English well, so I don't know how to ask questions, so I only wrote the main subject. sorry..

How do I solve this?

One way is to just compare the new sum to k before incrementing n and adding it to the sum.

#include <stdio.h>

int main()
{
    unsigned k, l, n;
    while (printf("k .. .....: "), scanf("%u", &k) > 0 && k != -1u)
    {
        for (l = 0, n = 0; l+n < k; l += ++n) ;
        printf("1.. %d... .. %d....\n", n, l);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM