一种读取整数的程序,只要整数是正负交替的。C++; 我对C++编程很陌生。我一直在尝试创建一个程序,只要整数是正负交替的,它就可以读取整数。如果不在if中使用if,我不知道如何制作它,因此它可以读取例如(2,-2,2,-2,-2,-2) int temp=0,x=0,y=0; 而(温度==0){ cin>>x; 如果(x>0){ y=x; cin>>x; if(x0&&x>0),则为else 打破 } if(x>x; 如果(x>0) 继续; else if(y
我将读取循环外的第一个数字,并根据该数字是正还是负设置一个变量。然后让循环读取数字,并根据变量知道期望的是正数还是负数。如果它得到了预期的结果,则反转变量中的值,然后重复。在一些伪代码中,一般顺序如下:一种读取整数的程序,只要整数是正负交替的。C++; 我对C++编程很陌生。我一直在尝试创建一个程序,只要整数是正负交替的,它就可以读取整数。如果不在if中使用if,我不知道如何制作它,因此它可以读取例如(2,-2,2,-2,-2,-2) int temp=0,x=0,y=0; 而(温度==0){ cin>>x; 如果(x>0){ y=x; cin>>x; if(x0&&x>0),则为else 打破 } if(x>x; 如果(x>0) 继续; else if(y,c++,C++,我将读取循环外的第一个数字,并根据该数字是正还是负设置一个变量。然后让循环读取数字,并根据变量知道期望的是正数还是负数。如果它得到了预期的结果,则反转变量中的值,然后重复。在一些伪代码中,一般顺序如下: read(number) // if this number's negative, expect the next to be positive (and vice versa) expect_positive = number < 0 for (;;) { read(num
read(number)
// if this number's negative, expect the next to be positive (and vice versa)
expect_positive = number < 0
for (;;) {
read(number)
bool positive = number >= 0
if (positive != expect_positive)
break;
expect_positive = !expect_positive;
}
读取(数字)
//如果该数字为负数,则预期下一个数字为正数(反之亦然)
期望值为正=数值<0
对于(;;){
读(数字)
布尔正=数字>=0
如果(阳性!=预期阳性)
打破
expect\u positive=!expect\u positive;
}
一种可能的实现方式:
#include <iostream>
int main()
{
int currentNumber, previousNumber;
std::cin >> currentNumber;
do
{
previousNumber = currentNumber;
std::cin >> currentNumber;
} while (currentNumber * previousNumber < 0);
}
#包括
int main()
{
int currentNumber,前一个数字;
标准::cin>>当前编号;
做
{
previousNumber=当前编号;
标准::cin>>当前编号;
}while(currentNumber*previousNumber<0);
}
如果两个数字的乘积为负数,则两个数字是相反的符号。将第一个cin移出循环
int temp = 0, x = 0, y = 0;
cin >>x;
if ( x !=0)
{
while (temp == 0){
//cin >> x;// should be above as this because it resets the +- order every 2nd time
if (x > 0) {
y = x;
cin >> x;
if (x < 0)
continue;
else if(y > 0 && x >= 0)//>= to avoid infinite loop if 0 is input
break;
}
if(x < 0){
y = x;
cin >> x;
if (x > 0)
continue;
else if(y < 0 && x <= 0)//<= to avoid infinite loop if 0 is input
break;
}
}
}
int temp=0,x=0,y=0;
cin>>x;
如果(x!=0)
{
而(温度==0){
//cin>>x;//应如上所示,因为它每2次重置+-顺序
如果(x>0){
y=x;
cin>>x;
if(x<0)
继续;
else如果(y>0&&x>=0)/>=则在输入0时避免无限循环
打破
}
if(x<0){
y=x;
cin>>x;
如果(x>0)
继续;
否则,如果(y<0&&x,如果a
和b
具有不同的符号位,则可以使用属性(异或b)<0
#include <iostream>
int main()
{
int a, b;
std::cin >> b;
do {
a = b;
std::cin >> b;
}while( (a^b) < 0);
std::cout << "Same sign" << std::endl;
}
#包括
int main()
{
INTA,b;
标准:cin>>b;
做{
a=b;
标准:cin>>b;
}而((a^b)<0);
std::cout其他答案已经很好了,但我想给出一个与你相近的解决方案,向你展示如何简化事情
例如,您有许多不必要的检查。您测试x<0
,设置y=x
,然后再次测试y<0
<>你不考虑单独的起始案例。<代码> CIN > X <代码>不应无条件地发生每个循环迭代。
以下是一个简化:
int x = 0;
int last = 0; //more meaningful name
cin >> last; //you were missing some starting point
while (true){ //you do not need temp, instead just replace it by what you want to say
cin >> x;
if (x > 0 && last < 0) {
last = x;
continue;
}
else if(x < 0 && last > 0){
last = x;
continue;
}
break;
}
intx=0;
int last=0;//更有意义的名称
cin>>last;//您缺少一些起点
虽然(true){//您不需要temp,只需用您想说的内容替换它即可
cin>>x;
如果(x>0&&last<0){
last=x;
继续;
}
else if(x<0&&last>0){
last=x;
继续;
}
打破
}
当然,最好用产品检查代替这些检查,以使事情更加简单。如果只需要oposite标志,则编号*-1也可以
#include <iostream>
#include <string>
int main(){
int input_int;
std::string input;
int last = 2;
while (1){
std::cout << "Last int -> " << last << std::endl;
std::cin >> input;
input_int = std::stoi(input);
if (last * -1 == input_int) {
last = input_int;
} else {
break;
}
}
return 0;
}
#包括
#包括
int main(){
int输入\ u int;
std::字符串输入;
int last=2;
而(1){
std::cout模板
在备选方案(b中、e中、r中、F中)时输出副本
{
如果(b==e)返回r;
bool lastF=f(*b);
*r++=*b++;
而(b!=e&f(*b)!=lastF){
*r++=*b++;
lastF=!lastF;
}
返回r;
}
你到底遇到了什么问题?请说得更具体一些。为什么不给自己设置一个bool
标志来知道你期望的是一个正数还是负数?然后只需在每次循环迭代中读取一次,并将结果与标志进行比较。读取一个数字,如果是正数,则记录它。如果是负数,则读取下一个数字符号与前一个数字相反,接受它并更新标志,否则停止。重复。当第一次旋转良好时出现问题,然后我开始输入错误,例如((2,-2)-第一次旋转(-2,-2)-第二次旋转)。我不知道如何在不进入第二个if的情况下进行。非常感谢。你真的帮助了我。