#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<math.h>
void reverse(char* str) { assert(str);//断言 int len = strlen(str);//这里是字符串
char* left = str;
char* right = str + len - 1;
while (left<right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void print(int arr[], int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("/n");
}
void move(int arr[], int sz)
{
int left = 0; int right = sz - 1;
while (left < right)
{
while ((left < right) && (arr[left] % 2 == 1))
{
left++;
}
while ((left < right) && (arr[right] % 2 == 0))
{
right--;
}
if (left < right)
{
int tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
}
}
//1.暴力求解法 void left_move(char*arr, int k) { assert(arr);//断言避免是一个空指针 int i = 0; int len = strlen(arr); for (i = 0; i < k; i++) { char tmp = *arr; int j = 0; for (j = 0; j < len - 1; j++) { *(arr + j) = *(arr + j + 1); } *(arr + len - 1) = tmp; } }
void left_move1(char*arr, int k) { assert(arr);//断言避免是一个空指针 int len = strlen(arr); assert(k <= len); reverse(arr, arr + k - 1);//逆序左边 reverse(arr+k, arr + len - 1);//逆序右边 reverse(arr, arr + len - 1);//逆序整体
}
int is_left_move(char* s1, char* s2)
{
int len = strlen(s1); int i = 0; for (i = 0; i < len; i++) { left_move1(s1, 1); int ret = strcmp(s1, s2); if (ret == 0) return 1; } return 0; }
int is_left_move1(char* s1, char* s2) { int len1 = strlen(s1);
int len2 = strlen(s2);
if (len1 != len2) return 0; //1.在str1字符串后面追加一个str1字符串 //2.判断str2指向的字符串是否是str1指向字符串的字串 //strcat自己给自己追加容易死循环,/0被修改 strncat(s1, s1, 6);//要追加几个就是几个 char* ret = strstr(s1, s2); if (ret == NULL) { return 0; } else { return 1; } }
int Findnum(int arr[3][3], int k, int row, int col) { int x = 0; int y = col - 1; while (x <= row - 1 && y >= 0) { if (arr[x][y] > k) { y--; } else if (arr[x][y] < k) { x++; } else { return 1; } } return 0; }
int main() { //unsigned long pularrptr[] = {6,7,8,9,10}; //unsigned long *pulptr;
//pulptr = pularrptr;
//*(pulptr + 3) += 3;
//printf("%d,%d\n", *pulptr, *(pulptr + 3));
//char arr[256] = {0};
////scanf("%s", arr);//有空格就停止
//gets(arr);//读取一行,空格也不影响
//reverse(arr);
//printf("逆序后的字符串%s\n", arr);
//int a = 0;
//int n = 0;
//scanf("%d%d", &a, &n);
//int sum = 0;
//int i = 0;
//int ret = 0;
//for (i = 0; i < n; i++)
//{
// ret = ret * 10 + 2;
// sum += ret;
//}
//printf("%d\n", sum);
//int i = 0;
//for (i = 0; i <= 100000;i++)
//{
// //判断是否为水仙花数
// //1:计算i的位数n
// int n = 1;
// int tmp = i;
// int sum = 0;
// while (tmp /= 10)
// {
// n++;
// }
// //2:计算i的每一位的n次方之和
// tmp = i;
// while (tmp)
// {
// sum += pow(tmp % 10, n);
// tmp /= 10;//tmp最后取到0
// }
// //3:比较i==sum
// if (i == sum)
// {
// printf("%d ", i);
// }
//}
//int line = 0;
//scanf("%d", &line);
////打印上半部分
//int i = 0;
//for (i = 0; i < line; i++)
//{
// int j = 0;
// for (j = 0; j < line - 1 - i; j++)
// {
// printf(" ");
// }
// for (j = 0; j < 2 * i + 1; j++)
// {
// printf("*");
// }
// printf("\n");
//}
////打印下半部分
//for (i = 0; i < line; i++)
//{
// int j = 0;
// for (j = 0; j <=i; j++)
// {
// printf(" ");
// }
// for (j = 0; j < 2 *(line-1-i)-1; j++)
// {
// printf("*");
// }
// printf("\n");
//}
//int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
//int sz = sizeof(arr) / sizeof(arr[0]);
//move(arr, sz);
//print(arr,sz);
//char arr[] = "abcdef";
//left_move(arr, 2);
//printf("%s\n", arr);
//char arr1[] = "abcdef";
//char arr2[] = "cdefab";
//int ret = is_left_move(arr1, arr2);
//if (ret == 1)
// printf("yes\n");
//else
// printf("No\n");
//char arr1[30] = "abcdef"; //char arr2[] = "cdefab"; //int ret = is_left_move1(arr1, arr2); //if (ret == 1) //{ // printf("yes\n"); //} //else //{ // printf("No\n"); //}
//杨氏矩阵求解 //int arr[3][3] = { {1,2,3},{4,5,6},{7,8,9} }; //int k = 7; //int ret = Findnum(arr, k, 3, 3); //if (ret == 1) //{ // printf("找到了\n"); //} //else //{ // printf("找不到\n"); //}
return 0;
}
标签:arr,right,int,作业,ret,char,指针,left From: https://blog.51cto.com/u_15891124/6156888