您的位置:首页 > 其它

翻转

2015-07-10 20:32 204 查看

1.翻转整型数字(翻转前为n,翻转后为t)

while(n)
{
t=t*10+n%10;
n/=10;
}


2.[b]翻转数组[/b]

void rever_char(char c[],int n)
{
char temp;
int i,j = n-1,m = (n-1)/2;
for(i = 0; i <= m; i++)
{
j = n-1-i;
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}

3,指针[b]翻转数组[/b]

void reversal(int *a,int len)
{
if (len<2)
return;
//用指针实现
int *left=a,*right=a+len-1,temp;
do
{
temp = *left;
*left = *right;
*right = temp;
}while (++left < --right);
/*while (left++ <= right--)//指针的第二种写法
{
temp = *left;
*left = *right;
*right = temp;
}
//用数组实现
int end=len/2,tmp,j;
for (int i=0;i<end;++i)
{
tmp = a[i];
j = len -1 - i;
a[i] = a[j];
a[j] = tmp;
}*/
}




转载请注明出处:/article/7660356.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: