数组倒转(*-*)

这段Java代码通过Scanner类从控制台读取6个整数存入数组a,然后将数组a的元素逆序存入数组b并输出。代码主要展示了Java中数组的使用和元素逆序的实现。
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int[] a = new int[6];
    for (int i = 1; i <= 6; i++) {
        if (scanner.hasNextInt()) {
            a[i - 1] = scanner.nextInt();
        }
    }
    for (int array : a) {
        System.out.print(array);
        System.out.println();
    }
    int[] b = new int[6];
    for (int i = 1; i <= 6; i++) {
        b[b.length - i] = a[i - 1];
    }
    for (int array : b) {
        System.out.print(array);
    }
}
#include "debug.h" #include "max7219.h" #include <stdlib.h> /* ---------------- 硬件 ---------------- */ #define MAX7219_DIN_PIN GPIO_Pin_7 #define MAX7219_CS_PIN GPIO_Pin_6 #define MAX7219_CLK_PIN GPIO_Pin_5 #define MATRIX_SIZE 8 #define MAX_SAND_GRAINS 64 // 正好 64 像素 /* ---------------- 沙粒 ---------------- */ typedef struct { uint8_t x; // 当前视觉列 uint8_t y; // 当前视觉行 uint8_t is_visible; uint8_t target_r; // 目标行(下屏) uint8_t target_c; // 目标列 } SandGrain; static SandGrain sand[MAX_SAND_GRAINS]; static uint8_t sand_cnt = 0; /* ---------------- 沙堆 ---------------- */ static uint8_t sand_top[MATRIX_SIZE] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; static uint8_t sand_bottom[MATRIX_SIZE] = {0}; /* ---------------- 对角线顺序表 64 像素 ---------------- */ static uint8_t order[64]; // high4=row, low4=col static uint8_t order_idx = 0; static uint8_t pile_sum = 14; static void build_order(void) { uint8_t k = 0; for (int8_t s = 14; s >= 0; --s) for (int8_t c = 7; c >= 0; --c) { int8_t r = s - c; if (r >= 0 && r <= 7) order[k++] = (r << 4) | c; } } /* ---------------- 顶屏移除 ---------------- */ static uint8_t remove_sum = 0; static uint8_t remove_done = 1; static uint8_t remove_one(void) { uint8_t buf[64], cnt = 0; for (uint8_t r = 0; r < MATRIX_SIZE; ++r) for (uint8_t c = 0; c < MATRIX_SIZE; ++c) if ((r + (7 - c)) == remove_sum && (sand_top[r] & (1 << c))) buf[cnt++] = (r << 4) | c; if (cnt == 0) { remove_done = 1; if (++remove_sum > 14) remove_sum = 0; return 0; } uint8_t pick = buf[rand() % cnt]; uint8_t r = pick >> 4, c = pick & 0x0F; sand_top[r] &= ~(1 << c); return 1; } /* ---------------- 闪烁控制 ---------------- */ static uint8_t blink_phase = 0; // 0=等待闪烁, 1=闪烁中, 2=下落中 static uint32_t blink_tick = 0; /* ---------------- 生成沙粒(每颗) ---------------- */ static void create_single_sand(void) { if (sand_cnt >= MAX_SAND_GRAINS || order_idx >= 64) return; if (!remove_one()) return; uint8_t pack = order[order_idx++]; uint8_t tr = pack >> 4; uint8_t tc = pack & 0x0F; sand[sand_cnt].x = 0; // 第二屏左上角 sand[sand_cnt].y = 0; sand[sand_cnt].target_r = tr; sand[sand_cnt].target_c = tc; sand[sand_cnt].is_visible = 1; ++sand_cnt; } /* ---------------- 更新沙粒 ---------------- */ static uint8_t layer_full(void) { for (uint8_t r = 0; r < MATRIX_SIZE; ++r) for (uint8_t c = 0; c < MATRIX_SIZE; ++c) if ((r + c) == pile_sum && !(sand_bottom[r] & (1 << (7 - c)))) return 0; return 1; } static void update_sand(void) { if (blink_phase == 0) { /* 1. 等待闪烁完成后再生成 */ return; } if (blink_phase == 1) { /* 2. 闪烁中:6 帧后进入下落 */ ++blink_tick; if (blink_tick >= 2) { blink_phase = 2; create_single_sand(); } return; } /* 3. 下落阶段 */ uint8_t filled = 0; for (int i = 0; i < sand_cnt; ++i) { if (!sand[i].is_visible) continue; /* 45° 对角线:x++, y++ */ if (sand[i].x < sand[i].target_c) ++sand[i].x; if (sand[i].y < sand[i].target_r) ++sand[i].y; uint8_t row = sand[i].y; uint8_t col = sand[i].x; if (row == sand[i].target_r && col == sand[i].target_c && !(sand_bottom[row] & (1 << (7 - col)))) { sand_bottom[row] |= (1 << (7 - col)); sand[i].is_visible = 0; filled = 1; } } if (filled && layer_full() && pile_sum > 0) --pile_sum; /* 当前沙粒全部落完后,准备下一次闪烁 */ if (filled && !layer_full()) { /* 还有同层未落 */ } else if (filled && layer_full() && pile_sum > 0) { /* 下一层 */ } else if (filled) { /* 全部落完 */ order_idx = 64; // 结束 } /* 若已无沙粒下落,回到闪烁等待 */ uint8_t any_running = 0; for (int i = 0; i < sand_cnt; ++i) if (sand[i].is_visible) { any_running = 1; break; } if (!any_running && order_idx < 64) { blink_phase = 1; blink_tick = 0; } } /* ---------------- 渲染 ---------------- */ static void render(void) { uint8_t top[MATRIX_SIZE] = {0}; uint8_t bottom[MATRIX_SIZE] = {0}; /* 静态沙堆 */ for (int i = 0; i < MATRIX_SIZE; ++i) { top[i] = sand_top[i]; bottom[i] = sand_bottom[i]; } /* 正在下落的沙粒 */ for (int i = 0; i < sand_cnt; ++i) { if (!sand[i].is_visible) continue; uint8_t row = sand[i].y; uint8_t col = sand[i].x; bottom[row] |= (1 << (7 - col)); } /* 闪烁:上屏 (7,7) */ if (blink_phase == 1) { if (blink_tick & 1) top[7] |= (1 << 0); else top[7] &= ~(1 << 0); } /* 先下屏,再上屏 */ for (uint8_t i = 0; i < 8; ++i) { MAX7219_SetDigitToDevice(1, i, bottom[i]); MAX7219_SetDigitToDevice(0, i, top[i]); } } /* ---------------- 主函数 ---------------- */ int main(void) { MAX7219_InitTypeDef cfg; SystemCoreClockUpdate(); Delay_Init(); cfg.GPIOx = GPIOC; cfg.DIN_Pin = MAX7219_DIN_PIN; cfg.CS_Pin = MAX7219_CS_PIN; cfg.CLK_Pin = MAX7219_CLK_PIN; cfg.numDevices = 2; MAX7219_Init(&cfg); MAX7219_SetBrightness(7); MAX7219_ClearAll(); build_order(); /* 第 1 次闪烁 */ blink_phase = 1; blink_tick = 0; uint32_t tick = 0; while (1) { ++tick; if (tick % 10 == 0) { update_sand(); render(); } Delay_Ms(12); } } 上述程序可以让两个点阵屏组成沙漏,并实现沙砾从屏幕1流入到屏幕2。现在需要重新梳理一下需求功能。 1. 两个点阵屏对角相对放置,组成沙漏的形状。 2. 第1块点阵屏从(7,7)角为沙漏流出口,沙砾从(0,0)开始逐层减少,比如(0,0)(0,1)(1,0)。3. 第2块点阵屏(7,7)角为沙漏流入角,沙砾沿着对角线(7,7)(6,6)(5,5)...向下移动。沙砾从(0,0)开始堆积,比如(0,0)(0,1)(1,0)。 4. 当第1个显示屏所有沙砾都清空,流入到第2个点阵屏上后,就反向,从第2个点阵屏(7,7)流出,从第1个点阵屏(7,7)流入。依次往复。 请在这个程序基础上完善上述功能
08-27
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值