c语言,函数编程

c语言,函数编程
最后一个G(x)=x x为奇数,是x为奇数还是x大于等于2且x为奇数,如果是>2且为奇数,则代码如下:
int F(int x)
{
if(x < 2)
return x;
else
{
if(x % 2 == 0)
return G(x / 2) * 2;
else
return G((x - 1) / 2);
}
}

int G(int x)
{
if(x < 2)
return x;
else
{
if(x % 2 == 0)
return G(x / 2) + 1;
else
return x;
}
}
#include

int F(int x);
int G(int x);

int F(int x){
if(x<2)
return x;
else if(x>=2&&x%2==0)
return G(x/2)*2;
else
return G((x/2)/2);
}

int G(int x){
if(x<2)
return x;
else if(x>=2&&x%2==0)
return G(x/2)+1;
else
return x;
}

void main(){
int x;
printf("测试F(x)函数和G(X)函数,请输入一个整数:\n");
scanf("%d",&x);
printf("F(%d) = %d\n",x,F(x));
printf("G(%d) = %d\n",x,G(x));
}

你要的答案只写F(X)和G(X),中间的那段程序就可以了。
if判断
← 上一篇: 下一篇: →