螺旋队列,两值交换,字符串拷贝等练习
// Primer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//取得|x|,|y|中的最大值为c,得出所在正方形边的长度为2c+1,正方形右上角顶点值为2c+1的平方(p),
//它的坐标为(m,n)(负坐标),次级正方形的右上角顶点值为2c-1的平方(q),它的坐标为(j,k)
//(负坐标),y-n值的绝对值为d,x-m值的绝对值为b,判断d和b的值
//获取最大值
inline int max(int a,int b)
{
return a < b ? b : a;
}
//另外一种方式获取最大值
inline int getMax(int a,int b)
{
return (a + b + abs(a - b)) / 2;
}
//获取绝对值
inline int abs(int a)
{
return a > 0 ? a : -a;
}
//临时交换
inline void tmpSwap(int *a , int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//加法交换
inline void addSwap(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
//异或交换
inline void xorSwap(int *a, int *b)
{
*a = (*a)^(*b);
*b = (*a)^(*b);
*a = (*a)^(*b);
}
//获取螺旋队列中的某个值
int foo(int x,int y)
{
int t = max(abs(x),abs(y));
int u = 2*t;
int v = 2*t -1;
v = v*v + u;
if(x == -t)
{
v += u + t - y;
}
else if(y == -t)
{
v += 3*u + x - t;
}
else if(y == t)
{
v += t - x;
}
else //at this point x == t
{
v += y - t;
}
return v;
}
//返回指针便于链式操作
char *strcpy_1(char *dest , const char *src)
{
//入口指针参数断言检测
assert((dest!=NULL) && (src!=NULL));
//保留原始地址
char *address = dest;
//赋值
while((*dest++ = *src++)!='\0');
//字符串末尾加0
*dest = '\0';
//返回该地址
return address;
}
//求取字符串长度
int strlen_1(const char *src)
{
//断言
assert(src!=NULL);
int len = 0;
while( (*src++) !='\0')
{
++len;
}
return len;
}
//测试函数
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
char str[10];
char* str10 = "0123456789";
int t = strlen_1(strcpy_1(str,"abc"));
int m = 10;
int n = 20;
printf("before xor swap , m = %d , n = %d\n" , m , n);
xorSwap(&m,&n);
printf("afeter xor swap , m = %d , n = %d\n" , m , n);
m = 10;
n = 20;
printf("before add swap , m = %d , n = %d\n" , m , n);
addSwap(&m,&n);
printf("afeter add swap , m = %d , n = %d\n" , m , n);
m = 10;
n = 20;
printf("before temp swap , m = %d , n = %d\n" , m , n);
tmpSwap(&m,&n);
printf("afeter temp swap , m = %d , n = %d\n" , m , n);
for(y = -4 ; y<= 4; y++)
{
for(x = -4 ; x <= 4; x++)
{
printf("%5d",foo(x,y));
}
printf("\n");
}
while(int c = (scanf_s("%d %d",&x,&y) == 2))
{
if(!c)
{
break;
}
printf("%d\n",foo(x,y));
}
return 0;
}
//
#include "stdafx.h"
//取得|x|,|y|中的最大值为c,得出所在正方形边的长度为2c+1,正方形右上角顶点值为2c+1的平方(p),
//它的坐标为(m,n)(负坐标),次级正方形的右上角顶点值为2c-1的平方(q),它的坐标为(j,k)
//(负坐标),y-n值的绝对值为d,x-m值的绝对值为b,判断d和b的值
//获取最大值
inline int max(int a,int b)
{
return a < b ? b : a;
}
//另外一种方式获取最大值
inline int getMax(int a,int b)
{
return (a + b + abs(a - b)) / 2;
}
//获取绝对值
inline int abs(int a)
{
return a > 0 ? a : -a;
}
//临时交换
inline void tmpSwap(int *a , int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//加法交换
inline void addSwap(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
//异或交换
inline void xorSwap(int *a, int *b)
{
*a = (*a)^(*b);
*b = (*a)^(*b);
*a = (*a)^(*b);
}
//获取螺旋队列中的某个值
int foo(int x,int y)
{
int t = max(abs(x),abs(y));
int u = 2*t;
int v = 2*t -1;
v = v*v + u;
if(x == -t)
{
v += u + t - y;
}
else if(y == -t)
{
v += 3*u + x - t;
}
else if(y == t)
{
v += t - x;
}
else //at this point x == t
{
v += y - t;
}
return v;
}
//返回指针便于链式操作
char *strcpy_1(char *dest , const char *src)
{
//入口指针参数断言检测
assert((dest!=NULL) && (src!=NULL));
//保留原始地址
char *address = dest;
//赋值
while((*dest++ = *src++)!='\0');
//字符串末尾加0
*dest = '\0';
//返回该地址
return address;
}
//求取字符串长度
int strlen_1(const char *src)
{
//断言
assert(src!=NULL);
int len = 0;
while( (*src++) !='\0')
{
++len;
}
return len;
}
//测试函数
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
char str[10];
char* str10 = "0123456789";
int t = strlen_1(strcpy_1(str,"abc"));
int m = 10;
int n = 20;
printf("before xor swap , m = %d , n = %d\n" , m , n);
xorSwap(&m,&n);
printf("afeter xor swap , m = %d , n = %d\n" , m , n);
m = 10;
n = 20;
printf("before add swap , m = %d , n = %d\n" , m , n);
addSwap(&m,&n);
printf("afeter add swap , m = %d , n = %d\n" , m , n);
m = 10;
n = 20;
printf("before temp swap , m = %d , n = %d\n" , m , n);
tmpSwap(&m,&n);
printf("afeter temp swap , m = %d , n = %d\n" , m , n);
for(y = -4 ; y<= 4; y++)
{
for(x = -4 ; x <= 4; x++)
{
printf("%5d",foo(x,y));
}
printf("\n");
}
while(int c = (scanf_s("%d %d",&x,&y) == 2))
{
if(!c)
{
break;
}
printf("%d\n",foo(x,y));
}
return 0;
}