修仙类 小练习

2023-10-14 04:50
文章标签 练习 修仙

本文主要是介绍修仙类 小练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

修仙者类,灵石类,妖兽类。
修仙者之间,相互交易:
(买卖妖兽,得到相应灵石,交换妖兽,通过战斗力捕获妖兽)
(PK,生存者得到失败者,所有的财物,妖兽,灵石)
(修仙者进阶,成功等级+1,失败-1);

重载运算符

灵石类
SpriteStore.h

#pragma once
#include<string>
#include<fstream>using namespace std;
//灵石等级
enum class SpriteStoneLevel {PRIMARY_LEVEL,MIDDLE_LEVEL,ADVANCE_LEVEL,SPRITE_STONE_LEVEL_COUNT
};class SpriteStone {
public:SpriteStone(int count=0, SpriteStoneLevel level= SpriteStoneLevel::PRIMARY_LEVEL);string ret();friend ostream& operator<<(ostream &os, SpriteStone& stone);SpriteStone operator+(const SpriteStone& stone);SpriteStone operator-(const SpriteStone& stone);int getCount();bool operator>=(const SpriteStone& stone);
private:int count;SpriteStoneLevel level;
};
ostream& operator<<(ostream &os, SpriteStone& stone);

SpriteStore.cpp


#include<sstream>SpriteStone::SpriteStone(int count, SpriteStoneLevel level)
{this->count = count;this->level = level;
}
string SpriteStone::ret()
{stringstream ret;ret << count << "块";switch (level){case SpriteStoneLevel::PRIMARY_LEVEL:ret << "初级灵石";break;case SpriteStoneLevel::MIDDLE_LEVEL:ret << "中级灵石";break;case SpriteStoneLevel::ADVANCE_LEVEL:ret << "高级灵石";break;default:ret << "其他灵石";break;}return ret.str();
}SpriteStone SpriteStone::operator+(const SpriteStone & stone)
{int sum = 0;if (stone.level == SpriteStoneLevel::PRIMARY_LEVEL) {sum += stone.count;}else if (stone.level == SpriteStoneLevel::MIDDLE_LEVEL) {sum += stone.count * 10;}else if (stone.level == SpriteStoneLevel::ADVANCE_LEVEL) {sum += stone.count * 100;}if (this->level == SpriteStoneLevel::PRIMARY_LEVEL) {sum += this->count;}else if (this->level == SpriteStoneLevel::MIDDLE_LEVEL) {sum += this->count * 10;}else if (this->level == SpriteStoneLevel::ADVANCE_LEVEL) {sum += this->count * 100;}return SpriteStone(sum, SpriteStoneLevel::PRIMARY_LEVEL);
}SpriteStone SpriteStone::operator-(const SpriteStone & stone)
{int sum1 = 0;if (this->level == SpriteStoneLevel::PRIMARY_LEVEL) {sum1 += this->count;}else if (this->level == SpriteStoneLevel::MIDDLE_LEVEL) {sum1 += this->count * 10;}else if (this->level == SpriteStoneLevel::ADVANCE_LEVEL) {sum1 += this->count * 100;}int sum2 = 0;if (stone.level == SpriteStoneLevel::PRIMARY_LEVEL) {sum2 += stone.count;}else if (stone.level == SpriteStoneLevel::MIDDLE_LEVEL) {sum2 += stone.count * 10;}else if (stone.level == SpriteStoneLevel::ADVANCE_LEVEL) {sum2 += stone.count * 100;}return SpriteStone(sum1-sum2,SpriteStoneLevel::PRIMARY_LEVEL);
}int SpriteStone::getCount()
{return count;
}bool SpriteStone::operator>=(const SpriteStone & stone)
{int sum1 = 0;if (this->level == SpriteStoneLevel::PRIMARY_LEVEL) {sum1 = this->count;}else if (this->level == SpriteStoneLevel::MIDDLE_LEVEL) {sum1 = this->count * 10;}else if (this->level == SpriteStoneLevel::ADVANCE_LEVEL) {sum1 = this->count * 100;}int sum2 = 0;if (stone.level == SpriteStoneLevel::PRIMARY_LEVEL) {sum2 = stone.count;}else if (stone.level == SpriteStoneLevel::MIDDLE_LEVEL) {sum2 = stone.count * 10;}else if (stone.level == SpriteStoneLevel::ADVANCE_LEVEL) {sum2 = stone.count * 100;}return sum1 >= sum2;
}ostream& operator<<(ostream &os, SpriteStone& stone) {os << stone.ret();return os;
}

妖兽类
Monster.h

#pragma once
#include<string>
#include<iostream>class SpriteStone;
using namespace std;
//妖兽类
class Monster
{
public:
Monster(int level = 1,const string& category = "未知");SpriteStone getValue() const;int getPower() const;//获得妖兽的战斗力friend ostream& operator<<(ostream& os, const Monster& monster);friend bool operator == (const Monster& one,
const Monster& other);
private:string category; //妖兽的种类int level;//1-9级
};
ostream& operator<<(ostream& os, const Monster& monster);
bool operator == (const Monster& one, const Monster& other);

Monster.cpp

#include "Monster.h"
#include"SpriteStore.h"
#define MONSTER_LEVEL_FACTOR 1000Monster::Monster(int level, const string & category)
{this->level = level;this->category = category;
}SpriteStone Monster::getValue() const 
{int stoneCount[] = { 100,200,500,1000,2000,5000,10000,20000,100000 };int count = stoneCount[level - 1];return SpriteStone(count,SpriteStoneLevel::PRIMARY_LEVEL);
}int Monster::getPower() const
{int ret = level * MONSTER_LEVEL_FACTOR;return ret;
}ostream & operator<<(ostream & os, const Monster & monster)
{os << monster.level << "级" << monster.category << "妖兽";return os;
}bool operator ==(const Monster& one, const Monster& other) {if (one.category == other.category &&one.level == other.level) {return true;}else {return false;}
}

修仙者类
immortal.h

#pragma once
#include<string>
#include<vector>
#include<iostream>class SpriteStone;
class Monster;//修仙者级别
enum class ImmortalLevel {LIAN_QI,ZHU_JI,JIE_DAN,YUAN_YING,HUA_SHENG,LIAN_XU,HE_TI,DA_CHENG,DU_JIE
};
using namespace std;//修仙者类
class Immortal
{
public:Immortal(const char *name,const char *menPai, ImmortalLevel level);//挖矿void mining();//卖所有妖兽bool trade();//卖指定妖兽bool trade(const Monster& monster);//用自己的灵石,购买其他修仙者的指定妖兽bool trade(Immortal& other, const Monster& monster);//用自己指定的妖兽,来和其他修仙者换。bool trade(const Monster& monsterSource, Immortal& other,const Monster& monsterDest);//把自己的妖兽,卖给其他修仙者,换灵石bool trade(const Monster& monster, Immortal& other);//修仙者比武void fight(Immortal& other);//修仙者决斗bool duel(Immortal& other);//进阶函数void advances_success();//降阶函数void Down_falst();//修仙者闭关修仙,进阶。void Cultivation();int getPower() const;//捕获妖兽void fight(const Monster& monster);friend ostream& operator<<(ostream& os, const Immortal& immortal);void dead();//修仙者死亡后的处理
private:string name;string menPai; //门派ImmortalLevel level;//修仙者级别vector<SpriteStone> stones;//灵石资产vector<Monster> monsters;  //妖兽资产bool alive;//生死状态bool hadMonster(const Monster& monster);//判断是否有指定妖兽bool removeMonster(const Monster& monster);//移除指定妖兽
};
ostream& operator<<(ostream& os, const Immortal& immortal);
ostream& operator<<(ostream& os, const ImmortalLevel level);

immortal.cpp


#include<time.h>
#include "immortal.h"
#include"SpriteStore.h"
#include"Monster.h"#define IMMORTAL_LEVEL_FACTOR 1000
#define SPRITE_FACTOR         0.1
#define MONSTER_FACTOR        0.1Immortal::Immortal(const char * name, const char * menPai, ImmortalLevel level)
{this->name = name;this->menPai = menPai;this->level = level;this->alive = true;
}void Immortal::mining()
{stones.push_back(SpriteStone(100, SpriteStoneLevel::PRIMARY_LEVEL));
}bool Immortal::trade()
{if (!alive) {return false;}SpriteStone stone;for (int i = 0;i < monsters.size();i++) {stone = stone + monsters[i].getValue();} stones.push_back(stone);//擦除妖兽monsters.erase(monsters.begin(),monsters.end());return false;
}bool Immortal::trade(const Monster & monster)
{if (!alive) {return false;}//判断是否有指定的妖兽if (!hadMonster(monster)) {cout << name << "没有" << monster << endl;return false;}SpriteStone stone = monster.getValue();stones.push_back(stone);removeMonster(monster);return false;
}
bool Immortal::trade(Immortal & other, const Monster & monster)
{if (alive == false || other.alive == false) {return false;}if (!other.hadMonster(monster)) {cout << other.name << "没有" << monster << endl;return false;}//计算当前所有灵石总价SpriteStone stone;for (int i = 0;i < stones.size();i++) {stone = stone + stones[i];}if (stone >= monster.getValue()) {//购买SpriteStone valueStone = monster.getValue();stone = stone - valueStone;stones.clear();//清空袋子stones.push_back(stone);monsters.push_back(monster);other.removeMonster(monster);other.stones.push_back(valueStone);return true;}else {cout << "抱歉!" << name << "的灵石不够买" << monster << endl;return false;}
}bool Immortal::trade(const Monster & monsterSource, Immortal & other, const Monster & monsterDest)
{if (alive == false || other.alive == false) {return false;}if(monsterSource == monsterDest || !hadMonster(monsterSource)||!other.hadMonster(monsterDest)||!(monsterSource.getValue()>=monsterDest.getValue())){return false;}removeMonster(monsterSource);other.removeMonster(monsterDest);monsters.push_back(monsterDest);other.monsters.push_back(monsterSource);return true;
}bool Immortal::trade(const Monster & monster, Immortal & other)
{if (alive == false || other.alive == false) {return false;}if (!hadMonster(monster)) {cout << name << "没有" << monster << endl;}SpriteStone otherStone;for (int i = 0;i < other.stones.size();i++) {otherStone = otherStone + other.stones[i];}if (!(otherStone >= monster.getValue())) {return false;}//对方出钱otherStone = otherStone - monster.getValue();other.stones.clear();other.stones.push_back(otherStone);//对方收货other.monsters.push_back(monster);//自己移除妖兽,收钱this->removeMonster(monster);this->stones.push_back(monster.getValue());return true;
}void Immortal::fight(Immortal & other)
{if (alive == false || other.alive == false) {cout << "修仙者以亡,无法开始切磋!";exit(0);}cout << this->name << "和" << other.name << "比武中...   ";if (this->getPower() > other.getPower()) {cout << this->name << "的武力更加深厚" << endl;}else if (this->getPower() < other.getPower()) {cout << other.name << "的武力更加深厚" << endl;}else {cout << this->name << other.name <<"的武力相当" << endl;}
}bool Immortal::duel(Immortal & other)
{if (alive == false || other.alive == false) {cout << "修仙者以亡,无法开始决斗!";return false;} cout << this->name << "和" << other.name << "决斗中...   ";if (this->getPower() > other.getPower()) {cout << this->name << "杀死了!" << other.name<<"——并收刮了" << other.name << "所有宝物!" << endl;//计算死亡者灵石,加入胜利方SpriteStone stone;for (int i = 0;i < other.stones.size();i++) {this->stones.push_back(stones[i]);}//计算死亡者妖兽,加入胜利方Monster monster;for (int i = 0;i < other.monsters.size();i++) {monsters.push_back(monsters[i]);}//死掉other.dead();}else if (this->getPower() < other.getPower()) {cout << other.name << "杀死了!" << this->name <<"    并收刮了"<< this->name <<"所有宝物!"<<endl;//计算死亡者灵石,加入胜利方SpriteStone stone;for (int i = 0;i < stones.size();i++) {other.stones.push_back(stones[i]);}//计算死亡者妖兽,加入胜利方Monster monster;for (int i = 0;i < monsters.size();i++) {other.monsters.push_back(monsters[i]);}//死掉this->dead();}else {cout << this->name << other.name << "的武力相当,隔日在战!"<< endl;}return true;
}void Immortal::Cultivation()
{//初级进阶cout <<this->name<<"在闭关修炼中"<< endl;if ((int)(this->level) <= 2) {cout << "初级进阶成功率为%80,需要消耗100灵石,祝您进阶成功!";srand((unsigned)time(NULL));if (rand()%10 <= 8) {advances_success();cout << "恭喜您进阶成功!等级加 +1" << endl;}else {cout << "进阶失败! 等级减 -1 " << endl;Down_falst();}SpriteStone stone;for (int i = 0;i < stones.size();i++) {stone = stone + stones[i];}stones.clear();stone = stone - 100;stones.push_back(stone);}//中级进阶if (2<(int)(this->level)&& (int)(this->level)<=6) {cout << "中级进阶成功率为%50,需要消耗200灵石,祝您进阶成功!";if (rand()%10 <= 4) {advances_success();cout << "恭喜您进阶成功!等级加 +1" << endl;}else {cout << "进阶失败! 等级减 -1 " << endl;Down_falst();}SpriteStone stone;for (int i = 0;i < stones.size();i++) {stone = stone + stones[i];}stones.clear();stone = stone - 200;stones.push_back(stone);}//高级进阶if ((int)(this->level)>6&& (int)(this->level)<9) {cout << "高级进阶成功率为%30,需要消耗300灵石,祝您进阶成功!";if (rand() % 10 <= 2) {void advances_success();cout << "恭喜您进阶成功!等级加 +1" << endl;}else {cout << "进阶失败! 等级减 -1" << endl;Down_falst();}cout << (int)(this->level) << endl;SpriteStone stone;for (int i = 0;i < stones.size();i++) {stone = stone + stones[i];}stones.clear();stone = stone - 300;stones.push_back(stone);}
}void Immortal::advances_success()
{switch (this->level){case ImmortalLevel::LIAN_QI:this->level = ImmortalLevel::ZHU_JI;break;case ImmortalLevel::ZHU_JI:this->level = ImmortalLevel::JIE_DAN;break;case ImmortalLevel::JIE_DAN:this->level = ImmortalLevel::YUAN_YING;break;case ImmortalLevel::YUAN_YING:this->level = ImmortalLevel::HUA_SHENG;break;case ImmortalLevel::HUA_SHENG:this->level = ImmortalLevel::LIAN_XU;break;case ImmortalLevel::LIAN_XU:this->level = ImmortalLevel::HE_TI;break;case ImmortalLevel::HE_TI:this->level = ImmortalLevel::DA_CHENG;break;case ImmortalLevel::DA_CHENG:this->level = ImmortalLevel::DU_JIE;break;}
}void Immortal::Down_falst()
{switch (this->level){case ImmortalLevel::DU_JIE:this->level = ImmortalLevel::DA_CHENG;break;case ImmortalLevel::DA_CHENG:this->level = ImmortalLevel::HE_TI;break;case ImmortalLevel::HE_TI:this->level = ImmortalLevel::LIAN_XU;break;case ImmortalLevel::LIAN_XU:this->level = ImmortalLevel::HUA_SHENG;break;case ImmortalLevel::HUA_SHENG:this->level = ImmortalLevel::YUAN_YING;break;case ImmortalLevel::YUAN_YING:this->level = ImmortalLevel::JIE_DAN;break;case ImmortalLevel::JIE_DAN:this->level = ImmortalLevel::ZHU_JI;break;case ImmortalLevel::ZHU_JI:this->level = ImmortalLevel::LIAN_QI;break;}
}int Immortal::getPower() const
{//计算该级别的战斗力int ret = ((int)level + 1)* IMMORTAL_LEVEL_FACTOR;//计算灵石助攻的战斗力//SpriteStone stone;for (int i = 0;i < stones.size();i++) {stone = stone + stones[i];}ret += stone.getCount()* SPRITE_FACTOR;//计算所有妖兽的助攻for (int i = 0;i < monsters.size();i++) {ret += monsters[i].getPower() * MONSTER_FACTOR;}return ret;
}void Immortal::fight(const Monster & monster)
{int selfPower = getPower();int monsterPower = monster.getPower();if (selfPower > monsterPower) {monsters.push_back(monster);}else if(selfPower < monsterPower){dead();}
}void Immortal::dead()
{alive = false;stones.erase(stones.begin(), stones.end());monsters.erase(monsters.begin(),monsters.end());
}bool Immortal::hadMonster(const Monster & monster)
{for (int i = 0;i < monsters.size();i++) {if (monster == monsters[i]) {return true;}}return false;
}bool Immortal::removeMonster(const Monster & monster)
{//迭代器 特殊指针vector<Monster>::iterator it = monsters.begin();while (it != monsters.end()) {if (*it == monster) {it = monsters.erase(it);//移除指定妖兽 返回下一个妖兽return true;}else {it++;      //迭代器 自加}}return true;
}ostream & operator<<(ostream & os, const Immortal & immortal)
{os << "[修仙者]" << immortal.name << (immortal.alive ? "[在修]" : "[以亡]")<< "\t门派:" << immortal.menPai<< "\t级别:" << immortal.level;SpriteStone stone;for (int i = 0;i < immortal.stones.size();i++) {stone = stone + immortal.stones[i];}os << "\t灵石:折合" << stone;os << "\t妖兽:";if (immortal.monsters.size() == 0) {os << "无";}else {for (int i = 0;i < immortal.monsters.size();i++) {os << immortal.monsters[i] << " ";}}return os;
}ostream & operator<<(ostream & os, const ImmortalLevel level)
{switch (level){case ImmortalLevel::LIAN_QI:os << "练气期";break;case ImmortalLevel::ZHU_JI:os << "筑基期";break;case ImmortalLevel::JIE_DAN:os << "结丹期";break;case ImmortalLevel::YUAN_YING:os << "元婴期";break;case ImmortalLevel::HUA_SHENG:os << "化神期";break;case ImmortalLevel::LIAN_XU:os << "炼虚期";break;case ImmortalLevel::HE_TI:os << "合体期";break;case ImmortalLevel::DA_CHENG:os << "大乘期";break;case ImmortalLevel::DU_JIE:os << "渡劫期";break;}return os;
}

main.cpp
测试

#include "SpriteStore.h"
#include"Monster.h"
#include"immortal.h"
#include<iostream>void testSpriteStore() {SpriteStone stone(100, SpriteStoneLevel::ADVANCE_LEVEL);cout << stone << endl;
}void testMonster() {Monster monster(5, "蛟龙");monster.getValue();cout << monster << endl;
}void testImmortal() {Immortal rock("Rock", "奇牛", ImmortalLevel::LIAN_QI);cout << rock << endl;for (int i = 0;i < 50;i++) {rock.mining();}cout << rock << endl;Monster monster(1, "蛟龙");rock.fight(monster);cout << "捕获" << monster << "之后: "<<endl;cout << rock << endl;Monster monster2(1, "蜘蛛");rock.fight(monster2);Monster monster3(1, "壁虎");rock.fight(monster3);cout << "捕获:" << monster2 << monster3 <<"之后"<<endl;cout << rock << endl;rock.trade(monster2);cout << "售卖" << monster2 << "之后" << endl;cout << rock << endl;rock.trade();cout << "售卖所有妖兽之后" << endl;cout << rock << endl;Immortal hanni("韩立", "黄枫谷", ImmortalLevel::JIE_DAN);cout << hanni << endl;Monster monster4(2,"蛟龙");hanni.fight(monster4);cout << hanni << endl;//买妖兽rock.trade(hanni, monster4);cout << "贸易之后" << endl;cout << rock << endl;cout << hanni << endl;//捕获妖兽Monster monster5(1, "天蚕");hanni.fight(monster5);cout << "韩立捕获了" << monster5 << endl;cout << hanni << endl;//换妖兽cout << "Rock使用" << monster4 << "和韩立交换" << monster5 << endl;rock.trade(monster4, hanni, monster5);cout << rock << endl;cout << hanni << endl;//卖妖兽hanni.trade(monster4, rock);cout << "韩立卖" << monster4 << "给Rock" << endl;cout << rock << endl;cout << hanni << endl;//友谊比武rock.fight(hanni);//生死决斗rock.duel(hanni);cout << rock << endl;cout << hanni << endl;//进阶hanni.Cultivation();cout << hanni << endl;
}int main(void) {testSpriteStore();testMonster();testImmortal();system("pause");
}

运行结果:
在这里插入图片描述

这篇关于修仙类 小练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/208278

相关文章

RabbitMQ练习(AMQP 0-9-1 Overview)

1、What is AMQP 0-9-1 AMQP 0-9-1(高级消息队列协议)是一种网络协议,它允许遵从该协议的客户端(Publisher或者Consumer)应用程序与遵从该协议的消息中间件代理(Broker,如RabbitMQ)进行通信。 AMQP 0-9-1模型的核心概念包括消息发布者(producers/publisher)、消息(messages)、交换机(exchanges)、

【Rust练习】12.枚举

练习题来自:https://practice-zh.course.rs/compound-types/enum.html 1 // 修复错误enum Number {Zero,One,Two,}enum Number1 {Zero = 0,One,Two,}// C语言风格的枚举定义enum Number2 {Zero = 0.0,One = 1.0,Two = 2.0,}fn m

MySql 事务练习

事务(transaction) -- 事务 transaction-- 事务是一组操作的集合,是一个不可分割的工作单位,事务会将所有的操作作为一个整体一起向系统提交或撤销请求-- 事务的操作要么同时成功,要么同时失败-- MySql的事务默认是自动提交的,当执行一个DML语句,MySql会立即自动隐式提交事务-- 常见案例:银行转账-- 逻辑:A给B转账1000:1.查询

html css jquery选项卡 代码练习小项目

在学习 html 和 css jquery 结合使用的时候 做好是能尝试做一些简单的小功能,来提高自己的 逻辑能力,熟悉代码的编写语法 下面分享一段代码 使用html css jquery选项卡 代码练习 <div class="box"><dl class="tab"><dd class="active">手机</dd><dd>家电</dd><dd>服装</dd><dd>数码</dd><dd

014.Python爬虫系列_解析练习

我 的 个 人 主 页:👉👉 失心疯的个人主页 👈👈 入 门 教 程 推 荐 :👉👉 Python零基础入门教程合集 👈👈 虚 拟 环 境 搭 建 :👉👉 Python项目虚拟环境(超详细讲解) 👈👈 PyQt5 系 列 教 程:👉👉 Python GUI(PyQt5)文章合集 👈👈 Oracle数据库教程:👉👉 Oracle数据库文章合集 👈👈 优

如何快速练习键盘盲打

盲打是指在不看键盘的情况下进行打字,这样可以显著提高打字速度和效率。以下是一些练习盲打的方法: 熟悉键盘布局:首先,你需要熟悉键盘上的字母和符号的位置。可以通过键盘图或者键盘贴纸来帮助记忆。 使用在线打字练习工具:有许多在线的打字练习网站,如Typing.com、10FastFingers等,它们提供了不同难度的练习和测试。 练习基本键位:先从学习手指放在键盘上的“家位”开始,通常是左手的

anaconda3下的python编程练习-csv翻译器

相关理解和命令 一、环境配置1、conda命令2、pip命令3、python命令 二、开发思路三、开发步骤 一、环境配置 1、conda命令 镜像源配置 conda config --show channels //查看镜像源conda config --remove-key channels //删除添加源,恢复默认源#添加镜像源conda config --ad

推荐练习键盘盲打的网站

对于初学者来说,以下是一些推荐的在线打字练习网站: 打字侠:这是一个专业的在线打字练习平台,提供科学合理的课程设置和个性化学习计划,适合各个水平的用户。它还提供实时反馈和数据分析,帮助你提升打字速度和准确度。 dazidazi.com:这个网站提供了基础的打字练习,适合初学者从零开始学习打字。 Type.fun打字星球:提供了丰富的盲打课程和科学的打字课程设计,还有诗词歌赋、经典名著等多样

综合DHCP、ACL、NAT、Telnet和PPPoE进行网络设计练习

描述:企业内网和运营商网络如上图所示。 公网IP段:12.1.1.0/24。 内网IP段:192.168.1.0/24。 公网口PPPOE 拨号采用CHAP认证,用户名:admin 密码:Admin@123 财务PC 配置静态IP:192.168.1.8 R1使用模拟器中的AR201型号,作为交换路由一体机,下图的WAN口为E0/0/8口,可以在该接口下配置IP地址。 可以通过

JAVA学习-练习试用Java实现“删除有序数组中的重复项”

问题: 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 说明: 为什么返回数值是整数,但输出的答案是数组呢? 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。 你可以想象内部操作如下