5.17移数字游戏

Q:有一个包含9个圆圈的数阵,将1~8这8个数随机写到数阵外围,只剩下中间一个空圆圈。规定每个数字只能按照数阵中的直线从一个圆圈移动到另一个空的圆圈中。通过若干步的移动,将数阵移动成

1--2--3

8--  --4//中间数字是空格

7--6--5 

编写程序,输出数字每一步的移动过程。

#include <iostream>
#include<cstdio>
using namespace std;
int m[8];
getstep(int m[]) {
	int i,j,tmp;
	for(i=0;i<7;i++)	//冒泡排序
		for(j=0;j<7-i;j++)
			if(m[j]>m[j+1]) {
				tmp=m[j];
				m[j]=m[j+1];
				m[j+1]=tmp;
				printf("(%d --> 0)\n",j+1);	//输出移动步骤
				printf("(%d --> %d)\n",j+2,j+1);
				printf("(0 --> %d)\n",j+2);
			}
			printf("\n");
}

print(int m[]) {
	printf(" [%d]--[%d]--[%d]\n",m[0],m[1],m[2]);
	printf("  |  %c |  %c  | \n",92,47);
	printf(" [%d]--[ ]--[%d]\n",m[7],m[3]);
	printf("  |  %c |  %c  | \n",47,92);
	printf(" [%d]--[%d]--[%d]\n",m[6],m[5],m[4]);

}
int main() {
	int i;
	printf("input 8 integer to arrange this matrix\n");
	for(i=0;i<8;i++)
	scanf("%d",&m[i]);
	printf("initial data matrix is like\n");
	print(m);
	printf("\nMOVE STEP:\n");
	getstep(m);
	printf("the result of moving is\n");
	print(m);
	return 0;
}

  

时间: 2024-08-10 16:55:47

5.17移数字游戏的相关文章

选字游戏

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>选字游戏</title> <style> #wrap{width: 400px;height: 600px;border: 1px solid red;margin: auto} #big{font-size: 140px;text-align:

猜字游戏---批处理

最近学习批处理,看到一个大神写得猜字游戏,看懂了之后也仿了一个一模一样的,今天就来说道说道.先看下源码和效果图. @echo off color 0f mode con:cols=55 lines=30 title 猜数字游戏 By leo echo.&echo. echo.&echo ***** 游戏规则 ***** echo.&echo 程序会自动生成一组四个互不相同的0-9内的整数, echo.&echo 请你根据猜测和判断,每次输入一组四个互不相同 echo.&am

猜字游戏java

一.实践目的 1.掌握基本输入输出. 2.掌握方法定义与调用,理解参数传递方式. 3.掌握数组的声明.定义与初始化,数组的处理. 4.掌握数组作为方法参数和返回值. 二.实践要求 利用方法.数组.基本输入输出等Java基础知识,给出所选项目的基本设计与实现. 三.项目需求 所选项目名称:猜字游戏 项目需求描述,主要指应实现的主要功能.如:制作一个猜字小程序,又根据实践一:基础程序设计实践实践目的,只要作出相应的功能,实现相应的输入输出即可,窗口以及共多功能可以在后期完善! https://git

猜字游戏源码

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; namespace 制作

原生js实现选字游戏

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" con

学习Python 猜字游戏(上)

缺点:不能生成随机答案 num = 19 i_num = -1 n = 0 # 一个猜数字的游戏,3次机会 while num != i_num: n = n + 1 i_num = int(input('请输入数字:')) if n < 3: if i_num > num: print("大了哦") elif i_num < num: print('小了哦') else: print('Bingo!') elif n >= 3: if i_num == num

【c语言】 &#160; &#160; &#160; 小游戏——猜字游戏

大家一起来玩一个小游戏吧! srand((unsigned int)time(NULL));//设置随时间变化的随机数 ret=rand()%100; //产生0~100随机数 添加 #include<time.h> 改变100的值,使其产生0~其他的随机数. 自定义游戏菜单menu. #include<stdio.h> #include<stdlib.h> #include<time.h> void  fun1(int ret,int count)//游戏

c语言小程序:编写猜字游戏

#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int input=1; printf("欢迎使用猜数字游戏\n"); while (input) { printf("**********************\n"); printf("******* 1.start ******\n"); printf("

猜字游戏

num = counter = counter2 = i () :           counter < :         num_input = (())         (,counter,i,counter2)         num == num_input :             ()             num > num_input :   ()         num < num_input :             ()     counter >=