我试图用 C 语言理解这种程序,但我不能。确切地说,我无法弄清楚 *s 是如何更改的,以及为什么编译器显示 210012。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void WhatIamDoing(char *s) {
char ch;
if (*s) {
ch = *s;
s++;
WhatIamDoing(s);
putchar(ch);
}
}
int main() {
char s[20] = "012" ;
WhatIamDoing(s) ;
printf( "%s", s ) ;
}
Run Code Online (Sandbox Code Playgroud) 我需要在C中编写一个函数,给定一个指向链表的指针,将打印出Python语法中的元素:例如,对于包含1,2,3,4和5的列表,该函数将打印出来[ 1,2,3,4,5].
我试着编写如下代码:
struct node {
struct node *next;
int data;
};
void print_list(struct node *list) {
printf("[");
if (list == NULL) {
printf("]");
} else {
printf("%d", list->data);
if (list->next != NULL) {
printf(", ");
}
print_list(list->next);
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下:[1,[2,[3,[4,[5 []
据我所知,每次该函数调用自身时,都会打印"[".有没有办法在第一次调用函数时打印"["?
#include <stdio.h>
int main() {
static int i = 5;
if (--i) {
main();
printf("%d ", i);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我想知道printf这个程序是如何执行的.
INPUT :( A(B(D(E)(F)))(C)(K))我目前有两个函数,它给我一个OUTPUT:
一个
乙
C
ķ
乙
d
d
Ë
F
Ë
零
但是我需要这样的输出:
a:bck
b:d
c:
k:
d:ef
E:
F:
要么
一个
BSK
d
EF
(defun print-children (s)
(cond ((null (caar (cdr s))) nil)
(t (print (caar (cdr s))) (print-children (cdr s)))))
(defun print-tree (s)
(cond ((null s) nil)
((atom (car s)) (print (car s)) (print-children s) (print-tree (cdr s)))
(t (print-tree (car s)))))
Run Code Online (Sandbox Code Playgroud) 我有一个递归的Haskell函数,它接受一个数字n并生成一个列表,该列表是从1开始的n长平方数.
代码可以工作但不是n = 3的列表是[9,4,1]我希望它是[1,4,9].
sqNList :: Int -> [Int]
sqNList 0 = []
sqNList n = n*n : sqNList (n-1)
Run Code Online (Sandbox Code Playgroud)
我试过swappig:for a ++但是模式匹配不起作用.我今天刚刚开始使用Haskell所以可能很明显
我试图让我的头围绕递归.我可以做一些我一直在做的练习,但是我不明白当你把它传给我时,这个输出是如何打印出来的七次,有人可以向我解释.
public static void quiz(int i) {
if (i > 1) {
quiz(i / 2);
quiz(i / 2);
}
System.out.print("*");
}
Run Code Online (Sandbox Code Playgroud) 如果递归函数在单独的句子中没有return语句,则返回什么.
#include <stdio.h>
int rec(int i)
{
if (i != 3)
return rec(++i);
}
int main()
{
rec(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 问:有些人喜欢用单词"LOL"垃圾邮件互联网聊天室.这通常会使一些其他用户感到恼火,他们将通过宣布"再多一次LOL和我出局"作出回应.还有其他用户会通过发布"再多一个LOL而我已经出局'并且我已经出局"来模仿这个回应,等等,嵌套越来越多的层次"再多一个{X}我就出局了".完成递归lol()函数,该函数采用表示嵌套级别的正(非零)整数参数
("另外一个LOL,我出去了"代表一个级别的嵌套).该函数返回一个字符串,其中包含上面字符串的适当嵌套版本.例如,lol(3)会返回字符串(再多一个LOL,我出去了,我出去了,我出去了).
我的解决方案
def lol(y):
middle = "LOL"
part1 = " One more "
part2 = " and I'm out"
templist = []
answer = ""
if y == 0:
print ("LOL")
else:
for i in range(y):
middle = middle + part2
for j in range(len(middle)):
templist.append(middle[j])
templist.reverse()
for k in range (y):
templist.append(part1)
templist.reverse()
for h in range(len(templist)):
answer = answer + templist[h]
print (answer)
Run Code Online (Sandbox Code Playgroud)
我想看一个使用递归的解决方案.然后我很想看到最有效的解决方案.谢谢!
我的程序非常简单我正在制作一个递归程序我希望它一次又一次地调用它自己直到任务完成它编译得很好但它总是说"programme.exe停止工作"
#include <iostream>
using namespace std;
double factorial (double);
main ()
{
double n;
cin >> n;
cout << factorial (n);
}
double factorial (double n)
{
return (n * factorial (n - 1));
}
Run Code Online (Sandbox Code Playgroud) 我有一个自然语言处理解析树为
(S
(NP I)
(VP
(VP (V shot) (NP (Det an) (N elephant)))
(PP (P in) (NP (Det my) (N pajamas)))))
Run Code Online (Sandbox Code Playgroud)
并且我想将其存储在关联数组中,但是PHP中没有函数,因为NLP通常在python中完成。
因此,我应该解析开始和结束括号,以构建树结构的关联数组。我可以想到两种选择
我认为第一种方法是非标准的,并且正则表达式模式在复杂情况下可能会中断。
您能建议一个可靠的方法吗?
关联数组可以具有任何形式,因为操作起来并不困难(我需要循环使用),但是它可以像
Array (
[0] = > word => ROOT, tag => S, children => Array (
[0] word => I, tag = > NP, children => Array()
[1] word => ROOT, tag => VP, children => Array (
[0] => word => ROOT, tag => VP, children => Array ( .... )
[1] …Run Code Online (Sandbox Code Playgroud) recursion ×10
c ×4
function ×2
c++ ×1
common-lisp ×1
haskell ×1
java ×1
linked-list ×1
lisp ×1
php ×1
pointers ×1
preg-match ×1
printf ×1
printing ×1
python ×1
regex ×1
return ×1
tree ×1
xml-parsing ×1