d[j][i]=d[i][j]; for(i=0;i<4;i++) { printf(\为了保持每行的间隔与每列的相同*/ for(j=0;j<4;j++) if(j>=i) printf(\为了使位数不同的数输出依然保持三角型*/ else printf(\ } } 7、
*题目:输入一个3x4的二维数组,然后分别按行和按列输出。*/ #include
/*题目:编写程序,将两个字符串连接起来,不用strcat函数。*/ #include
{ char s1[50],s2[50]; int i=0,j=0,t=0; printf(\ \ gets(s1); while(s1[i]!='\\0') i++; /*统计s1中字符个数*/ printf(\ \ gets(s2); while(s2[j]!='\\0') j++; /*统计s2中字符个数*/
21
for(t=0;t<=j;t++) /*把s2中字符连接到s1后面*/ { s1[i]=s2[t]; i++;} printf(\ \ puts(s1); } 9、
/*输入一行字符串,统计字符对ab的个数。*/ #include
#include
int paixu(int a[],int n) /*构造对长度为n的数组排序的函数*/ { int i,j,t,k,temp;
for(i=0;i
if(k!=i) { t=a[i]; a[i]=a[k];
22
a[k]=t; } } temp=a[n-1]; return temp; /*返回最大字符串的长度的值*/ }
void main() { char str1[50],str2[50],str3[50],str4[50],str5[50],str6[50]; char str7[50],str8[50],str9[50],str10[50]; int count[10]={0},temp[10]={0},i,j; int paixu(int a[],int n); for(i=1;i<=10;i++) /*输入10个字符串*/ { printf(\ \ if(i==1) { gets(str1);while(str1[count[0]]){count[0]++;temp[0]++; }} if(i==2) { gets(str2);while(str2[count[1]]){count[1]++;temp[1]++;}} if(i==3) { gets(str3);while(str3[count[2]]){count[2]++;temp[2]++; }} if(i==4) { gets(str4);while(str4[count[3]]){count[3]++; temp[3]++;}} if(i==5) { gets(str5);while(str5[count[4]]){count[4]++; temp[4]++;}} if(i==6) { gets(str6);while(str6[count[5]]){count[5]++; temp[5]++;}} if(i==7) { gets(str7);while(str7[count[6]]){count[6]++; temp[6]++;}} if(i==8) { gets(str8);while(str8[count[7]]){count[7]++; temp[7]++;}} if(i==9) { gets(str9);while(str9[count[8]]){count[8]++; temp[8]++;}} if(i==10) { gets(str10);while(str10[count[9]]){count[9]++;temp[9]++;}} } j=paixu(temp,10); for(i=1;i<=10;i++) { if(count[i-1]==j) { /*只要字符串长度与最大长度相等就打印出来*/ switch(i) { case 1:puts(str1); printf(\ case 2:puts(str2); printf(\
23
case 3:puts(str3); printf(\ case 4:puts(str4); printf(\ case 5:puts(str5); printf(\ case 6:puts(str6); printf(\ case 7:puts(str7); printf(\ case 8:puts(str8); printf(\ case 9:puts(str9); printf(\ case 10:puts(str10); printf(\ } } } } 11、
/*给数组a输入m个按升序排列的数,给数组b输入n个按降序排列的数*/ /*将a与b中的元素按降序排列存在数组c中。*/ #include
int a[100],b[100],c[100],i,m,n,j,k,temp=0,s;
printf(\ \ /*确定M的个数*/ printf(\ \ /*确定N的个数*/ printf(\ \ for(s=0;s scanf(\ printf(\ \ for(s=0;s scanf(\ i=m-1;j=0;k=0; /*i,j,分别标记a[]与b[]且都是从最大元素开始做标记*/ for(;;) { if(i==0) /*a[]中只有一个数或者a[]处理到了最后一个元素*/ { if(a[i]<=b[j]) /*如果a[]的最后一个元素<=b[]中当前元素*/ { c[k]=b[j]; k++; j++; } else { c[k]=a[i]; k++; for(;;) /*把b[]数组接到c[]后面*/ { c[k]=b[j]; j++; k++; if(j==n) /*如果b[]处理完,结束。并做标记temp=1*/ { temp=1;break; } } } }if(temp==1)break; /*标记temp=1,全部处理结束*/ 24 if((a[i]<=b[j])&&(i!=0)&&(j!=n)) { c[k]=b[j]; j++; k++; } if((a[i]>b[j])&&(i!=0)&&(j!=n)) { c[k]=a[i]; i--; k++; } if(j==n) /*b[]处理到完*/ { for(;;) /*把b[]数组接到c[]后面*/ { if(i==0) /*如果a[]处理完,结束。并做标记temp=1*/ { c[k]=a[i];temp=1;break; } c[k]=a[i]; i--; k++; } }if(temp==1) break;/*标记temp=1,全部处理结束*/ } for(s=0;s 第八章 1: (1) A (2) B (3) D 2: (1) 10||10||10 (2) pogam 3: (1) *p!=0&&*q!=0 *p>=*q; q++ (2) i<11 &a[i] {i--;if(i==0)break;} i!=0 4、 /*题目:输入一个字符串,再删除指定的字符*/ /*如果字符串中没有指定字符,则输出原字符串。*/ #include void fun(char *str,char c) { char *p; for(p=str;*p!='\\0';p++) if(*p!=c) *str++=*p; *str='\\0'; } void main() 25