c Language question: Take a positive integer x and find the minimum n value that becomes 1-2+3-4+5-6+...+n>x

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

//Input a positive integer x and enter 1-2+3-4+5-6+...Find the minimum n value that becomes +n>x

#include <stdio.h>
int main(){
    int x,a,b=0;
        else if(a%2==1){
        else if(b>x){
    return 0;

It's been a week since I started... Please reply.


2022-09-19 23:26

1 Answers

There are no questions in the questionnaire and there are no question marks, so I don't know what to "answer" you. Perhaps the question (i.e., agony and embarrassment) is, "Why doesn't this code end and keep running? break doesn't get caught and the answer doesn't print out! What should I do?"I know… I understand that you say it's been a week since you started something.

To get right to the conclusion, you can change the order. Set if(b>x) to the first if in for.

If you think about it carefully, a%2 is always 1 or 0. There are no exceptions. (Why there are no exceptions is omitted due to the need to explain the secondary mathematical dimension) So the current if-elseif-elseif control always falls to the first or second quarter and never falls to the third quarter. That's why I don't ride break and go around the infinite loop. If you change the order, b>x can be true or false, so if it is false, then you will move on to examining a%2.

Did the (I guessed) question answer?

2022-09-19 23:26

If you have any answers or tips

© 2022 pinfo. All rights reserved.