1> 计算二维数组组最大和,以及最大差【不使用排序】
#include <stdio.h>
int main(int argc, const char *argv[])
{
//计算二维数组最大和,以及最大差
int line,row;
printf("输入行列:");
scanf("%d %d",&line,&row);
int arr[line][row];
for(int i=0;i<line;i++){
for(int j=0;j<row;j++){
printf("%d行,%d列:",i,j);
scanf("%d",&arr[i][j]);
}
}
int mini[2],maxi[2],max2i[2];
int min,max,max2;
min=max=max2=arr[0][0];
for(int i=0;i<line;i++){
for(int j=0;j<row;j++){
//min
if(min>arr[i][j]){
min=arr[i][j];
mini[0]=i;mini[1]=j;
}
//max
if(max<arr[i][j]){
max=arr[i][j];
maxi[0]=i;maxi[1]=j;
}
}
}
printf("%d,%d\n",maxi[0],maxi[1]);
#if 1
for(int i=0;i<line;i++){
for(int j=0;j<row;j++){
//max2
if(max2<arr[i][j]&&!(i==maxi[0]&&j==maxi[1])){
max2=arr[i][j];
printf("%d,%d\n",i,j);
}
}
}
printf("最大值:%d,第二大值:%d,最小值:%d\n",max,max2,min);
#endif
return 0;
}
2> 实现两个字符串交换【可以使用字符串函数】
3> 输入三个字符串str1\str2\str3,把三个字符串连接,连接顺序是str1\str2\str3,
请计算连接后字符串中大写字母、小写字母、数字、特殊字符的个数【使用字符串函数】
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//实现两个字符串的交换
char str1[10]="HEllo123";
char str2[10]="wORld!@#";
char str3[10];
strcpy(str3,str1);
strcpy(str1,str2);
strcpy(str2,str3);
printf("str1=%s\nstr2=%s\nstr3=%s\n",str1,str2,str3);
//拼接字符串,计算各种字符数量
char str_all[100];
strcat(str_all,str1);
strcat(str_all,str2);
strcat(str_all,str3);
printf("%s\n",str_all);
int ABC=0,abc=0,count=0,num=0;
for(int i=0;str_all[i]!='\0';i++){
//A~Z
if(str_all[i]>='A' && str_all[i]<='Z'){
ABC++;
}else if(str_all[i]>='a' && str_all[i]<='z'){
abc++;
}else if(str_all[i]>='0' && str_all[i]<='9'){
num++;
}else{
count++;
}
}
printf("大写:%d,小写:%d,数字:%d,其他:%d\n",ABC,abc,num,count);
return 0;
}
4> 使用非函数实现字符串拷贝和链接的功能
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//strlen() 计算字符串和字符数组的元素数量,返回ld类型
//strcpy() 拷贝字符串
char str1[20]="hello";
char str2[10]="hi";
printf("cp前str2的元素数量:%ld,字节大小:%ld\n",strlen(str2),sizeof(str2));
//strcpy(str1,str2);
printf("cp前str2的元素数量:%ld,字节大小:%ld\n",strlen(str2),sizeof(str2));
//非函数实现拷贝
int i;
for(i=0;str2[i]!='\0';i++){
str1[i]=str2[i];
}
str1[i]='\0';
printf("str1=%s\n",str1);
//strcat 字符串连接
strcat(str1,str2);
printf("%s\n",str1);
strcat(str1,"meme");
printf("%s\n",str1);
//手动实现
int j=0;
for(;str1[j]!='\0';j++);
for(int i=0;str2[i]!='\0';i++){
str1[j++]=str2[i];
}
str1[j]='\0';
puts(str1);
return 0;
}

867

被折叠的 条评论
为什么被折叠?



