这个教程属于哪个教程 | C语言程序设计II |
这次作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/homework/3204 |
我在这个课程的目标是 | 递归函数的概念 |
这个具体在哪个方面帮助我实现目标的 | 玩类似于搬动盘子的游戏 |
参考文献 | c语言程序设书以及百度 |
汉诺塔是一个源于印度古老传说的益智玩具。据说大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘,大梵天命令僧侣把圆盘移到另一根柱子上,并且规定:在小圆盘上不能放大圆盘,每次只能移动一个圆盘。当所有圆盘都移到另一根柱子上时,世界就会毁灭。
请编写程序,输入汉诺塔圆片的数量,输出移动汉诺塔的步骤。
输入格式:
圆盘数 起始柱 目的柱 过度柱
输出格式:
移动汉诺塔的步骤 每行显示一步操作,具体格式为: 盘片号: 起始柱 -> 目的柱 其中盘片号从 1 开始由小到大顺序编号。
输入样例:
3 a c b
输出样例:
1: a -> c 2: a -> b 1: c -> b 3: a -> c 1: b -> a 2: b -> c 1: a -> c
实验代码:
#include<stdio.h> void hano(int n,char x,char y,char z); int main () { int n; char x,y,z; scanf("%d ",&n); scanf("%c %c %c ",&x,&y,&z); hano(n,x,y,z); } void hano(int n,char x,char y,char z) { if(n==1){ printf("%d: %c -> %c ",n,x,y); } else{ hano(n-1,x,z,y); printf("%d: %c -> %c ",n,x,y); hano(n-1,z,y,x); } }
设计思路:
运行结果截图:
以上图片来自新浪微博。
本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:
- 无论用户说什么,首先把对方说的话在一行中原样打印出来;
- 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
- 把原文中所有大写英文字母变成小写,除了
I
; - 把原文中所有独立的
can you
、could you
对应地换成I can
、I could
—— 这里“独立”是指被空格或标点符号分隔开的单词; - 把原文中所有独立的
I
和me
换成you
; - 把原文中所有的问号
?
换成惊叹号!
; - 在一行中输出替换后的句子作为 AI 的回答。
输入格式:
输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。
输出格式:
按题面要求输出,每个 AI 的回答前要加上 AI:
和一个空格。
输入样例:
6 Hello ? Good to chat with you can you speak Chinese? Really? Could you show me 5 What Is this prime? I,don 't know
输出样例:
Hello ? AI: hello! Good to chat with you AI: good to chat with you can you speak Chinese? AI: I can speak chinese! Really? AI: really! Could you show me 5 AI: I could show you 5 What Is this prime? I,don 't know AI: what Is this prime! you,don't know
emmmm这题我不会做,问我的搭档他也不会......百度了老半天不是看不懂就是不对。这题要分别将要改的东西改成所需的答案,面对这种题目该怎么做QAQ
预习作业:
指针常量:自身值不能被修改,但指针可以修改指向。
#include <stdio.h> int main() { int num = 123; int const cnum = 111; int const *p = &cnum; printf("cnum: %d, &cnum: %p ", cnum, &cnum); printf("*p: %d, p: %p ", *p, p); *p = 1235; p = num; printf("cnum: %d, &cnum: %p ", cnum, &cnum); printf("*p: %d, p: %p ", *p, p); num = 110; printf("cnum: %d, &cnum: %p ", num, &num); printf("*p: %d, p: %p ", *p, p); return 0; }
(出自:https://blog.csdn.net/u013283956/article/details/80065015)
常量指针:自身值可以修改,但指向不能被修改。
#include <stdio.h> int main() { int num = 123; int const cnum = 111; int * const p = &cnum; *p = 1024; printf("*p: %d ", *p); num = 110; printf("*p: %d, num = %d ", *p, num); p = &cnum; return 0; }
(出自:https://blog.csdn.net/u013283956/article/details/80065015)
#include <stdio.h> float *find(float(*pionter)[4],int n);//函数声明 int main(void) { static float score[][4]={{60,70,80,90},{56,89,34,45},{34,23,56,45}}; float *p; int i,m; printf("Enter the number to be found:"); scanf("%d",&m); printf("the score of NO.%d are: ",m); p=find(score,m-1); for(i=0;i<4;i++) printf("%5.2f ",*(p+i)); return 0; } float *find(float(*pionter)[4],int n)/*定义指针函数*/ { float *pt; pt=*(pionter+n); return(pt); }
(出自:https://baike.baidu.com/item/指针函数/2641780?fr=aladdin)
函数指针:函数指针是指向函数的指针变量。 因此“函数指针”本身首先应是指针变量,只不过该指针变量指向函数。这正如用指针变量可指向整形变量、字符型、数组一样,这里是指向函数.
#include<stdio.h> int max(int x,int y){return (x>y? x:y);} int main() { int (*ptr)(int, int); int a, b, c; ptr = max; scanf("%d%d", &a, &b); c = (*ptr)(a,b); printf("a=%d, b=%d, max=%d", a, b, c); return 0; }
(出自:https://baike.baidu.com/item/函数指针/2674905?fr=aladdin)
二级指针:A(即B的地址)是指向指针的指针,称为二级指针,用于存放二级指针的变量称为二级指针变量.根据B的不同情况,二级指针又分为指向指针变量的指针和指向数组的指针。
int fun(void) { int *buf ; int ret ; ret = mem_init(&buf); return ret; } int mem_init(int **buf_t) { *buf_t = malloc(100); return 1; }
(出自:https://baike.baidu.com/item/二级指针/303561?fr=aladdin)
单向链表:特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点。
void scan() { int id; Node *list_temp; list_head = NULL; list_temp = list_head; cout << “请输入id和score:” << endl; while(cin >> id,id) { Node *list_body = new Node(); list_body->id = id; cin >> list_body->score; list_body->next = NULL; if(list_head == NULL) list_head = list_body; else list_temp->next = list_body; list_temp = list_body; } }
(出自:https://baike.baidu.com/item/单向链表/8671935?fr=aladdin)
学习感悟:这一周学习了函数嵌套、递归函数、递归程序设计、宏定义等知识点,但是做题的时候还是感觉很难,看完题都还是蒙的,只有第一题比较容易,其他题目完全不会做,也没什么思路。最后还预习了指针进阶的部分内容,预习内容是在网上查找的。
结对感悟:由于这周作业对我两而言太难得,所以这周的编程没交流什么名堂,只讨论了第一个编程题,剩下的我们都是各自写自己的看法,在寝室里面也有跟室友讨论题目,请教她们,虽然只做出了一个题目,但是还是很开心的。
周/日期 | 这周所花的时间 | 代码行 |
---|---|---|
3/9-3/15 | 30mim | 35 |
3/15-3/22 | 40min | 46 |
3/22-3/29 | 1h | 69 |
3/29-4/5 | 50min | 87 |
4/5-4/12 | 30min | 97 |
4/12-4/19 | 20min | 75 |
4/19-4/26 | 1h20min | 116 |
5/5-5/9 | 3h | 79 |