11-20

11、

 

 

12、

 

 

13、

 

 

 

14、

 代码:

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #pragma warning(disable:4700)
 4 #include <stdio.h>
 5 #include <string.h>
 6 
 7 int strCmp(char* str1, char* str2) {
 8     int d = 0;
 9     while (*str1 != '\0' && *str2 != '\0') {
10         d = *str1 - *str2;
11         if (d != 0)
12             break;
13         str1++;
14         str2++;
15     }
16     //再做一次减:因为存在特殊情况,如aaaa aaaabb,跳出循环时,其中一个已经结束,另一个还未结束
17     //此时str1指向'\0',str2指向b,因此需要再做一次减法
18     d = *str1 - *str2;
19 }
20 
21 int main() {
22     char c1[100] = { 0 };
23     char c2[100] = { 0 };
24     /*scanf("%s", c1);
25     scanf("%s", c2);*/
26     gets_s(c1,100);
27     gets_s(c2,100);
28     printf("%d\n", strCmp(c1, c2));
29     return 0;
30 }
View Code
复制代码

 结果:

 

 

 

 

15、

 代码:

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 void strpy(char* str1,char* str2) {
 7     while (*str2!='\0')
 8     {
 9         *str1 = *str2;
10         str1++;
11         str2++;
12     }
13     *str1 = '\0';
14 }
15 
16 int main() {
17     char str1[100];
18     char str2[100];
19     scanf("%s", str1);
20     scanf("%s", str2);
21     strpy(str1, str2);
22     printf("复制后:\nstr1:%s\tstr2:%s\n",str1,str2);
23     return 0;
24 }
View Code
复制代码

结果:

 

16、

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #include <stdio.h>
 4 #include <string.h>
 5 /// <summary>
 6 /// 本质:同时从前往后遍历非负数,从后往前遍历负数,定位之后两者做交换
 7 /// </summary>
 8 /// <param name="arr"></param>
 9 /// <param name="arrLen"></param>
10 void negBeforePos(int arr[], int arrLen) {
11     int low = 0;
12     int high = arrLen - 1;
13     while (low < high) {
14         //从后往前找小于0的数
15         while (arr[high]>=0 && high>low)
16         {
17             high--;
18         }
19         //从前往后找大于等于0的数
20         while (arr[low]<0 && high>low)
21         {
22             low++;
23         }
24         if (low < high) {
25             int temp = arr[high];
26             arr[high] = arr[low];
27             arr[low] = temp;
28             low++;
29             high--;
30         }
31     }
32 }
33 
34 int main() {
35     int arr[12] = { 0,-6,2,3,4,0,0,-1,-2,-3,-4,-5 };
36     negBeforePos(arr, 12);
37     for (int i = 0; i < 12; i++) {
38         printf("%d\t", arr[i]);
39     }
40     printf("\n");
41     return 0;
42 }
View Code
复制代码

17、

 代码:

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #pragma warning(disable:4700)
 4 #include <stdio.h>
 5 #include <string.h>
 6 
 7 int compare(int arr1[], int arr2[], int length) {
 8     int bigger = 0, letter = 0, equal = 0;
 9     for (int i = 0; i < 10; i++) {
10         if (arr1[i] > arr2[i]) {
11             bigger++;
12         }
13         else if (arr1[i] == arr2[i]) {
14             equal++;
15         }
16         else if (arr1[i] < arr2[i]) {
17             letter++;
18         }
19     }
20     return bigger - letter;
21 }
22 
23 int main() {
24     int a1[10] = { 3,1,2,2,1,3,3,2,1,1 };
25     int a2[10] = { 2,1,3,1,2,3,3,2,2,2 };
26     int com = compare(a1, a2, 10);
27     if (com > 0)
28         printf("bigger %d\n", com);
29     else if (com == 0)
30         printf("equal\n");
31     else 
32         printf("letter %d\n", com);
33     return 0;
34 }
View Code
复制代码

结果:

 

18、

 代码:

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 void reverse(char* str) {
 7     char* front = str;
 8     int len = 0;
 9     while (*front != '\0') {
10         len++;
11         front++;
12     }
13     char* tail = str + len - 1;
14     front = str;
15     while (front <= tail) {
16         char temp = *front;
17         *front = *tail;
18         *tail = temp;
19         front++;
20         tail--;
21     }
22 }
23 
24 int main() {
25     char str[100];
26     scanf("%s", str);
27     reverse(str);
28     printf("转换后:%s\n", str);
29     return 0;
30 }
View Code
复制代码

 结果:

19、

 代码:

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #pragma warning(disable:4700)
 4 #include <stdio.h>
 5 #include <string.h>
 6 
 7 /// <summary>
 8 /// 
 9 /// </summary>
10 /// <param name="a"></param>
11 /// <param name="n"></param>
12 void func(int a, int n) {
13     int sum = 0, p = 0;
14     for (int i = 0; i < n; i++) {
15         p = p * 10 + a;
16         sum += p;
17     }
18     printf("%d", sum);
19 }
20 
21 int main() {
22     int a, n;
23     printf("enter:\n");
24     scanf("%d%d", &a, &n);
25     func(a, n);
26     return 0;
27 }
View Code
复制代码

结果:

20、

代码:

复制代码
 1 #pragma warning(disable:4996)
 2 #pragma warning(disable:6031)
 3 #pragma warning(disable:4700)
 4 #include <stdio.h>
 5 #include <string.h>
 6 
 7 int process(int a, int b) {
 8     int static times = 0;
 9     times++;
10     if (times == 1){
11         return a > b ? a : b;
12     }
13     else if (times == 2) {
14         return a < b ? a : b;
15     }
16     else if (times == 3) {
17         return a + b;
18     }
19 }
20 
21 int main() {
22     printf("%d\n", process(1, 2));
23     printf("%d\n", process(1, 2));
24     printf("%d\n", process(1, 2));
25     return 0;
26 }
View Code
复制代码

结果:

 

posted @   船长华莱士  阅读(109)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示