第1题
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.applet.*;
import java.util.StringTokenizer;
public class ex27_3 extends Applet{
private TextField tf;
private Button btn;
public void init(){
tf = new TextField(25);
add(tf);
btn= new Button("分析");
add(btn);
resize(250, 200);
}
public void paint(Graphics g){
String str = tf.getText();
StringTokenizer st = new StringTokenizer(tf);
int n = 80;
while (!st.hasMoreTokens()){
String tem = st.nextToken();
g.drawString(tem, n, 80);
n += 20;
}
}
public boolean action(Event e, Object o){
if (o == "分析") {
repaint();
return true;
}
else
return false;
}
}
ex27_3.html
<HTML>
<HEAD>
<TITLE>ex27_3</TITLE>
</HEAD>
<BODY>
<applet code="ex27_3.class" width=800 height=400
</applet>
</BODY>
</HTML>
第2题
例如:str=“how do you do”,结果为:num=4。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
void fun(char *s,int *num)
{
int i,n=0;
for(i=0;【 】;i++)
{
if(s[i]>='a',&&s[i]<='z',&&(s[i+1)==’’||s[i+1]=='\0'))
【 】;
}
【 】;
}
main()
{
char str[N];
int num=0;
printf("Enter a string:\n");
gets(str);
while(str[num])
num++;
fun(str,&num);
printf("The number of word is:
%d\n\n",num);
第3题
S=“S1S2…Sn”是一个长为n的字符串,存放在一个数组中,编程序将S改造之后输出。 S = "S1S2 ... Sn" is a string of length n, and stored in an array, output S after its programmable transformation. 1.将S的所有第偶数个字符按照其原来的下标从大到小的次序放在S的后半部分; 1.All the even-numbered characters of S should be placed in accordance with their subscript descending order in the second half of S; 2.将S的所有第奇数个字符按照其原来的下标从小到大的次序放在S的前半部分; 2.All the odd-numbered characters of S should be placed in accordance with their subscript ascending order in the first half of S. 例如:S=‘ABCDEFGHIJKL’,则改造后的S为‘ACEGIKLJHFDB’。则 S=’algorithm’, 改造后为____________(Hint: 1. 答案不需要加引号 2. 系统基于字符匹配来判定答案,所以您的答案中不要出现空格)。 For example: S = 'ABCDEFGHIJKL', then after the transformation S is 'ACEGIKLJHFDB'. If S = 'algorithm', then after the transformation S is ____________ (Hint:1. please don’t include any quotes in your answer. 2.This problem is judged by string matching, Please make sure your answer don't contain any blanks ).
第4题
试设计一个算法,利用T公司提供的m个补丁程序,将原软件修复成一个没有错误的软件,并使修复后的软件耗时最少.
算法设计:对于给定的n个错误和m个补丁程序,找到总耗时最少的软件修复方案.
数据输入:由文件input.txt提供输入数据.文件第1行有2个正整数n和m,n表示错误总数,m表示补丁总数(1≤n≤20,1≤m≤100).接下来m行给出了m个补丁的信息.每行包括一个正整数,表示运行补丁程序i所需时间以及2个长度为n的字符串,中间用个空格符隔开.在第1个字符串中,如果第k个字符bk为“+”,则表示第k个错误属于B1[i],若为“-”,则表示第k个错误属于B2[i],若为“0”,则第k个错误既不属于B1[i]也不属于B2[i],即软件中是否包含第k个错误并不影响补丁i的可用性.在第2个字符串中,如果第k个字符bk为“+”,则表示第k个错误属于F1[i],若为“-”,则表示第k个错误属于F2[i],若为“0”,则第k个错误既不属于F1[i]也不属于F2[i],即软件中是否包含第k个错误不会因使用补丁i而改变.
结果输出:将总耗时数输出到文件output.txt.如果问题无解,则输出0.
第5题
阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
该程序的功能是从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中,最后把结果xx输出到文件OUT6.DAT中。
例如:原文:You He Me
I am a student.
结果:Me He You
student a am I
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
【函数】
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
char xx[50][80];
int maxline=0;/*文章的总行数*/
int ReaaDat(void);
void WriteDat(void);
void StrOL(void)
{
char*pl,*p2,t[80];
int i;
for(i=0;i<maxline;i++)
{p1=xx[i];t[0]=0;
while(*p1)p1++;
while(p1>=xx[i])
{while(!isalpha(*p1)&&p1!=xx[i])p1--;
p2=p1;
while( (1) )p1--;
if(p1==xx[i])
if(isalpha(*p1))p1--;
else if(!isalpha(*(p1+1)))break;
p2++;
(2) ;
strcat(t,p1+1);
strcat(t," ");
}
strcpy(xx[i],t);
}
}
void main()
{
if( (3) ){
printf("数据文件in.dat不能打开!\n\007");
return;
}
StrOL();
writeDat();
getch();
}
int ReadDat(void)
{
FILE*fp;
int i=0;
char*p;
if((fp=fopen("e:\\a\\in.dat","r"))==NULL)return 1;
while(fgets(xx[i],80,fp)!=NULL){
p=strchr(xx[i],′\n′);
if(p)*p=0;
i++;
}
maxline= (4)
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE*fp;
int i;
fp=fopen("e:\\a\\out6.dat","w");
for(i=0;i< (5) ;i++){
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
第6题
【说明】
该程序的功能是从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中,最后把结果xx输出到文件OUT6.DAT中。
例如:原文:You He Me
I am a student.
结果:Me He You
student a am I
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
【函数】
include<string.h>
include<conio.h>
include<ctype.h>
include<stdio.h>
char xx[50] [80];
int maxline=0; /*文章的总行数*/
int ReaaDat(void);
void WriteDat(void);
void StrOL(void)
{
char * p1, * p2,t[80];
int i;
for(i=0;i<maxline;i++)
{ p1=xx[i];t[0]=0;
while(*p1)p1++;
while(p1>=xx[i])
{ while(!isalpha(*p1) &&p1!=xx[i])p1--;
p2=p1;
while((1))p1--;
if(p1==xx[i])
if(isalpha(*p1))p1--;
else if(!isalpha(*(p1+1)))break;
p2++;
(2);
strcat(t, p1+1);
strcat(t," ");
}
strcpy(xx[i],t);
}
}
void main( )
{
if((3)) {
printf("数据文件in.dat不能打开!\n\007" );
return;
}
StroL();
writeDat();
getch();
} int ReadDat(void)
{
FILE * fp;
int i =0;
char * p;
if((fp=fopen("e:\a\in.dat"," r" ))==NULL)return 1;
while(fgets(xx[i],80,fp)!=NULL) {
p=strchr(xx[i],'\n')
if(p)*p=0;
i++;
}
maxline=(4)
fclose(fp);
return 0;
}
void WriteDat(void)
{
FILE * fp;
int i;
fp=fopen("e:\\a\\out6,dat","w");
for(i=0;i<(5);i++){
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i])
}
fclose(fp)
}
第7题
出租车计费问题 已知出租车收费标准为:起步里程为3公里,起步费为8元,10公里以内超过起步里程的部分,每公里加收2元,超过10公里以上的部分加收50%的回空补贴费,即每公里3元。出租车营运过程中,因堵车和乘客要求临时停车等客的,按每5分钟加收2元计算,不足5分钟的不计费。 从键盘任意输入行驶里程(精确到0.1公里)和等待时间(精确到分钟),请编程计算并输出乘客应支付的车费,对结果进行四舍五入,精确到元。 程序运行结果示例1: Input distance and time:2,2↙ fee = 8 程序运行结果示例2: Input distance and time:5,5↙ fee = 14 程序运行结果示例3: Input distance and time:12,15↙ fee = 34 程序运行结果示例4: Input distance and time:20,0↙ fee = 52 输入提示信息:"Input distance and time:" 输入格式: 用逗号分隔的两个数字,第一个表示距离、第二个表示时间:"%f,%d" 输出格式:"fee = %.0f\n" (注意:等号的两边各有一个空格) 为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。 编程提示:可以先用分支语句按行驶里程计算费用,然后再用分支语句根据等待时间计算是否需要增加等待的费用。 如果把里程和等待时间同时作为条件考虑,比较繁琐。
第8题
例如,分别输入下面两个字符串:
FirstString
SecondString
则程序输出:
FirstStringSecondString
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号小填入所编写的若干语句。
试题程序:
include <stdio. h>
include<conio. h>
void fun (char pi[],char p2[])
{
}
main ( )
{
char s1[80],s2[40];
clrscr ();
printf("Enter s1 and s2:\n");
scanf ("%s%s", s1, s2);
printf ("s1=%s\n", s1);
printf("s2=%s\n",s2);
printf("Invoke fun(s1,s2) :\n");
fun(s1,s2);
printf("After invoking:\n");
printf ("%s\n", s1);
}
第9题
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!