问答题

【程序说明】
该程序定义了两个子函数strsort和strmerge。它们分别实现了将一个字符串按字母顺序排序和将两个字符串合并排序,并删去相同字符。在主函数里,先输入两个字符串s1和s2,然后调用strsort函数对它们分别排序,然后调用strmerge函数将s1和s2合并,将合并后的字符串赋给字符串s3,最后输出字符串s3。
【程序】
#include<stdio.h>
void strmerge(char,a,char *b,char *c) //将字符串a,b合并到字符串c中
char t,*w;
w=c;
while( (1) )
//找到字符串a,b当前字符中较小的字符
if(*a< *b)

t= *a;
(2)

else if (*a>*b)

t= *b;
(3)

else //字符串a,b当前字符相等

t= *a;
a++;
b++;

if( (4) ) //开始,可直接赋值
*w=t;
else if(t!=*w)
//如果a,b中较小的当前字符与c中当前字符不相等,才赋值 (5)
if(*a!=\’\0’) //如果字符串a还没有结束,则将a的剩余部分赋给C
while(*a!=’\0’)
if(*a!=*w)

*(++w)=*a;
a++;

else
(6)
if(*6!=’\0’) //如果字符串b还没有结束,则将b的剩余部分赋给c
while(*b!=’\0’)
if(*b! = *w)

*(++w)=*b;
b++;

else
b++;
(7)

void strsort(char*s) //将字符串S中的字符排序
int i,j,n;
char t,*w;
W=S;
for(n=0;*w!=’\0’;n++) //得到字符串长度n
w++;
for(i=0;i<n-1;i++) //对字符串s进行排序,按字母先后顺序
for(j=i+1;j<n;j++)
if( (8) )
t=s[i];
s[i]=s[j];
(9)
void main()
char s1[100],s2[100],s3[100];
printf("\nPlease,input the first string:");
scanf("%s",s1);
printf("\nPlease input the second string:");
scanf("%s",s2);
strsort(s1); //将字符串s1排序
strsort(s2); //将字符串s2排序
printf("%s\n",s1);
printf("%s\n",s2);
s3[0]=’\0’; //字符串s3的第一个字符先置’\0’结束标志
(10) //将s1和s2合并,按照字母顺序排列,
//且要删去相同字符,存入s3中
printf("%s",s3);

【参考答案】

(1)(*a!=’\0’)&&(*b! =’\0’) (2)a++
(3)b ++ (4)*w==’\0’ ......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
【说明】 下面是一个Applet程序,其功能是根据给出的小时,分钟和秒数计算相等的秒数,即将1分钟化为60秒,依此类推。要求建立一个时间类,时间参数均作为类的成员变量,并且给出换算时间的方法,也作为这个类的成员函数,可以供外部对象进行调用。同时还需要在输出窗口中显示换算结果,并且将结果写到out3_3.txt文件中,本题给出确定的时间为4小时23分47秒,要求换算成以秒做单位的时间。 程序运行结果如图11所示。 import java.io.*; import java.awt.*; impOrt java.applet.*; * <applet code=ex7_7.class width=800 height=400> < applet> * public class ex7_7 extends APPlet public void paint (Graphics g) int nSum; class myTime7_7 public int h; public int m; public int s; public int out; public int caculateSecond() (1) ; return out; myTime7_7 objTime7_7=new myTime7_7(); ObjTime7_7. h=4; ObjTime7_7.m=23; ObjTime7_7.s=47; nSum=objTime7_7. (2) ; g.drawString( 时: +objTime7_7.h,20,30); g.drawString( 分: +objTime7_7.m,20,50); g.drawString( 秒: +objTime7_7.s,20,70); g.drawString( (3) ); try FileOutputStream fos7_7=new FileOutputStream( out7_7.txt ); BufferedOutputStream bos7_7=new BufferedOutputStream(fos7_7,1024); PrintStream ps7_7=new PrintStream(bos7_7,false); SyStem.setOut(ps7_7); System.out.println( (4) ); ps7_7. close(); catch(|OException ioe) (5) (ioe); ex7_7.htm| <HTML> <HEAD> <TITLE>ex7_7< TITLE> < HEAD> <BODY> <appletcode= ex7_7.class width=800 height=400> < applet> < BODY> < HTML>
问答题
【程序5说明】 著名的四色定理指出任何平面区域图均可用四种颜色着色,使相邻区域着不同的颜色。本程序对给定的区域图找出所有可能的不超过四种颜色的着色方案。 程序中用1~4表示四种颜色。要着色的N个区域用0~N-1编号,区域相邻关系用 adj[][]矩阵表示,矩阵的i行j列的元素为1,表示区域i与区域j相邻;矩阵的i行j列的元素为0,表示区域i与区域j不相邻。数组color[]用来存储着色结果,color[i]的值为区域i所着颜色。 【程序5】 #include<stdio.h> #define N 10 void output(int color[]) *输出一种着色方案* int i; for(i=0;i<N;i++) printf( %4d ,color[i]); printf( n ); int back (int * ip,int color[]) *回溯* int c=4; while(c==4) if(*ip<=0)return 0; --(*ip); c= (1) ; color[*ip]=-1; return c; *检查区域i,对c种颜色的可用性* int colorOk(int i,int c,int [][N],int color[] int j; for(j=0;j<i;j++) if( (2) ) return 0; return 1; *为区域i选一种可着的颜色* int select (int i,int c,int adj[][N],int color[]) int k; for(k=c;k<=4;k++) if(colorOK( (3) )) return k; return 0; int coloring(int adj[][N]) *寻找各种着色方案* int color[N],i,c,cnt; for(i=0;i<N;i++)color[i] =-1; i=c=0;cnt=0; while(1) if((c= (4) )==0) c=back(&i,color); if(c==0)return cnt; else (5) ;i++; if(i==N) output(color); ++cnt; c=back(&i,color); else c=0; void main() int adj[N][N]= 0,1,0,1,1,1,1,1,1,1, 1,0,1,1,0,1,1,1,1,0, 0,1,0,1,0,1,1,0,1,1, 1,1,1,0,1,1,0,0,1,1, 1,0,0,1,0,1,0,0,0,0, 1,1,1,1,1,0,1,0,0,1, 1,1,1,0,0,1,0,0,1,0, 1,1,0,0,0,0,0,0,1,1, 1,1,1,1,0,0,1,1,0,1, 1,0,1,1,0,1,0,1,1,0 ; printf( 共有%d组解. n ,coloring(adj));