当前位置:网站首页>One dimensional array exercise
One dimensional array exercise
2022-07-29 05:27:00 【Tortilla 85】
One dimensional array exercises
One 、 exercises
1. Before the n Put the number behind
Ideas :
The array length is len
First, reverse the entire array
Then on the front len - n The number is in reverse order , This will be behind n To len Put the number to the front
To the back again n Elements are in reverse order , In this way, the former n The number is put behind
The time complexity is O(n)
Code :
void Reverse(int* arr, int begin, int end)// Yes begin To end Data in reverse order
{
assert(arr != NULL && begin <= end);
int temp = 0;
int len = (end - begin + 1) / 2;
for(int i = 0;i<len;i++,begin++,end--)
{
temp = arr[begin];
arr[begin] = arr[end];
arr[end] = temp;
}
}
void Adjust(int* arr, int len, int m)
{
assert(arr != NULL);
Reverse(arr, 0, len-1);// Yes arr The whole is in reverse order
Reverse(arr, 0, len - 1 - m);// To the front len-m Elements in reverse order
Reverse(arr, len - m, len - 1);// Right back m Elements in reverse order
}
test :
int main()
{
int arr[] = {
1,2,3,4,5,6,7 };
int len = sizeof(arr) / sizeof(arr[0]);
Adjust(arr, len, 4);
for (int i = 0; i < len; i++)
{
printf("%-2d", arr[i]);
}
return 0;
}
Running results :
2. Replace string characters
Code :
int GetCharCount(char *ch,char c) // Count the number of characters to be replaced
{
int count = 0;int i = 0;
assert(ch!=NULL);
while (ch[i] != '\0')
{
if (ch[i] == c)count++;
i++;
}
return count;
}
void Rplace(char* ch, char aim, int num, char newc) // Replace character
{
assert(ch!=NULL);
int count = GetCharCount(ch,aim);
int i = strlen(ch), j = i + count * (num - 1);
while (i != j)
{
if (ch[i] != aim)
{
ch[j--] = ch[i--];
}
else
{
for (int a = 0; a < num; a++)
{
ch[j--] = newc;
}
i--;
}
}
}
test :
int main()
{
char ch[100] = "I am a student";
Rplace(ch, ' ', 4, '!');
for (int i = 0; i < strlen(ch); i++)
{
printf("%c",ch[i]);
}
return 0;
}
Running results :
3. Realization strcpy()
Code :
void my_strcpy(char* arr,char* brr)
{
assert(arr != NULL && brr != NULL);
int size = strlen(brr)+1,i=0;
while(i<=size)
{
arr[i] = brr[i];
i++;
}
}
test :
int main()
{
char ch1[100] = "hi hello";
char ch2[] = "world";
my_strcpy(ch1, ch2);
for (int i = 0; i < strlen(ch1); i++)
{
printf("%c", ch1[i]);
}
return 0;
}
Running results :
4. Realization strcat()
Code :
void my_strcat(char* arr, char* brr)
{
assert(arr!=NULL && brr!=NULL);
int i = strlen(arr), j = 0;
while (arr[j] != '\0')
{
arr[i] = brr[j];
i++;
j++;
}
}
test :
int main()
{
char ch1[100] = "hello ";
char ch2[] = "world";
my_strcat(ch1, ch2);
for (int i = 0; i < strlen(ch1); i++)
{
printf("%c", ch1[i]);
}
return 0;
}
Running results :
Two 、 Assertion assert() Use
Add header file when using :#include<assert.h>
Using the logical :
assert( Judge the condition );
Can be seen as :
if( Judge the condition )
{
Condition if true , Continue to execute the statement ;
}
else Jump out of
If the result of the judgment condition is 1, Continue operation
If 0, Then the program crashes
Code :
void test(int a,int b)
{
assert(a==b);
printf(" equal \n\n");
}
When a 、b Wait a moment :
int main()
{
int a = 1, b = 2 ;
test(a,b);
return 0;
}
Running results :
Because the result of the judgment condition is 0, The program crash

When a 、b When equal :
int main()
{
int a = 1, b = 1 ;
test(a,b);
return 0;
}
Running results :
边栏推荐
- QML custom tabbar
- How to get command parameters in Visual Basic.Net
- Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
- Live broadcast Preview: integration of JD cloud Devops and jfrog product library
- 预约中,2022京东云产业融合新品发布会线上开启
- vim编辑器使用
- QtCreator+CMake编译器设置
- JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
- Cryengine3 debugging shader method
- 研发效能生态完整图谱&DevOps工具选型必看
猜你喜欢
随机推荐
C language one-dimensional array
365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
数据泄漏、删除事件频发,企业应如何构建安全防线?
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
C语言数组入门到精通(数组精讲)
osgSimplegl3例子分析
一维数组练习
vim编辑器使用
千人规模互联网公司研发效能成功之路
如视技术副总裁杨永林:当传统产业遇到“数字空间”
CryEngine技术
167. Sum of two numbers II - enter an ordered array
WDDM learning
【C语言系列】—深度解剖数据在内存中的存储(一) 暑假开篇
英伟达周锡健:设计到数字营销的最后一公里
321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value
CSDN的md编辑器如何输入上下标?公式和非公式的输入方式不一样
APP常用跨端技术栈深入分析
The latest tank battle 2022 - Notes on the whole development -2









