Home > Back-end >  Of the new solution
Of the new solution

Time:11-10

Bosses how to use c + + write a procedure in all integers and strives for the closed interval, kneel beg

CodePudding user response:

Hope this program can help you
 
#include
using namespace std;
Int main () {
Int left, right;
Cout<& lt;" Please enter the interval "& lt; & lt; Endl;
Cin> Left> Right;
Cout<& lt;" Interval [" & lt; & lt; left<& lt; ", "& lt; & lt; right<& lt;"] All the integers and for "& lt; & lt; (left + right) * (right - left + 1)/2 & lt; & lt; Endl;
return 0;
}

CodePudding user response:

reference 1/f, no. 3 room response:
hope this program can help you
 
#include
using namespace std;
Int main () {
Int left, right;
Cout<& lt;" Please enter the interval "& lt; & lt; Endl;
Cin> Left> Right;
Cout<& lt;" Interval [" & lt; & lt; left<& lt; ", "& lt; & lt; right<& lt;"] All the integers and for "& lt; & lt; (left + right) * (right - left + 1)/2 & lt; & lt; Endl;
return 0;
}

On both sides of the interval is not necessarily an integer, such as [0.3, 5.8], it will not be able to use plastic to define, should be how to change?

CodePudding user response:

refer to the second floor clearlove1777 response:
Quote: reference 1/f, no. 3 room response:
hope this program can help you
 
#include
using namespace std;
Int main () {
Int left, right;
Cout<& lt;" Please enter the interval "& lt; & lt; Endl;
Cin> Left> Right;
Cout<& lt;" Interval [" & lt; & lt; left<& lt; ", "& lt; & lt; right<& lt;"] All the integers and for "& lt; & lt; (left + right) * (right - left + 1)/2 & lt; & lt; Endl;
return 0;
}

On both sides of the interval is not necessarily an integer, such as [0.3, 5.8], it will not be able to use plastic to define, should be how to change?


Revised as follows:
 
#include
#include
using namespace std;
Int main () {
Double left, right;
Int l, r;
Cout<& lt;" Please enter the interval "& lt; & lt; Endl;
Cin> Left> Right;
//the left up integer, ceil for integral function up
L=ceil (left);
//right down integer, floor is downward integral function
R=floor (right);
Cout<& lt;" Interval [" & lt; & lt; left<& lt; ", "& lt; & lt; right<& lt;"] All the integers and for "& lt; & lt; (l + r) * (r - l + 1)/2 & lt; & lt; Endl;
return 0;
}
  • Related