0

我尝试了很多组合并在网上搜索了一些有用的东西,但不幸的是我没有发现任何有用的东西。

这是为了我的家庭作业。69 个问题中唯一一个我无法回答的问题。

问题:

四个整型变量 pos1, pos2, pos3, pos4 已被声明和初始化。编写“左旋转”它们的值所需的代码:为每个变量获取后续变量的值,而 pos4 获取 pos1 的值。

我试过的例子:

int tempP1 = pos1;
int tempP2 = pos2;
int tempP3 = pos3;
int tempP4 = pos4;

pos4 = tempP1;
pos3 = tempP2;
pos2 = tempP3;
pos1 = tempP4;

它向我展示了什么:

    Remarks:
     ⇒     Your code had an error during execution

More Hints:
     ⇒     Are you sure you want to use: tempP1
     ⇒     Are you sure you want to use: tempP2
     ⇒     Are you sure you want to use: tempP3

Problems Detected:
     ⇒     pos1 has wrong value
     ⇒     pos3 has wrong value
4

6 回答 6

3
pos4 = tempP1;
pos2 = tempP3;
pos3 = tempP4;
pos1 = tempP2;

听起来对吗?

于 2012-09-09T17:11:52.023 回答
2
int tempP1 = pos1; 
int tempP2 = pos2; 
int tempP3 = pos3; 
int tempP4 = pos4; 

pos4 = tempP1; 
pos3 = tempP4; 
pos2 = tempP3; 
pos1 = tempP2;
于 2012-09-10T18:36:03.270 回答
2
int tmp = pos1;
pos1 = pos2;
pos2 = pos3;
pos3 = pos4;
pos4 = tmp;
于 2016-10-20T20:06:44.810 回答
0

代码是: pos4=tempP1; pos3=tempP4; pos2=tempP3; pos1=tempP2;

于 2012-09-09T17:50:24.503 回答
0

您可以通过仅声明一个 temp 值而不是创建四个不同tempValue的 s 来更好地压缩代码:

int tempValue = 0;


tempValue = pos1;

pos1 = pos2;

pos2 = pos3;

pos3 = pos4;

pos4 = tempValue;
于 2014-02-11T23:15:07.270 回答
-1
int a=pos4;
int b=pos3;
int c=pos2;
pos4=pos1;
pos3=a;
pos2=b;
pos1=c;
于 2016-10-19T19:50:45.163 回答