鱼C论坛

 找回密码
 立即注册
查看: 2376|回复: 10

[已解决]求助两个C语言题目,最好有注释

[复制链接]
发表于 2018-12-2 16:53:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Taessica 于 2018-12-3 18:50 编辑

1、毒草的入侵
一份自定义大小的苹坪,其中有三种土地形式,普通草,毒草,石头。第0个星期毒草只有一颗。每个星期,毒草会传到已被毒草占领的格子四面八方的每一个没有石头的格(包括垂直与水平相邻的和对角线上相邻的格)1周之后,这些新占领的格又可以把
毒草传到更多的格里面了。
草地由一个图片表示。"M"表示毒草,”.”表示草,而”*”表示大石头.比如这个x=4y=3的例子
.  .  .  .
.  .  * .
M * * .

如果毒草一开始在左下角(第1排,第1列),那么草地的地图将会以如下态势发展
                .  .  .  .        .  .   .  .       M M M .          M M M M         M M M M
                .  .  * .       M M *  .      M  M * .          M M  * M         M M * M
                M * * .      M  * *  .      M  *  * .          M *  *  .          M *  *  M
星期数         0                1                  2                    3                     4
毒草会在4星期后占领整片土地。

输入                                                                                           输出
4 3 1 1(土地大小和毒草起始位置)                                              4星期
.  .  .  .
.  .  * .
M * * .




2、最大子树和
一株奇怪的花卉,上面共连有N朵花,共有N+1条枝条将花连在一起,并且未修剪时每
朵花都不是孤立的。每朵花都有一个“美丽指数”,该数越大说明这朵花越漂亮,也有“美
丽指数”为负数的,说明这朵花看着都让人恶心所谓“修剪”,意为去掉其中的一条
枝条,这样一株花就成了两株,扔掉其中一株经过一系列“修剪“之后,还剩下最后一
株花(也可能是一朵)。老师的任务就是:通过一系列“修剪”(也可以什么“修剪”都
不进行),使剩下的那株(那朵)花卉上所有花朵的“美丽指数”之和最大。第一行
第一行输入N朵花
第二行输入每朵花的美丽指数
输出最大美丽指数和
最佳答案
2018-12-3 12:44:38
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <ctime>
  5. #include <windows.h>

  6. enum {STONE = '*', GRASS = '.', BADLY = 'M'};

  7. const std::vector<std::vector<char>> GenerateMap(size_t width, size_t height)
  8. {
  9.         std::vector<std::vector<char>> v;
  10.         v.resize(height);
  11.         for(auto &i: v)
  12.                 i.resize(width);

  13.         std::default_random_engine e((unsigned int)time(nullptr));
  14.         std::uniform_int_distribution<size_t> u(0, 2);
  15.         for(auto h = v.begin(); h != v.end(); ++h)
  16.         {
  17.                 for(auto w = h->begin(); w != h->end(); ++w)
  18.                 {
  19.                         if(u(e))
  20.                                 *w = STONE;
  21.                         else
  22.                                 *w = GRASS;
  23.                 }
  24.         }
  25.         return v;
  26. }

  27. void PrintMap(const std::vector<std::vector<char>> &map)
  28. {
  29.         for(auto h = map.begin(); h != map.end(); ++h)
  30.         {
  31.                 for(auto w = h->begin(); w != h->end(); ++w)
  32.                 {
  33.                         std::cout << *w << " ";
  34.                 }
  35.                 std::cout << std::endl;
  36.         }
  37. }

  38. inline bool ConditionInfection(std::vector<std::vector<char>> &map, int x, int y)
  39. {
  40.         if((y >= 0) && (size_t(y) < map.size()))
  41.                 if((x >= 0) && (size_t(x) < map[0].size()))
  42.                         if((map[y][x] != STONE) && (map[y][x] != BADLY))
  43.                         {
  44.                                 map[y][x] = BADLY;
  45.                                 return true;
  46.                         }
  47.         return false;
  48. }

  49. bool Infection(std::vector<std::vector<char>> &map)
  50. {
  51.         std::vector<POINT> v;
  52.         for(auto h = map.begin(); h != map.end(); ++h)
  53.         {
  54.                 for(auto w = h->begin(); w != h->end(); ++w)
  55.                 {
  56.                         if(*w == BADLY)
  57.                                 v.push_back({w - h->begin(), h - map.begin()});
  58.                 }
  59.         }

  60.         // 下面进行感染
  61.         bool flag = false;
  62.         for(auto &i : v)
  63.         {
  64.                 if(ConditionInfection(map, i.x, i.y - 1))        // 上
  65.                         flag = true;

  66.                 if(ConditionInfection(map, i.x, i.y + 1))        // 下
  67.                         flag = true;

  68.                 if(ConditionInfection(map, i.x - 1, i.y))        // 左
  69.                         flag = true;

  70.                 if(ConditionInfection(map, i.x + 1, i.y))        // 右
  71.                         flag = true;

  72.                 if(ConditionInfection(map, i.x + 1, i.y + 1))        // 右下角
  73.                         flag = true;

  74.                 if(ConditionInfection(map, i.x + 1, i.y - 1))        // 右上角
  75.                         flag = true;

  76.                 if(ConditionInfection(map, i.x - 1, i.y + 1))        // 左下角
  77.                         flag = true;

  78.                 if(ConditionInfection(map, i.x - 1, i.y - 1))        // 左上角
  79.                         flag = true;
  80.         }
  81.         return flag;
  82. }

  83. int main()
  84. {
  85.         std::cout << "请输入: ";
  86.         size_t width, height, x, y;
  87.         std::cin >> width >> height >> x >> y;

  88.         std::vector<std::vector<char>> map = GenerateMap(width, height);
  89.         map[y][x] = BADLY;        // 这里就使用 原点在左上角,有什么理由使用左下角的原点?
  90.        
  91.         PrintMap(map);
  92.         while(Infection(map))
  93.         {
  94.                 std::cout << "*****************" << std::endl;
  95.                 PrintMap(map);
  96.         }

  97.         return 0;
  98. }
复制代码

  1. 请输入: 4 3 1 1
  2. . . * *
  3. * M * *
  4. * . * *
  5. *****************
  6. M M * *
  7. * M * *
  8. * M * *
  9. 请按任意键继续. . .
复制代码

  1. 请输入: 4 3 0 0
  2. M * * *
  3. * * . *
  4. * * * *
  5. 请按任意键继续. . .
复制代码

  1. 请输入: 4 3 0 0
  2. M * * .
  3. . * * .
  4. . . * .
  5. *****************
  6. M * * .
  7. M * * .
  8. . . * .
  9. *****************
  10. M * * .
  11. M * * .
  12. M M * .
  13. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-2 17:46:54 | 显示全部楼层
一份自定义大小的苹坪,其中有三种土地形式,普通草,草,石头。第个基期
毒草只有一颗


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-2 17:49:11 | 显示全部楼层
第一题我好像看懂了,好像不难
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-2 19:33:27 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-12-2 17:49
第一题我好像看懂了,好像不难

完整的代码能写下来吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-3 12:26:12 | 显示全部楼层
一份自定义大小的苹坪,其中有三种土地形式,普通草,草,石头。

提问题要认真呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-3 12:37:33 | 显示全部楼层
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <ctime>
  5. #include <windows.h>

  6. enum {STONE = '*', GRASS = '.', BADLY = 'M'};

  7. const std::vector<std::vector<char>> GenerateMap(size_t width, size_t height)
  8. {
  9.         std::vector<std::vector<char>> v;
  10.         v.resize(height);
  11.         for(auto &i: v)
  12.                 i.resize(width);

  13.         std::default_random_engine e((unsigned int)time(nullptr));
  14.         std::uniform_int_distribution<size_t> u(0, 2);
  15.         for(auto h = v.begin(); h != v.end(); ++h)
  16.         {
  17.                 for(auto w = h->begin(); w != h->end(); ++w)
  18.                 {
  19.                         if(u(e))
  20.                                 *w = STONE;
  21.                         else
  22.                                 *w = GRASS;
  23.                 }
  24.         }
  25.         return v;
  26. }

  27. void PrintMap(const std::vector<std::vector<char>> &map)
  28. {
  29.         for(auto h = map.begin(); h != map.end(); ++h)
  30.         {
  31.                 for(auto w = h->begin(); w != h->end(); ++w)
  32.                 {
  33.                         std::cout << *w << " ";
  34.                 }
  35.                 std::cout << std::endl;
  36.         }
  37. }

  38. inline bool ConditionInfection(std::vector<std::vector<char>> &map, int x, int y)
  39. {
  40.         if((y >= 0) && (size_t(y) < map.size()))
  41.                 if((x >= 0) && (size_t(x) < map[0].size()))
  42.                         if((map[y][x] != STONE) && (map[y][x] != BADLY))
  43.                         {
  44.                                 map[y][x] = BADLY;
  45.                                 return true;
  46.                         }
  47.         return false;
  48. }

  49. bool Infection(std::vector<std::vector<char>> &map)
  50. {
  51.         std::vector<POINT> v;
  52.         for(auto h = map.begin(); h != map.end(); ++h)
  53.         {
  54.                 for(auto w = h->begin(); w != h->end(); ++w)
  55.                 {
  56.                         if(*w == BADLY)
  57.                                 v.push_back({w - h->begin(), h - map.begin()});
  58.                 }
  59.         }

  60.         // 下面进行感染
  61.         bool flag = false;
  62.         for(auto &i : v)
  63.         {
  64.                 if(ConditionInfection(map, i.x, i.y - 1))        // 上
  65.                         flag = true;

  66.                 if(ConditionInfection(map, i.x, i.y + 1))        // 下
  67.                         flag = true;

  68.                 if(ConditionInfection(map, i.x - 1, i.y))        // 左
  69.                         flag = true;

  70.                 if(ConditionInfection(map, i.x + 1, i.y))        // 右
  71.                         flag = true;

  72.                 if(ConditionInfection(map, i.x + 1, i.y + 1))        // 右下角
  73.                         flag = true;

  74.                 if(ConditionInfection(map, i.x + 1, i.y - 1))        // 右上角
  75.                         flag = true;

  76.                 if(ConditionInfection(map, i.x - 1, i.y + 1))        // 左下角
  77.                         flag = true;

  78.                 if(ConditionInfection(map, i.x - 1, i.y - 1))        // 左上角
  79.                         flag = true;
  80.         }
  81.         return flag;
  82. }

  83. int main()
  84. {
  85.         std::vector<std::vector<char>> map = {
  86.                 {GRASS, GRASS, GRASS, GRASS},
  87.                 {GRASS, GRASS, STONE, GRASS},
  88.                 {BADLY, STONE, STONE, GRASS},
  89.         };
  90.        
  91.         PrintMap(map);
  92.         while(Infection(map))
  93.         {
  94.                 std::cout << "*****************" << std::endl;
  95.                 PrintMap(map);
  96.         }

  97.         return 0;
  98. }
复制代码

  1. . . . .
  2. . . * .
  3. M * * .
  4. *****************
  5. . . . .
  6. M M * .
  7. M * * .
  8. *****************
  9. M M M .
  10. M M * .
  11. M * * .
  12. *****************
  13. M M M M
  14. M M * M
  15. M * * .
  16. *****************
  17. M M M M
  18. M M * M
  19. M * * M
  20. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-3 12:44:38 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <ctime>
  5. #include <windows.h>

  6. enum {STONE = '*', GRASS = '.', BADLY = 'M'};

  7. const std::vector<std::vector<char>> GenerateMap(size_t width, size_t height)
  8. {
  9.         std::vector<std::vector<char>> v;
  10.         v.resize(height);
  11.         for(auto &i: v)
  12.                 i.resize(width);

  13.         std::default_random_engine e((unsigned int)time(nullptr));
  14.         std::uniform_int_distribution<size_t> u(0, 2);
  15.         for(auto h = v.begin(); h != v.end(); ++h)
  16.         {
  17.                 for(auto w = h->begin(); w != h->end(); ++w)
  18.                 {
  19.                         if(u(e))
  20.                                 *w = STONE;
  21.                         else
  22.                                 *w = GRASS;
  23.                 }
  24.         }
  25.         return v;
  26. }

  27. void PrintMap(const std::vector<std::vector<char>> &map)
  28. {
  29.         for(auto h = map.begin(); h != map.end(); ++h)
  30.         {
  31.                 for(auto w = h->begin(); w != h->end(); ++w)
  32.                 {
  33.                         std::cout << *w << " ";
  34.                 }
  35.                 std::cout << std::endl;
  36.         }
  37. }

  38. inline bool ConditionInfection(std::vector<std::vector<char>> &map, int x, int y)
  39. {
  40.         if((y >= 0) && (size_t(y) < map.size()))
  41.                 if((x >= 0) && (size_t(x) < map[0].size()))
  42.                         if((map[y][x] != STONE) && (map[y][x] != BADLY))
  43.                         {
  44.                                 map[y][x] = BADLY;
  45.                                 return true;
  46.                         }
  47.         return false;
  48. }

  49. bool Infection(std::vector<std::vector<char>> &map)
  50. {
  51.         std::vector<POINT> v;
  52.         for(auto h = map.begin(); h != map.end(); ++h)
  53.         {
  54.                 for(auto w = h->begin(); w != h->end(); ++w)
  55.                 {
  56.                         if(*w == BADLY)
  57.                                 v.push_back({w - h->begin(), h - map.begin()});
  58.                 }
  59.         }

  60.         // 下面进行感染
  61.         bool flag = false;
  62.         for(auto &i : v)
  63.         {
  64.                 if(ConditionInfection(map, i.x, i.y - 1))        // 上
  65.                         flag = true;

  66.                 if(ConditionInfection(map, i.x, i.y + 1))        // 下
  67.                         flag = true;

  68.                 if(ConditionInfection(map, i.x - 1, i.y))        // 左
  69.                         flag = true;

  70.                 if(ConditionInfection(map, i.x + 1, i.y))        // 右
  71.                         flag = true;

  72.                 if(ConditionInfection(map, i.x + 1, i.y + 1))        // 右下角
  73.                         flag = true;

  74.                 if(ConditionInfection(map, i.x + 1, i.y - 1))        // 右上角
  75.                         flag = true;

  76.                 if(ConditionInfection(map, i.x - 1, i.y + 1))        // 左下角
  77.                         flag = true;

  78.                 if(ConditionInfection(map, i.x - 1, i.y - 1))        // 左上角
  79.                         flag = true;
  80.         }
  81.         return flag;
  82. }

  83. int main()
  84. {
  85.         std::cout << "请输入: ";
  86.         size_t width, height, x, y;
  87.         std::cin >> width >> height >> x >> y;

  88.         std::vector<std::vector<char>> map = GenerateMap(width, height);
  89.         map[y][x] = BADLY;        // 这里就使用 原点在左上角,有什么理由使用左下角的原点?
  90.        
  91.         PrintMap(map);
  92.         while(Infection(map))
  93.         {
  94.                 std::cout << "*****************" << std::endl;
  95.                 PrintMap(map);
  96.         }

  97.         return 0;
  98. }
复制代码

  1. 请输入: 4 3 1 1
  2. . . * *
  3. * M * *
  4. * . * *
  5. *****************
  6. M M * *
  7. * M * *
  8. * M * *
  9. 请按任意键继续. . .
复制代码

  1. 请输入: 4 3 0 0
  2. M * * *
  3. * * . *
  4. * * * *
  5. 请按任意键继续. . .
复制代码

  1. 请输入: 4 3 0 0
  2. M * * .
  3. . * * .
  4. . . * .
  5. *****************
  6. M * * .
  7. M * * .
  8. . . * .
  9. *****************
  10. M * * .
  11. M * * .
  12. M M * .
  13. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-3 12:54:15 | 显示全部楼层
本帖最后由 人造人 于 2018-12-3 12:56 编辑

一株奇怪的花卉,上面共连有N朵花,共有N1条枝千将花几连在一起,并且未修剪时每
朵花都不是孤立的。

N1   ???
你想说的是 N + 1  ???

千将花几   ??? 什么鬼 ?
这里我实在联想不出正确问题,应该能猜出这里是写错字了,应该是吧?

再说一次,问问题要认真,你认真的问问题,别人认真的给你回答,你匆匆忙忙提问,别人也匆匆忙忙回答,这样好吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-3 12:59:34 | 显示全部楼层
第二题在修正错别字的基础上最好举个例子,像第一题那样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-3 18:51:51 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-12-3 12:54
一株奇怪的花卉,上面共连有N朵花,共有N1条枝千将花几连在一起,并且未修剪时每
朵花都不是孤立的。


抱歉,我确实是打得有点快,而且没有校对,谢谢你的批评,也谢谢你肯帮忙。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-3 18:52:44 From FishC Mobile | 显示全部楼层
人造人 发表于 2018-12-3 12:59
第二题在修正错别字的基础上最好举个例子,像第一题那样

谢谢,第二题我已经能做出来了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 23:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表