Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: Given char arr[100]. what is strlen(arr) and what would printf("%s", arr) do?

Answer:
arr[100] has not been initialized to anything so a strlen could print out some random number because it could be pointing to some random memory right now.

Used to test your understanding of the difference between sizeof and strlen I suppose,
sizeof would be 100 bytes and as abc said - strlen would be random characters

[Quote]:http://www.careercup.com/question/?q=b0a37e61-15b1-482e-91fa-cc4d87f881c0

| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: Write a program to multiply 2 really long numbers

Answer:
I guess the trick is that the number could be over 16 byte
so first find out how many bytes we need, then multiply different parts seperately

その前に、
17 * 17 = 1 0010 0001
は二進数で
     0001 0001
×   0001 0001
--------------------
1 0010 0001
である。

最初の 0001 0001 を a と b と見なし、
あとの 0001 0001 を c と d と見なすと、

結果の0001 0010 0001はそれぞれ、a*c, a*d+b*c, b*d に対応する。

同じ例をもう一つ
1568 * 1568 = 0010 0101 1000 0000 0000
は二進数で
     0110 0010 0000
×   0110 0010 0000
--------------------------------------
0010 0101 1000 0000 0000
である。

最初の 0110 0010 0000 を a, b, c と見なし、
あとの 0110 0010 0000 を d, e, f と見なすと、

結果の0010 0010 0000 0000はそれぞれ、a*d,  a*e+b*d,  a*f+b*e+c*d, c*e+b*f, c*f に対応する。

以上の二つの例から導かれる法則を一般化すると、

int *a = (int *)malloc(sizeof(int)*10000);      // a,b, c ...を入れる
int *b = (int *)malloc(sizeof(int)*10000);      //d, e, f ...をいれる
long long *c = (long long*)calloc(10000, sizeof(long long));   //   8bytes整数型がないので llong で代用

long long AA = LLONG_MAX      //  限界値
long long BB = LLONG_MAX      // 限界値

int i=1, j=1;

// 例と違って8 ビットずつ a, b, c などとする
while ( AA/INT_MAX > 0 )      // 32 ビットシフトできる回数を数える
{
    i++;                               // i 個の32ビット塊がある
}

while ( BB/INT_MAX > 0 )      // 32 ビットシフトできる回数を数える
{
    j++;                               // j 個の32ビット塊がある
}

for (int m=i, n=0; m<0; m- -, n++)
    a[n] = AA>>(32)*m;      //   AA の先頭から a[ ] に代入する。

for (int m=i, n=0; m<0; m- -, n++)
    b[n] = BB>>(32)*m;      //   BB の先頭から b[ ] に代入する。

for (int m=0; m<i; m++)
    for (int n=0; n<j; n++)
       c[m+n] += a[m] * b[[n];

アルゴリズムの流れは上になります。まだ、compile してないのだが、考え方は上で大丈夫です。

あとは人の手で、c[0], c[1], ..... c[ ] を二進数としてつなぎ合わせてから10進数に変換すればよい。

ここでの、各最小ブロックは例のような4 bit ずつではなく、32 bit のなっている。

別解としては、10進数のまま行うこともできます。

例をもう一つ
123 * 123
は10進数で
     123
×   123
--------------------------------------
15129
である。

最初の 123 を a, b, c と見なし、
あとの 123 を d, e, f と見なすと、

結果の1, 5, 1, 2, 9はそれぞれ、a*d,  a*e+b*d,  a*f+b*e+c*d, c*e+b*f, c*f に対応する。
これは2進数と同じ法則性である。

なので、まず大きい数AA, BB をstring に変えて一文字ずつ
a[ ] と b[ ] に入れていく。

で同じようにc[ ] を計算すれば同じ結果が得られます。ただし、1文字につき1配列分なのでmemory 消費は2進数の解法よりも激しいです。

[Quote]:http://www.careercup.com/question/?q=8ee8f6ee-5c59-43b2-888f-eff52e552795
| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: How do you figure out the size of int without using sizeof()?

Answer:
int s[2];
cout<<(char *)&s[0] - (char *)&s[1]<or
int s;
cout<<(char *)(&s+1) - (char *)&s<
Comment:
The reason that the (char *) is needed is that &s[0] is of int * type
and thus, (int *) - (int *) just tells how many int address differences there are.

&s[0] - &s[1] would just be 1 in above case.
and (char *) is mostly 1 byte long.


[Quote]:http://www.careercup.com/question/?q=460a5046-b8ed-4871-91dd-13297d7eec7f

| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: What's the difference between malloc and calloc?

Answer:
calloc does '0' clear all the assigned N bytes memory.

												
												
| BLOG TOP |
DATE: CATEGORY:C/C++ puzzle
Q: What's static methtod in C?

Answer:
Say that you have a
File called _test_static and
function (static void foo) inside this file.
WITHOUT the word static at the beginning of the function declaration, the function becomes a global function. If any file #includes _test_static, It can not call the function. So basically the static keyword makes it visible only within _test_static file. Hope that helps.

Static function has scope visibility for the file in which it is defined.


[Quote]:http://www.careercup.com/question/?q=4f9e73b0-1335-4429-8a50-8563e9c41a8e

| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ