N***m 发帖数: 4460 | 1 题目:求1+2+…+n,
要求不能使用乘除法、for、while、if、else、switch、case等关键字以及
条件判断语句(A?B:C)。 |
g**e 发帖数: 6127 | 2 递归
【在 N***m 的大作中提到】 : 题目:求1+2+…+n, : 要求不能使用乘除法、for、while、if、else、switch、case等关键字以及 : 条件判断语句(A?B:C)。
|
N***m 发帖数: 4460 | 3 哈哈,强大!
【在 g**e 的大作中提到】 : 递归
|
i**********e 发帖数: 1145 | 4 这种递归就叫 tail recursion (因为recursive call 在最后面),可以很容易转换为
iterative.
int recursiveSum(int n) {
if (n == 0)
return 0;
return n+recursiveSum(n-1);
}
一些常见面试题的答案与总结 -
http://www.ihas1337code.com |
N***m 发帖数: 4460 | 5 网站不错
为
【在 i**********e 的大作中提到】 : 这种递归就叫 tail recursion (因为recursive call 在最后面),可以很容易转换为 : iterative. : int recursiveSum(int n) { : if (n == 0) : return 0; : return n+recursiveSum(n-1); : } : 一些常见面试题的答案与总结 - : http://www.ihas1337code.com
|
z****e 发帖数: 2024 | 6 没一个说到点上的,连题目都梅毒懂.
这种题估计各位师傅们都懒得出手了,弟子我就先代劳了.
template
class Sigma{
public:
enum { val= Sigma::val+N };
};
template< >
class Sigma<1>{
public:
enum { val=1 };
};
int main(int argc, char* argv[]){
cout<::val<
} |
g**e 发帖数: 6127 | 7 你这跟递归有啥分别?
【在 z****e 的大作中提到】 : 没一个说到点上的,连题目都梅毒懂. : 这种题估计各位师傅们都懒得出手了,弟子我就先代劳了. : template : class Sigma{ : public: : enum { val= Sigma::val+N }; : }; : template< > : class Sigma<1>{ : public:
|
i**********e 发帖数: 1145 | 8 谢啦!
网站主要有像你这样的读者在面试时分享的题目而来的。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
【在 N***m 的大作中提到】 : 网站不错 : : 为
|
z****e 发帖数: 2024 | 9 区别大了去了.
根本不用stack.
递归是用stack来回溯函数地址的.
而且题目明确要求不用if
如果只是考量数学上所谓递归的算法,这题完全没必要这么出.
答不上来不要嘴硬,不懂装懂更扣分!哈哈.
【在 g**e 的大作中提到】 : 你这跟递归有啥分别?
|
X****r 发帖数: 3557 | 10 士别三日啊
【在 z****e 的大作中提到】 : 没一个说到点上的,连题目都梅毒懂. : 这种题估计各位师傅们都懒得出手了,弟子我就先代劳了. : template : class Sigma{ : public: : enum { val= Sigma::val+N }; : }; : template< > : class Sigma<1>{ : public:
|
|
|
z****e 发帖数: 2024 | 11 与师傅们一日不见,如隔三秋.
俺特地从郓城县投奔各位押司哥哥们来了.
【在 X****r 的大作中提到】 : 士别三日啊
|
g**e 发帖数: 6127 | 12 我是疑问不是反问……不太懂c++,只是语法看起来像递归
确实没看清楚题,不能用if
谁知道用java怎么写
【在 z****e 的大作中提到】 : 区别大了去了. : 根本不用stack. : 递归是用stack来回溯函数地址的. : 而且题目明确要求不用if : 如果只是考量数学上所谓递归的算法,这题完全没必要这么出. : 答不上来不要嘴硬,不懂装懂更扣分!哈哈.
|
i**********e 发帖数: 1145 | 13 呵呵
不好意思 漏掉了if 不能用的条件
你的解法很好啊 也同样是 recursion,但是不同的是把 run time recursion 变成
compile time recursion.
既然是 compile time recursion,你的解法只能规定于 n 是 constant 的前提上使用.
如果 n 是 variable 的话,该怎么办呢?
一些常见面试题的答案与总结 -
http://www.ihas1337code.com
【在 z****e 的大作中提到】 : 没一个说到点上的,连题目都梅毒懂. : 这种题估计各位师傅们都懒得出手了,弟子我就先代劳了. : template : class Sigma{ : public: : enum { val= Sigma::val+N }; : }; : template< > : class Sigma<1>{ : public:
|
z****e 发帖数: 2024 | 14 目前俺也木有办法,你可以问问各位及时雨哥哥们.
用.
【在 i**********e 的大作中提到】 : 呵呵 : 不好意思 漏掉了if 不能用的条件 : 你的解法很好啊 也同样是 recursion,但是不同的是把 run time recursion 变成 : compile time recursion. : 既然是 compile time recursion,你的解法只能规定于 n 是 constant 的前提上使用. : 如果 n 是 variable 的话,该怎么办呢? : 一些常见面试题的答案与总结 - : http://www.ihas1337code.com
|
N***m 发帖数: 4460 | 15 这属于变相耍流氓阿,哈哈。
【在 z****e 的大作中提到】 : 没一个说到点上的,连题目都梅毒懂. : 这种题估计各位师傅们都懒得出手了,弟子我就先代劳了. : template : class Sigma{ : public: : enum { val= Sigma::val+N }; : }; : template< > : class Sigma<1>{ : public:
|
P*****f 发帖数: 2272 | 16 template metaprogramming
【在 N***m 的大作中提到】 : 这属于变相耍流氓阿,哈哈。
|
N***m 发帖数: 4460 | 17 how about this:
public class Main {
static int n = 100;
static int[] arr = new int[n];
static int sum = 0;
public static void main(String[] args) {
addToList(n);
System.out.println(sum);
}
public static void addToList(int m) {
try{
arr[m-1] = m;
sum += m;
addToList(m-1);
}catch(Exception e){
}finally {
}
}
}
just don't like metaprogramming.
【在 P*****f 的大作中提到】 : template metaprogramming
|
e****d 发帖数: 895 | 18 Or this,
int sum(int i)
{
int s;
__asm__
(
"movl %1, %%ecx\n\t"
"movl $0, %%eax\n\t"
"0:\n\t"
"addl %%ecx, %%eax\n\t"
"decl %%ecx\n\t"
"jnz 0b\n\t"
"movl %%eax, %0\n\t":
"=m"(s):
"m"(i):
"cc"
);
return s;
}
【在 N***m 的大作中提到】 : how about this: : public class Main { : static int n = 100; : static int[] arr = new int[n]; : static int sum = 0; : : public static void main(String[] args) { : addToList(n); : System.out.println(sum); : }
|
b********e 发帖数: 58 | 19 The following code (I know it's ugly) seems to do the job.
Tested with gcc under cygwin.
#include
#include
void add(int);
void finish(int);
void (*p[2])(int x);
int sum = 0;
int main(int argc, char **argv)
{
p[0] = add;
p[1] = finish;
doit(atoi(argv[1]));
return(0);
}
int doit(int n)
{
int next;
sum += n;
next = n-1;
(*p[next==0])(next);
}
void add(int x)
{
doit(x);
}
void finish(int x)
{
printf("sum = %d\n", sum);
exit(0);
} |
N***m 发帖数: 4460 | 20 hehe,nice.
【在 b********e 的大作中提到】 : The following code (I know it's ugly) seems to do the job. : Tested with gcc under cygwin. : #include : #include : void add(int); : void finish(int); : void (*p[2])(int x); : int sum = 0; : int main(int argc, char **argv) : {
|