- 求一维数组中的第二大值
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, const char *argv[]) { int a[5]={}; for(int i=0;i<5;i++) { scanf("%d",&a[i]); } for(int j=1;j<5;j++) { for(int i=0;i<5-j;i++) { if(a[i]<a[i+1]) { int m= a[i]; a[i]=a[i+1]; a[i+1]=m; } } } printf("第二大值是:%d\n",a[1]); return 0; }
输出: 12 32 44 11 2 第二大值是:32
- 终端输入带空格的字符串,删除字符中的空格
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, const char *argv[]) { char str[10]={}; int n=0; gets(str); for(int i=0;i<10;i++) { if(str[i]==' ') { for(int j=i;j<10;j++) { str[j]=str[j+1]; } } } puts(str); return 0; }
输入: qdw fwd 输出: qdwfwd
- 自己实现剩余的string函数族函数,strcat、strcpy、strcmp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char a[]="hello";
char b[]="world";
printf("%s\n",strcpy(a,b));
printf("%ld\n",strlen(a));
printf("%d\n",strcmp(a,b));
printf("%s\n",strcat(a,b));
return 0;
}
输出:
world
5
0
worldwolld
4.整理思维导图
5.终端输入10个学生成绩,使用冒泡排序对学生成绩从低到高排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int a[10]={};
for(int i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
int len=sizeof(a)/sizeof(int);
for(int i=1;i<len;i++)
{
for(int j=0;j<len-i;j++)
{
if(a[j]>a[j+1])
{
int m=a[j];
a[j]=a[j+1];
a[j+1]=m;
}
}
}
for(int i=0;i<len;i++)
{
printf("%d ",a[i]);
}
putchar(10);
return 0;
}
输入:
99 82 32 55 68 87 94 83 99 100
输出:
32 55 68 82 83 87 94 99 99 100