|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
小明从第一个桩走到第七个桩,再往回走,如此反复,求第五十步走到哪
#include <stdio.h>
int main()
{
int b,z,f;
for(b=1;b<=50;b++){
for(z=1;z<8;z++){
if(f=1){
f=go;
}
if(z=7){
f=back;
}
if(f=back){
z--;
}
if(f=go){
z++;
}
}
}
printf("当走完50步后,小明站在第%d个桩上",z);
return 0;
}
求友友指点一下
- #include <stdio.h>
- int main() {
- int steps = 50;
- int num_poles = 7;
- int current_pole = 1;
- int direction = 1; // 1表示向前走,-1表示向后走
- for (int i = 1; i <= steps; i++) {
- // 计算当前走到的桩号
- if (direction == 1) {
- current_pole++;
- if (current_pole > num_poles) {
- current_pole = num_poles - 1;
- direction = -1;
- }
- } else {
- current_pole--;
- if (current_pole < 1) {
- current_pole = 2;
- direction = 1;
- }
- }
- }
- printf("小明在第%d步时走到了第%d个桩\n", steps, current_pole);
- return 0;
- }
复制代码
|
|