2020年4月13日 by HXuesong
- 递归练习 - 查找数组下标范围中的最大值
#include <stdio.h>
int findMax(int arr[], int L, int R) {
if(L == R) {
return arr[L];
} else {
if(arr[R] > findMax(arr, L, R - 1)) {
return arr[R];
} else {
return findMax(arr, L, R - 1);
}
}
}
int main() {
int arr[8] = {3, 4, 6, 2, 12, 7, 9, 4};
int max = findMax(arr, 0, 7);
printf("maxNumber is %d", max);
return 0;
}
输出:
maxNumber is 12
- 递归练习 - 求数组下标范围中数值的和
#include <stdio.h>
int sum(int arr[], int L, int R) {
if(L == R) {
return arr[L];
} else {
return sum(arr, L, R - 1) + arr[R];
}
}
int main() {
int arr[8] = {1, 2, 3, 4, 5, 6, 7, 8};
int s = sum(arr, 0, 7);
printf("sum is %d", s);
return 0;
}
输出:
num is 36
- 递归练习 - 冒泡排序
#include <stdio.h>
void bubbleSort(int arr[], int L, int R) {
if(R > L) {
int i;
for(i = L; i <= R - 1; i++) {
if(arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
bubbleSort(arr, L, R - 1);
}
}
int main() {
int i, arr[8] = {3, 4, 6, 2, 12, 7, 9, 4};
bubbleSort(arr, 0, 7);
for(i = 0; i <= 7; i++) {
printf("%d ", arr[i]);
}
return 0;
}
输出:
2 3 4 4 6 7 9 12
- 递归练习:求最大公约数
#include <stdio.h>
int gcd(int a, int b) {
int r = a % b;
if(r == 0) {
return b;
} else {
return gcd(b, r);
}
}
int main() {
int a, b, g;
scanf("%d", &a);
scanf("%d", &b);
g = gcd(a, b);
printf("gcd is %d", g);
return 0;
}
输入:
4
128
输出:
4
-
场景1:好人鉴定👌 OK. If you want to adopt an animal, first we need to know some references. 好,如果你想领养动物,首先,我们需要一些旁证人。 References? People we know? 旁证人?是指了解我的人吗? Friends, teachers... We need to talk to some people about you. We want to be sure that you're responsible and that you can take good care of animal. Then you have to fill out this form about your family background. 朋友,老师......我们得向一些人打听你。 我们得确认你是肯负责的。而且能够好好照顾动物。 然后你得填写这张关于你家庭背景的表格。 Is that it? 就这些啦? No, there's more. 不,还有别的。
-
场景2:经验鉴定🐶 We need to know about your history with animals. Have you ever owned an animal? 我们要了解你以前和动物关系的情况。你曾经养过动物没? Yes. We had a cat when I was eight years old. I love cats. 有过。我8岁时我们家养过一只猫,我喜欢猫🐱。 Do you have any animals now? 你现在还养着什么动物吗? Unfortunately, no. 很遗憾,没有。 Anything else? 还有别的事吗? We also like to know your reasons for wanting an animal. 我们还想知道你想要领养动物的原因。
-
场景3:心态鉴定💝 Just to hold it and cuddle with it. Just to have as a pet. I love animals. 只是想抱着她,搂着她。只是想有个宠物。我最喜欢动物了。 To have a friend, a pal. You know, man's best friend is his dog. 我想有个朋友,一个伙伴。你知道的,人类最好的朋友就是狗🐕。 And one thing more. If you're under twenty-one years of age... 还有一件事,如果你还不到21岁...... That's me. 我是还不到21岁。 Then an adult must sign for you. 那就必须有一位成年人来替你签字。 Uh-oh. 啊,哦。 No problem. My parents will think it's a good idea. I'll be back with them. 没问题。我父母会觉得这是件好事,我会和他们一起来。
-
场景4:结果📞 We have good news and bad news, Robbie. 我给你带来了好消息和坏消息,Robbie。 Oh? 噢? The good news is that the Levinsons have come by to pick up the dog. The bad news is, you won't be able to adopt the dog. 好消息是Levinsons夫妇已来这儿把狗领回去了。 坏消息是你不能领养她了。 That's OK. 那没关系。 Come by one day and look at some of the other dogs. I'm sure there's one for you. Thanks, Robbie. And the Levinsons thank you for bringing their dog to us. 哪天有空来看看别的狗。我相信会有你喜欢的。谢谢你,Robbie。 Levinsons夫妇也让我谢谢你把狗送来。