1、
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
scanf("%s", str);
int length = strlen(str);
bool isHuiwen = true;
for(int i = 0;i < length/2;i++)
{
if(str[i] != str[length-i-1])
{
isHuiwen = false;
break;
}
}
if(isHuiwen)
printf("yes");
else
printf("no");
return 0;
}
2、
#include <stdio.h>
#include <string.h>
int main()
{
char src[100];
char dest[100];
scanf("%s", src);
int length = strlen(src);
int src_i = length-1, dest_i = 0;
while(src_i >= 0)
{
dest[dest_i++] = src[src_i--];
}
dest[dest_i] = '\0';
printf("%s", dest);
return 0;
}
3、
#include <stdio.h>
int main()
{
int num[3][3] = {1,2,3,
4,5,6,
7,8,9};
int newNum[3][3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
newNum[i][j] = num[j][i];
}
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d ", newNum[i][j]);
}
printf("\n");
}
printf("\n\n");
for(int i = 0; i < 3; i++)
{
for(int j = i+1; j < 3; j++)
{
int temp = num[i][j];
num[i][j] = num[j][i];
num[j][i] = temp;
}
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d ", num[i][j]);
}
printf("\n");
}
return 0;
}
4、
(1)
#include <stdio.h>
#include <string.h>
void huiwen(char *str)
{
int length = strlen(str);
bool isHuiwen = true;
for(int i = 0;i < length/2;i++)
{
if(str[i] != str[length-i-1])
{
isHuiwen = false;
break;
}
}
if(isHuiwen)
printf("yes");
else
printf("no");
}
int main()
{
char string[100];
scanf("%s", string);
huiwen(string);
return 0;
}
(2)
#include <stdio.h>
#include <string.h>
void output(char *src, char *dest)
{
int length = strlen(src);
int src_i = length-1, dest_i = 0;
while(src_i >= 0)
{
dest[dest_i++] = src[src_i--];
}
dest[dest_i] = '\0';
}
int main()
{
char str1[100];
char str2[100];
scanf("%s", str1);
output(str1, str2);
printf("%s", str2);
return 0;
}
(3)
void array(int **num, int m)
{
for(int i = 0; i < m; i++)
{
for(int j = i+1; j < m; j++) //j的起始是i+1,因为只需换矩阵的右上部分,右上和左下对调
{
//这里是变动比较大的地方。由于数组作为参数传进来,变成了指针,在这个函数内部,他是不知道是个m*m的数组
//由于数组在内存中的存储是连续的num[0][0]可以近似的认为是num[0],而num[1][0]可以近似的认为是num[1*3+0]
//接下来涉及的就是一些指针的强制转换了。这里比较复杂,可以查阅下相关资料,也可以问我
int temp = *((int*)num+i*m+j);
*((int*)num+i*m+j) = *((int*)num+j*m+i);
*((int*)num+j*m+i) = temp;
}
}
}
int main()
{
int num[3][3] = {1,2,3,
4,5,6,
7,8,9};
array((int**)num, 3);//注意num的使用方式 作为void array(int **num, int m,int n)的参数
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d ", num[i][j]);
}
printf("\n");//常用的二维数组输出方式
}
return 0;
}
5、
#include <stdio.h>
void BubbleSort(int *num, int len)
{
for(int i = 1;i < len;i++)
{
for(int j = len-1;j >= i;j--)
{
if(num[j-1] > num[j])
{
int temp = num[j-1];
num[j-1] = num[j];
num[j] = temp;
}
}
}
}
int main()
{
int num[5] = {1,4,3,2,5};
BubbleSort(num,5);
for(int i = 0;i<5;i++)
{
printf("%d ", num[i]);
}
return 0;
}
6、
#include <stdio.h>
typedef bool (*COMP)(int,int);
bool compara1(int a, int b)
{
return a < b;
}
bool compara2(int a, int b)
{
return a > b;
}
void fun(int num1, int num2, bool (*comp)(int,int))
{
if(comp(num1, num2))
{
printf("AAAAA\n");
}
else
{
printf("BBBBB\n");
}
}
void fun2(int num1, int num2, COMP comp2)
{
if(comp2(num1, num2))
{
printf("CCCCC\n");
}
else
{
printf("DDDDD\n");
}
}
int main()
{
fun(4,5,compara1);
fun(4,5,compara2);
fun2(4,5,compara1);
fun2(4,5,compara2);
return 0;
}
7、
#include <stdio.h>
typedef bool (*COMP)(int,int);
bool compara1(int a, int b)
{
return a < b;
}
bool compara2(int a, int b)
{
return a > b;
}
void QuickSort(int *num, int left, int right, bool (*comp)(int,int))
{
if(left < right)
{
int i = left, j = right;
int key = num[i];
while(i < j)
{
while(i<j && comp(key, num[j]))
j--;
if(i<j)
num[i++] = num[j];
while(i<j && comp(num[i], key))
i++;
if(i<j)
num[j--] = num[i];
}
num[i] = key;
QuickSort(num, left, i-1, comp);
QuickSort(num, i+1, right, comp);
}
}
int main()
{
int num[5] = {1,4,3,2,5};
QuickSort(num, 0, 4, compara1);
for(int i = 0;i<5;i++)
{
printf("%d ", num[i]);
}
return 0;
}
8、
#include <stdio.h>
#include <string.h>
void output(char *src, char *dest)
{
int length = strlen(src);
int src_i = length-1, dest_i = 0;
while(src_i >= 0)
{
dest[dest_i++] = src[src_i--];
}
dest[dest_i] = '\0';
}
bool output2file(char *filenameIn, char *filenameOut)
{
FILE *fpIn, *fpOut;
if ( (fpIn=fopen(filenameIn,"r+")) == NULL )
{
printf( "open %s error\n", filenameIn );
return false;
}
if ( (fpOut=fopen(filenameOut,"w+")) == NULL )
{
printf( "open %s error\n", filenameOut );
return false;
}
char bufSrc[512],bufDest[512];
fscanf(fpIn, "%s", bufSrc);
output(bufSrc, bufDest);
fprintf(fpOut, "%s", bufDest);
fclose(fpIn);
fclose(fpOut);
return true;
}
int main()
{
char *filein = "D:\\filein.txt";
char *fileout = "D:\\fileout.txt";
output2file(filein, fileout);
return 0;
}