問答1:
void Q1() {
char s[]="ABCDEFG";
char *t=&s[1];
*(t+4)=0;
cout << t << s;
}
ANS:
|
*t=&s[1]; |
指向字元B |
|
*(t+4)=0; |
字元B往後4個在F位置填入0表示字串結束 i=BCDE s=ABCDE |
問答2:
int q2(int x) {
if(x < 2) {
return 1;
}
else if(x % 2) { //奇數
return q2(x-1);
}
return q2(x/2) + 1; //偶數
}
|
q2(520) |
q2(260)+1 |
10 |
|
|
q2(260) |
q2(130)+1 |
9 |
|
|
q2(130) |
q2(65)+1 |
8 |
|
|
q2(65) |
q2(64) |
7 |
|
|
q2(64) |
q2(32)+1 |
7 |
|
|
q2(32) |
q2(16)+1 |
6 |
|
|
q2(16) |
q2(8)+1 |
5 |
|
|
q2(8) |
q2(4)+1 |
4 |
|
|
q2(4) |
q2(2)+1 |
3 |
|
|
q2(2) |
q2(1)+1 |
2 |
|
|
q2(1) |
1 |
1 |
問答3
int foo(int x, int *y) {
int f;
printf("%d %d",x,*y);
f=x*=*y;
printf("\nf=%d",f);
return f;
}
int Q3(int a, int b) {
int *c=&a,*d=&b;
*c = foo(b,c);
*d = a + b;
return a + b;
}