A Gravity Flip

本文介绍了一种解决玩具箱中立方体位置变化问题的算法。通过读取初始配置并进行排序来确定重力切换后的立方体分布。适用于100列以内、每列不超过100个立方体的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the abilitytochange gravity.

There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange.

Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!

Input

The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains n space-separatedintegernumbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column.

Output

Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switchEg4  2 34 5 4 PUTOUT 2 4 5 34

解题思路   最初朝着把n个数输出,减去其中最小的这个方向一直想,没想出来,后来看到右边这图从左到右逐渐增大所以联系右边图,感觉是一个比较大小的程序,又理解题意明白其实就是一个排序题,1循环输入2从小到大排列3,循环输出

#include<stdio.h#define num 101int main(){  int a,b,d,e,f,ss;
    int c[100];scanf("%d",&a);  {  for(b=1; b<=a; b++) { scanf("%d",&c[b]);       }
        for(d=1; d<a; d++)   {  for(e=d+1; e<=a; e++)
            { if(c[d]>c[e])
                {  ss=c[d];c[d]=c[e]; c[e]=ss;
                }
             }
        }
        for(f=1; f<=a; f++)  {  printf("%d ",c[f]);    }   }return 0;}

 

#include <graphics.h> #include "Sprite.h" #include "input.h" #include <chrono> #include <windows.h> #include "audio_player.hpp" #include "UI.h" #include "AssetManager.h" float vy = 0.0f; const float gravity = 0.5f; const float jumpSpeed = -14.0f; const float groundY = 400.0f; int main() { // 初始化图形窗口 initgraph(960, 640, EW_SHOWCONSOLE); IMAGE background; loadimage(&background, _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\background.png")); // 创建输入处理器 InputHandler input; Sprite patman; patman.AddState("right", _T("C:\\Users\\lin13\\Desktop\\提瓦特幸存者\\img\\player_right_%d.png"), 6, 100, true); patman.AddFlipAnim("right", "right_flip"); // 创建精灵对象 Sprite player; // 加载动画(假设精灵图为水平排列的帧) /* IMAGE spriteSheet; loadimage(&spriteSheet, _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\run.png")); // 请确保路径正确 IMAGE jumpSpriteSheet; loadimage(&jumpSpriteSheet, _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\jump.png")); IMAGE idleSheet; loadimage(&idleSheet, _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\idle.png")); */ AssetManager assetmanager; std::shared_ptr<TextureAtlasAsset> player_run_atlas = std::make_shared<TextureAtlasAsset>("player_run_atlas", _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\run.png"), 10); std::shared_ptr<TextureAtlasAsset> player_jump_atlas = std::make_shared<TextureAtlasAsset>("player_jump_atlas", _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\jump.png"), 5); std::shared_ptr<TextureAtlasAsset> player_idle_atlas = std::make_shared<TextureAtlasAsset>("player_idle_atlas", _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\idle.png"), 5); std::shared_ptr<TextureAtlasAsset> player_run_atlas_flip = player_run_atlas->CreateFlipped("player_run_atlas_flip"); std::shared_ptr<TextureAtlasAsset> player_jump_atlas_flip = player_jump_atlas->CreateFlipped("player_jump_atlas_flip"); std::shared_ptr<TextureAtlasAsset> player_idle_atlas_flip = player_idle_atlas->CreateFlipped("player_idle_atlas_flip"); assetmanager.AddExistingAsset("player_run_atlas", player_run_atlas); assetmanager.AddExistingAsset("player_jump_atlas", player_jump_atlas); assetmanager.AddExistingAsset("player_idle_atlas", player_idle_atlas); assetmanager.AddExistingAsset("player_run_atlas_flip", player_run_atlas_flip); assetmanager.AddExistingAsset("player_jump_atlas_flip", player_jump_atlas_flip); assetmanager.AddExistingAsset("player_idle_atlas_flip", player_idle_atlas_flip); Animation Run(player_run_atlas); Animation Jump(player_jump_atlas); Animation Idle(player_idle_atlas); Animation Run_flip(player_run_atlas_flip); Animation Jump_flip(player_jump_atlas_flip); Animation Idle_flip(player_idle_atlas_flip); player.AddState("Run", Run); player.AddState("Jump", Jump); player.AddState("Idle", Idle); player.AddState("Run_flip", Run_flip); player.AddState("Jump_flip", Jump_flip); player.AddState("Idle_flip", Idle_flip); player.SetTransition("Jump", "Run"); assetmanager.LoadAllAssets(); //player.AddState("Run", _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\run.png"), 10, 100, true); //10帧,间隔100ms,循环播放 //player.AddState("Jump", _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\jump.png"), 5, 120, false); //player.AddState("Idle", _T("C:\\Users\\lin13\\Source\\Repos\\lumin2D\\lumin2D\\resources\\player\\idle.png"), 5, 100, true); //player.AddFlipAnim("Run", "Run_flip"); //player.AddFlipAnim("Jump", "Jump_flip"); //player.AddFlipAnim("Idle", "Idle_flip"); //player.SetTransition("Jump", "Run"); // 设置初始状态 Vector2 pos; pos.x = 100; pos.y = 100; Vector2 v(6, 1); Vector2 pos2(200, 500); patman.SetState("right", pos, v); player.SetState("Idle", pos2, v); Camera cam; BeginBatchDraw(); // 主循环 bool facingLeft = false; // 朝向标志 bool jumping = false; // 跳跃中标志 //UI TextBox textbox1(200,200, 80, 50, "5月18号"); Button esc_button(300, 200, 80, 50, "退出游戏"); //音频 AudioPlayer BGM,player_jump,player_run; BGM.init("C:\\Users\\lin13\\Desktop\\game2\\resources\\audio\\bgm.mp3", "bgm"); player_jump.init("C:\\Users\\lin13\\Desktop\\game2\\resources\\audio\\player_jump.mp3", "player_jump", false); player_run.init("C:\\Users\\lin13\\Desktop\\game2\\resources\\audio\\player_run.mp3", "player_run", false); BGM.play(); while (true) { DWORD frame_start_time = GetTickCount(); cleardevice(); putimage(-cam.get_position().x, -cam.get_position().y, &background); textbox1.draw(); esc_button.draw(); int delta = 1000/144; input.update(); //处理输入 if (!jumping && input.isKeyDown(VK_SPACE)) { jumping = true; vy = jumpSpeed; player.Play(facingLeft ? "Jump_flip" : "Jump", false); player_jump.play(); } // 每帧模拟重力 if (jumping) { vy += gravity; player.Move(0, vy); if (vy < 100) { std::cout << "jumping: " << jumping << std::endl; std::cout << "vy: " << vy << std::endl; } // 落地检测 //if (player.position.y >= groundY) { //player.position.y = groundY; //vy = 0; //jumping = false; //player.play(facingLeft ? "Idle_flip" : "Idle", true); //} } float dx = 0, dy = 0; if (input.isKeyDown('A')) { dx -= 1; facingLeft = true; //player.switchFacingDuringPlay(true); patman.SwitchFacingDuringPlay(true); player_run.play(); } if (input.isKeyDown('D')) { facingLeft = false; dx += 1; //player.switchFacingDuringPlay(false); patman.SwitchFacingDuringPlay(false); player_run.play(); } if (input.isKeyDown('R')) { std::cout << "click R" << std::endl; player.Play("Jump", 50000,0); } if (!jumping) { if (input.isKeyDown('W')) dy -= 1; if (input.isKeyDown('S')) dy += 1; } if (dx != 0 || dy != 0) { float length = sqrt(dx * dx + dy * dy); dx /= length; dy /= length; //player.move(dx * 6, dy * 6); patman.Move(dx * 3, dy * 3); //cam.ChangePos(dx * 6, dy * 6); if (!jumping) { player.Play(facingLeft ? "Run_flip" : "Run", true); } } else { player.Play(facingLeft ? "Idle_flip" : "Idle", true); } player.UpdatePos(dx, dy,jumping); //player.UpdateAnim( dx, dy, facingLeft, jumping); if (jumping) { // 落地检测 if (player.position.y >= groundY) { player.position.y = groundY; vy = 0; jumping = false; //player.play(facingLeft ? "Idle_flip" : "Idle", true); } } // 更新精灵动画 player.Update(delta); player.Draw(cam); patman.Update(delta); patman.Draw(cam); FlushBatchDraw(); // 延迟以控制帧率 DWORD frame_end_time = GetTickCount(); if(frame_end_time-frame_start_time<1000/144) Sleep(delta-(frame_end_time-frame_start_time)); } // 关闭图形窗口 closegraph(); return 0; } 帮我看一下为什么我用资源管理器加载按下A会出问题,而用直接的路径加载就没事
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值