dangcongchuong
17-04-2009, 15:44
Mấy bạn xem giúp bài này nhé, mình ngẫm mãi mà không hiểu:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void myfun(int *p,int *&s);
void main()
{
clrscr();
int x=3;
int *p=&x;
int *s;
myfun(p,s);
cout<<setw(10)<<"x="<<x<<" *p="<<*p<<" *s= "<<*s;
getch();
return;
}
///////
void myfun(int *p,int *&s)
{
s=p;
*s=23;
int x=5;
s=&x;
return;
}
Đáp án là: x=23 *p=23 *s=5
mấy bạn cho mình hỏi: void myfun(int *p,int *&s), trong đó: int *&s là sao vậy nhỉ? mà nó lại cho kết quả thằng x=23, huu, hỏng hỉu ????????
ai giúp tớ thanks nhiều
[=========> Bổ sung bài viết <=========]
ah sẵn xem dùm mình bài này luôn:
không biết sai ở đâu mà nó không in ra ket quả, cũng không báo gì, mà bài này yêu cầu đặt ra co kết quả là: 0.25, mình không hiểu sao nó lại vậy nữa, mong giải thích giúp:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
float f(float x,int n);
void main()
{
clrscr();
float x=2,a;
int n=-2;
a=f(x,n);
cout<<setw(20)<<a;
getch();
return;
}
/////////////
float f(float x,int n)
{
float ret,xlast;
if(n<0)
ret=1/f(x,-n);
else
if(n==0)
ret=1;
else
if(n==1)
ret=x;
else
{
if(n%2==0)
{
xlast=f(x,n/2);
ret=xlast*xlast;
}
else
{
xlast=f(x,(n-1)/2);
ret=xlast*xlast*x;
}
return ret;
}
}
Đáp án: 0.25
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void myfun(int *p,int *&s);
void main()
{
clrscr();
int x=3;
int *p=&x;
int *s;
myfun(p,s);
cout<<setw(10)<<"x="<<x<<" *p="<<*p<<" *s= "<<*s;
getch();
return;
}
///////
void myfun(int *p,int *&s)
{
s=p;
*s=23;
int x=5;
s=&x;
return;
}
Đáp án là: x=23 *p=23 *s=5
mấy bạn cho mình hỏi: void myfun(int *p,int *&s), trong đó: int *&s là sao vậy nhỉ? mà nó lại cho kết quả thằng x=23, huu, hỏng hỉu ????????
ai giúp tớ thanks nhiều
[=========> Bổ sung bài viết <=========]
ah sẵn xem dùm mình bài này luôn:
không biết sai ở đâu mà nó không in ra ket quả, cũng không báo gì, mà bài này yêu cầu đặt ra co kết quả là: 0.25, mình không hiểu sao nó lại vậy nữa, mong giải thích giúp:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
float f(float x,int n);
void main()
{
clrscr();
float x=2,a;
int n=-2;
a=f(x,n);
cout<<setw(20)<<a;
getch();
return;
}
/////////////
float f(float x,int n)
{
float ret,xlast;
if(n<0)
ret=1/f(x,-n);
else
if(n==0)
ret=1;
else
if(n==1)
ret=x;
else
{
if(n%2==0)
{
xlast=f(x,n/2);
ret=xlast*xlast;
}
else
{
xlast=f(x,(n-1)/2);
ret=xlast*xlast*x;
}
return ret;
}
}
Đáp án: 0.25