case4
- 格式:doc
- 大小:28.00 KB
- 文档页数:2
# include <reg51.h>int status=-1; //工作状态包括:0,空闲状;1.调整;2.计时开始;3.计时临近;4.计时终止;5.误按int TimeLeft=0; //倒计时时限int TimeSet=20;int Member=-1; //相关选手号码,-1表示该数值无效unsigned short TimeCounter=0;//以10ms为单位,对定时器进行计数,以便控制数码管、蜂鸣器和抢答计时(以秒为单位)//数码管每20ms进行一次的刷新,蜂鸣器的最大响停间隔为500ms,抢答计数以1s为单位。
//计数规则:最大计数为2000,代表20s.此时计数返回0struct{int LED1,LED2,BeepMode;}OutInfo;void Init() //系统初始化,主要是中断服务程序、定时器中断和定时器相关参数设置{;//初始化过程}void /*interrupt */Timer0(){void SetBeep(int command);void SetLED(int member,int time);TimeCounter++;if(TimeCounter==TimeSet*100)TimeCounter=0; //达到了计数顶点if((TimeCounter & 0x0001)==0) //显示LEDSetLED(OutInfo.LED1,OutInfo.LED2);if(status==2 && OutInfo.BeepMode==0) //计时开始,并且方式等于间歇输出,则调用输出蜂鸣器if( ((TimeCounter%50)&0x0001) ==0)SetBeep(0);elseSetBeep(1);if(TimeCounter%100==0 && (status==2 || status==3)) //已经开始计时,则每过一秒,剩余时间的计数值减一TimeLeft--;}void SetBeep(int command){if(command==0);////关闭蜂鸣器else;////开启蜂鸣器}void SetLED(int member,int time) //当出现-1,表示不显示该数码管{void Delay1ms();;//显示人员的数码管Delay1ms();;//显示剩余时间的数码管Delay1ms();}void Delay1ms() //基数时间等于1us.{int x;for(x=1000;x;x--) ;}void main(){int getkey();void showLED();void outBeep();void changeStatus(int keycode);int keycode=0; //中间变量if(status==-1){Init();status=0;}keycode=getkey();showLED();outBeep();changeStatus(keycode);}int getkey() //对所有按键进行编码.同时修改Member的数值,以及TimeSet的值{int key=0;;////你的代码,最终取值介于1-12if(status==0 || status==2 || status==3) //需要记录用户侧的按键if(key>=5 && key<=12)Member=key-4;if(status==1) //设置状态{if(key==3)TimeSet++;if(key==4)TimeSet--;}return key;//获取按键的编码,例如把主机侧设为1、2、3、4号,把客户侧设为5-12号,把无按键或者错误按键设为-1}void showLED(){void numberLED(int x);void TimeLED(int x);switch(status){case 0: //空闲状态TimeLED(0);numberLED(-1); //如果输入Member为-1,表示有选手犯规按下break;case 1: //调整状态TimeLED(TimeSet);break;case 2: //计时开始TimeLED(TimeLeft);numberLED(Member);break;case 3: //计时临近TimeLED(TimeLeft);numberLED(Member);break;case 4: //计时终止TimeLED(TimeLeft);numberLED(Member);break;case 5: //误按状态TimeLED(0);numberLED(Member);break;default: //错误break;}}void numberLED(int x) //输入-1,表示需要禁止显示{OutInfo.LED1=x;}void TimeLED(int x){OutInfo.LED2=x;}void outBeep(){void MBeep(int mode);switch(status){case 0: //空闲状态if(Member==-1)MBeep(1);break;case 1: //调整状态MBeep(2);break;case 2: //计时开始MBeep(0);break;case 3: //计时临近MBeep(1);break;case 4: //计时终止MBeep(1);break;case 5: //误按状态MBeep(1);break;default: //错误break;}}void MBeep(int mode) //0,代表0.5s的正常计时音;1,代表连续响声;2,代表关掉声音{OutInfo.BeepMode=mode;}void changeStatus(int keycode){switch(status){case 0: //空闲状态switch(keycode){case 1:status=2;break;//开始状态case 2:break;case 3:case 4:status=1;break; //设置状态case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12: status=5;break; //误按状态}break;case 1: //调整状态,当主持人发清零命令,进入空闲状态if(keycode==2)status=0;break;case 2: //计时开始,当有效按键及时间改变时,发生切换if (keycode>=5 && keycode<=12) //用户按键status=4;elseif(TimeLeft<=5) //剩余时间status=3;break;case 3: //计时临近if (keycode>=5 && keycode<=12) //用户按键status=4;elseif(TimeLeft==0) //计时完毕status=0;break;case 4: //计时终止,只等待清零if(keycode==2)status=0;break;case 5: //误按状态,等待清零,并需要记录犯规的用户编号?? if(keycode==2)status=0;break;default: //错误break;}}/*其中,StdAfx.h的内容如下:*/#if !defined(AFX_STDAFX_H__100A4F3F_F87C_4656_BCD5_715CBC48BBB0__INCLUDED _)# define AFX_STDAFX_H__100A4F3F_F87C_4656_BCD5_715CBC48BBB0__INCLUDED_# if _MSC_VER > 1000# pragma once# endif // _MSC_VER > 1000// TODO: reference additional headers your program requires here//{{AFX_INSERT_LOCA TION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.# endif // !defined(AFX_STDAFX_H__100A4F3F_F87C_4656_BCD5_715CBC48BBB0__INCLUDED _)//结束。
1962年11月,Overseas通过签署协议同意与RAKTA开设并管理一间在埃及Alexandria的纸板制造厂。
AID是美国国务院的一个分部,想给这项目资金援助。
所以AID给RAKTA 提供资金去购买了对Overseas有利的信用证。
合同条款里有用以提供解决执行过程中发生的争议方法的仲裁条款以及可赦免在执行过程中由于超出Overseas控制能力范围外的延误的不可抗力条款。
工作如同计划中顺利进行。
但到了1967年5月,阿拉伯以色列六日战争发生了,埃及人在对美国人(以色列敌国的主要盟国的侨民)宣泄憎恨的行为,迫使大部分Overseas工作团队人员离开埃及。
到了6月6日,埃及政府与美国断交,并下令驱逐所有美国人离境,除了那些申请并符合特殊签证要求的人。
迫于现实,将近完工的工程不得不被放弃。
Overseas通知RAKTA说这种延误按不可抗拒条款是可被赦免的。
RAKTA不同意,并发现因违反协议而发生的损害。
Overseas拒绝理赔,RAKTA已对Overseas履约,故援用仲裁条款。
Overseas回复说将这对执行条款的选择争议交由由ICC管理的三人仲裁委员会裁决。
最后的裁决于1973年3月作出:overseas需偿还RAKTA$321,507.45的违约金和$30,000的成本费。
最后裁决之后,overseas审核该宣告式判决以求避免通过RAKTA按overseas要求在美国银行开具的信用证执行的裁决。
该证规定了未来对overseas违约评估的仲裁法庭的处罚,RAKTA表示同意,认为对损失的仲裁结果符合该证对罚款和确认及国外裁决作成判决反索赔的规定。
Overseas反对的理由源于其所适用的《纽约公约》的表示语言,包括:裁决的执行会违背美国的公共政策;这裁决体现了这项仲裁不被恰当正确地仲裁;这裁决并非是建立在合约协议范围内的仲裁。
对公共政策应做狭义解释:不能用做保护本国利益的抗辩公共政策,其实质是法院地国根本的道德观念和政策,因而各国有异。
全国计算机等级考试等级考试真题库之选择题(18)1.下列叙述中正确的是A) 算法的效率只与问题的规模有关,而与数据的存储结构无关B) 算法的时间复杂度是指执行算法所需要的计算工作量C) 数据的逻辑结构与存储结构是一一对应的D) 算法的时间复杂度与空间复杂度一定相关参考答案:B【解析】算法的效率与问题的规模和数据的存储结构都有关,A错误。
算法的时间复杂度,是指执行算法所需要的计算工作量,B正确。
由于数据元素在计算机存储空间中的位置关系可能与逻辑关系不同,因此数据的逻辑结构和存储结构不是一一对应的,C错误。
算法的时间复杂度和空间复杂度没有直接的联系,D错误。
2.下列叙述中正确的是A) 线性表链式存储结构的存储空间一般要少于顺序存储结构B) 线性表链式存储结构与顺序存储结构的存储空间都是连续的C) 线性表链式存储结构的存储空间可以是连续的,也可以是不连续的D) 以上说法均错误参考答案:C【解析】线性表的顺序存储结构具备如下两个基本特征:(1)线性表中的所有元素所占的存储空间是连续的;(2)线性表中各数据元素在存储空间中是按逻辑顺序依次存放的。
用一组任意的存储单元来依次存放线性表的结点,这组存储单元既可以是连续的,也可以是不连续的,甚至是零散分布在存中的任意位置上的。
因此C正确。
3.某二叉树共有12个结点,其中叶子结点只有1个。
则该二叉树的深度为(根结点在第1层)A) 3B) 6C) 8D) 12参考答案:D【解析】二叉树中,度为0的节点数等于度为2的节点数加1,即n2=n0-1,叶子节点即度为0,n0=1,则n2=0,总节点数为12=n0+n1+n2=1+n1+0,则度为1的节点数n1=11,故深度为12,选D。
4.对长度为n的线性表作快速排序,在最坏情况下,比较次数为A) nB) n-1C) n(n-1)D) n(n-1)/2参考答案:D【解析】快速排序最坏情况就是每次选的基准数都和其他数做过比较,共需比较(n-1)+(n-2)+…+1=n(n-1)/2,选D。
C语言程序设计形成性考核册参考答案作业1一、选择题1. 在每个C语言程序中都必须包含有这样一个函数,该函数的函数名为(A)。
A.main B.MAIN C.name D.funtion2.C语言原程序文件的缺省扩展名为(A)。
A.cpp B.exe C.obj D.C3.由C语言目标文件连接而成的可执行的缺省扩展名为(B)。
A.cpp B.exe C.obj D.C4.程序运行中需要从键盘输入多于一个数据时,各数据之间应使用(D)符号作为分隔符。
A.空格或逗号B.逗号或回车C.回车或分号D.空格或回车5.每个C语言程序的编译错误分为(B)类。
A.1 B.2 C.3 D.46.设x 和y 均为逻辑值,则x && y为真的条件是(A)。
A.它们均为真B.其中一个为真C.它们均为假D.其中一个为假7.设有语句“int a=12;a+=a*a;”,则执行结束后,a 的值为(C)。
A.12 B.144 C.156 D.2888.x>0 && x<=10的相反表达式为(A)。
A.x<=0 || X>10 B.x<=0 && x>10 C.x<=0 || x<=10 D.x>0 && x>10 9.字符串“a+b=12\n”的长度为(B)。
A.6 B.7 C.8 D.910.在下列符号常量定义中。
错误的定义语句格式为(C)。
A.const M1=10; B.const int M2=20; C.const M3 10 D.const char mark=’3’;11.带有随机函数的表达式rand()%20的值在(C)区间内,A.1~19 B.1~20 C.0~19 D.0~2012.当处理特定问题时的循环次数已知时,通常采用(A)循环来解决。
A.for B.while C.do-while D.switch13.在switch语句的每个case块中,假定都是以break语句结束的,则此switch语句容易被改写为(B)语句。
1.以下运算符中优先级最低的是(C)。
A、&&B、|C、||D、&2.不合法的if语句是:(D)A、if(表达式) 语句1 else 语句2B、if(表达式) 语句C、if(表达式1)语句1 else if(表达式2)语句2 else 语句3。
D、if 关系表达式语句1 else 语句23.以下不是无限循环的语句为(D)。
A、while(1){x++;}B、for(;;x++=i);C、for(i=10;;i--) sum+=i;D、for(y=0,x=1;x>++y;x=i++) i=x;4.调用函数时,基本类型变量做函数参数, 实参和对应的形参(B).A、不占用存储单元B、各自占用独立的存储单元C、同名时才能共用存储单元D、共占用一个存储单元5.已知有宏定义#define F(a) a*(a+2),则F(4+3)的值为(D).A、24B、15C、63D、31a[10]; 给数组a的所有元素分别赋值为1、2、3、……的语句是(D)A、for(i=1;i<11;i++)a[i]=i;B、for(i=1;i<11;i++)a[0]=1;C、for(i=1;i<11;i++)a[i+1]=i;D、for(i=1;i<11;i++)a[i-1]=i;7.若有说明:int a[3][4];则数组a中各元素(A)。
A 、不能得到确定的初值 B、可在程序的编译阶段得到初值0C、可在程序的运行阶段得到初值0D、可在程序的编译或运行阶段得到初值08. 已定义float a; int i;选择正确的输入语句(C).A、scanf("%6.2f%3d", &a,&i);B、scanf("%f%s", &a,&i);C、scanf("%6f%3d",&a,&i);D、scanf("%f%d", a,i);9 执行下面程序段后,k的值是:(B)int k=2;switch(k+1){ case 3:k+=1;case 4:k--;break;case 5:k*=3;case 6:++k;} A、10 B、2 C、28 b D、2711.以下程序段中,能够正确地执行循环的是(B)。
第四章习题答案一、选择题1)以下是if语句的基本形式:if(表达式) 语句其中"表达式"A)必须是逻辑表达式B)必须是关系表达式C)必须是逻辑表达式或关系表达式D)可以是任意合法的表达式2)以下选项中,值为1的表达式是()。
A)1-'0' B)1-'\0' C)'1'-0 D)'\0'-'0'3)若a是数值类型,则逻辑表达式(a==1)||(a!=1)的值是:A)1 B)0 C)2 D)不知道a的值,不能确定4)若变量已正确定义,在if (W)printf(“%d\n”,k);中,以下不可替代W的是A)a<>b+c B)ch=getchar()C)a==b+c D)a++5)设变量x和y均已正确定义并赋值,以下if语句中,在编译时将产生错误信息的是A)if(x++); B)if(x>y&&y!=0);C)if(x>y) x- - D)if(y<0) {;}else y++; else x++;6) 以下选项中,当x为大于1的奇数时,值为0的表达式A)x%2==1 B)x%2 C)x%2!=0 D)x%2==07) 设有条件表达式:(EXP)?i++;j--,则以下表达式中(EXP)完全等价的是A)(EXP= =0) B)(EXP!=0)C)(EXP= =1)D)(EXP!=1)8) 在以下给出的表达式中,与while(E)中的(E)不等价的表达式是A)(!E=0) B) (E>0||E<0) C) (E==0) D) (E!=0)9)若有定义int x,y;并已正确给变量赋值,则以下选项中与表达式(x-y)?(x++):(y++)中的条件表达式(x-y)等价的是()。
A)(x-y>0) B)(x-y<0) C)(x-y<0||x-y>0) D)(x-y==0)10) 若有表达式(w)?(--x):(++y),则其中与w等价的表达式是 ( )A)w==1 B) w==0 C) w! =1 D) w! =011) 设有定义: int a=1,b=2,c=3;,以下语句中执行效果与其它三个不同的是A)if(a>b) c=a,a=b,b=c; B)if(a>b){c=a,a=b,b=c;}C)if(a>b) c=a;a=b;b=c; D)if(a>b){c=a;a=b;b=c;}12) 已知字母A的ASCⅡ代码值为65,若变量kk为char型,以下不能正确判断出kk中的值为大写字母的表达式是A)kk>='A'&&kk<='Z' B)!(kk>='A'‖kk<='Z')C)(kk+32)>='a'&&(kk+32)<='z' D)isalpha(kk)&&(kk<91)isalpha(c) 判断参数c是否为英文字母头文件:ctype.hisupper(c) 判断参数c是否为大写英文字母是返回非零值,否则返回零islower(c ) 检查参数c是否为小写英文字母13)已有定义:char c; ,程序前面已在命令行中包含ctype.h文件,不能用于判断c中的字符是否为大写字母的表达式是A)isupper(c) B)’A’<=c<=’Z’C)’A’<=c&&c<=’Z’D)c<=(‘z’-32)&&(‘a’-32)<=c14) 当变量c的值不为2、4、6时,值也为“真”的表达式是A)(c==2)‖(c==4)‖(c==6) B)(c>=2&&c<=6)‖(c!=3)‖(c!=5)C)(c>=2&&c<=6)&&!(c%2) D)(c>=2&&c<=6)&&(c%2!=1)15) 若变量已正确定义,有以下程序段int a=3,b=5,c=7;if(a>b) a=b; c=a;if(c!=a) c=b;printf("%d,%d,%d\n",a,b,c);其输出结果是A)程序段有语法错 B)3,5,3 C)3,5,5 D)3,5,7 16)有以下程序#includemain(){int a=1,b=0;if(!a) b++;else if(a==0) if(a) b+=2;else b+=3;printf("%d\n",b); }程序运行后的输出结果是A)0 B)1 C)2 D)317)在嵌套使用if语句时,C语言规定else总是()。
第四章什么是算术运算什么是关系运算什么是逻辑运算解:略。
C语言中如何表示“真”和“假”系统如何判断一个量的“真”和“假”解:设有一个逻辑表达式,若其结果为“真”,则以1表示;若其结果为“假”,则以0表示。
但是判断一个逻辑量的值时,以0代表“真”,以非0代表“假”。
例如3&&5的值为“真”,系统给出3&&5的值为1。
写出下面各逻辑表达式的值。
设a=3,b=4,c=5。
(1) a+b>c&&b==c(2) a||b+c&&b-c(3) !(a>b)&&!c||1(4) !(x=a)&&(y=b)&&0(5) !(a+b)+c-1&&b+c/2解:(1) 0(2) 1(3) 1(4) 0(5) 1有3个整数a、b、c,由键盘输入,输出其中最大的数。
解:方法一#include <>main(){ int a,b,c;printf("请输入3个整数:");scanf("%d,%d,%d",&a,&b,&c);if(a<b)if(b<c) printf("max=%d\n",c);else printf("max=%d\n",b);else if(a<c) printf("max=%d\n",c);else printf("max=%d\n",a);}方法二:使用条件表达式,可以使程序更加简明、清晰。
程序如下:#include <>main(){ int a,b,c,temp,max;printf("请输入3个整数:");scanf("%d,%d,%d",&a,&b,&c);temp=(a>b)a:b; /* 将a和b中的大者存人temp中 */max=(temp>c)temp:c; /* 将a和b中的大者与c比较,取最大者*/printf("3个整数的最大数是%d\n”,max);}有一函数:写一程序,输入x值,输出y值。
一、单选题1、解释Java源文件,生成字节码文件所用到的命令A.cmd.exeB.java.exeC.javac.exeD.keytool.exe正确答案:C2、Java 源程序文件的后缀是A.cB.classC.txtD.java正确答案:D3、下列特点不是Java语言的特点A.跨平台B.安全性C.面向过程D.多线程正确答案:C4、public class Test{public static void main(String[] args){System.out.println("欢迎参加《Java编程基础》在线课程学习!"); }}该源代码保存的文件名为()A.Text.javaB.Text.classC.Text.java.txtD.Test.txt正确答案:A5、public class Test{public static void main(String[] args){System.out.println("欢迎参加《Java编程基础》在线课程学习!"); }}编译上述文件的命令为()A.java TestB.java Test.javaC.javac TestD.javac Test.java正确答案:D6、public class Test{public static void main(String[] args){System.out.println("欢迎参加《Java编程基础》在线课程学习!"); }}编译后生成的字节码文件名为()A.Test.class.txtB.Test.classC.Test.txtD.Test.java正确答案:B7、下面标识符正确的是A.hello worldB.DemoC.classD.123stu正确答案:B8、执行下面的语句后,a,b,c的值分别()int a=2;int b=(a++)*3;int c=(++a)*3A.4,6,12B.2,6,6C.3,9,9D.4,9,9正确答案:A9、下面的数据类型是float型的是()A.129B.8.6FC.33.8D.89l正确答案:B10、为一个boolean类型变量a赋值时,下列语句正确的是()A.boolean a="false";B.boolean a="真";C.boolean a=1;D.boolean a=(9>=10);正确答案:D11、以下标识符不合法的是()A.x3xB.de$fC.voidD.STRING正确答案:C12、int m=3,n=2,max=0;if(m>n){max=m;}else{max=n;}程序段执行后,max的值为()A.2B.3C.0D.1正确答案:B13、以下代码运行后,i的值和输出内容int i=2;if(i++==3){System.out.println("i==3");}A.i值为2,控制台输出i==3B.i值为2,控制台没有内容输出C.i值为3,控制台没有内容输出D.i值为3,控制台输出i==3正确答案:C14、switch(表达式) {case 值1:语句块1;break; case 值2:语句块2;break; case 值3:语句块3;break;……case 值n:语句块n;break; default:语句块n+1;}语句中“表达式”的类型不能为()A.浮点数类型B.字符串类型C.字符类型D.整数类型正确答案:A15、int i=10;while(i>0){i=i+1;if(i==10){break;}}A.循环一次都不执行B.while循环执行10次C.循环执行一次D.死循环正确答案:D16、如果有以下程序代码for(int i=1;i<10;i++){if(i==5){continue;}System.out.println("i=%d%n",i); }A.显示i=1到4,以及6到9B.显示i=1到9C.显示i=6到9D.显示i=1到4正确答案:A二、判断题1、Java中使用的是英文半角的字符正确答案:√2、可以将一个char类型赋值给int类型正确答案:√3、可以将一个double类型的值赋值给float类型的变量正确答案:×三、填空题1、Java字节码文件的后缀名字是()。
1.5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。
main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabcAㄩN3.7 要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
解:#include <stdio.h>main(){ char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’;c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5);}运行结果:密码是Glmre3.9求下面算术表达式的值。
第5章循环结构程序设计【第140页】5-2#include <stdio.h>#include <math.h> // 程序中用到数学函数fabs,应包含头文件math.n int main(){int sign=1,count=0; // sign用来表示数值的符号,count用来统计循环次数double pi=0.0,n=1.0,term=1.0; // pi开始代表多项式的值,最后代表π的值, n代表分母,term代表当前项的值while(fabs(term)>=1e-8) // 检查当前项term的绝对值是否大于或等于10的(-6)次方{pi=pi+term; // 把当前项term累加到pi中n=n+2; // n+2是下一项的分母sign=-sign; // sign代表符号,下一项的符号与上一项符号相反term=sign/n; // 求出下一项的值termcount++; // count累加1}pi=pi*4; // 多项式的和pi乘以4,才是π的近似值printf("pi=%10.8f\n",pi); // 输出π的近似值printf("count=%d\n",count); // 输出循环次数return 0;}5-3#include <stdio.h>int main(){int p,r,n,m,temp;printf("请输入两个正整数n,m:");scanf("%d,%d,",&n,&m);if (n<m){temp=n;n=m;m=temp;}p=n*m;while(m!=0){r=n%m;n=m;m=r;printf("它们的最大公约数为:%d\n",n);printf("它们的最小公约数为:%d\n",p/n);return 0;}5-4#include <stdio.h>int main(){char c;int letters=0,space=0,digit=0,other=0;printf("请输入一行字符:\n");while((c=getchar())!='\n'){if (c>='a' && c<='z' || c>='A' && c<='Z')letters++;else if (c==' ')space++;else if (c>='0' && c<='9')digit++;elseother++;}printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符数:%d\n",letters,space,digit,other);return 0;}5-5#include <stdio.h>int main(){int a,n,i=1,sn=0,tn=0;printf("a,n=:");scanf("%d,%d",&a,&n);while (i<=n){tn=tn+a; /*赋值后的tn为i个a组成数的值*/sn=sn+tn; /*赋值后的sn为多项式前i项之和*/a=a*10;++i;}printf("a+aa+aaa+...=%d\n",sn);return 0;5-6#include <stdio.h>int main(){double s=0,t=1;int n;for (n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+...+20!=%22.15e\n",s);return 0;}5-7#include <stdio.h>int main(){int n1=100,n2=50,n3=10;double k,s1=0,s2=0,s3=0;for (k=1;k<=n1;k++) /*计算1到100的和*/{s1=s1+k;}for (k=1;k<=n2;k++) /*计算1到50各数的平方和*/ {s2=s2+k*k;}for (k=1;k<=n3;k++) /*计算1到10的各倒数和*/{s3=s3+1/k;}printf("sum=%15.6f\n",s1+s2+s3);return 0;}5-8#include <stdio.h>int main(){int i,j,k,n;printf("parcissus numbers are ");for (n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if (n==i*i*i + j*j*j + k*k*k)printf("%d ",n);}printf("\n");return 0;}5-9-1#define M 1000 /*定义寻找范围*/#include <stdio.h>int main(){int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a<=M;a++) /* a是2-1000之间的整数,检查它是否完数*/{n=0; /* n用来累计a的因子的个数*/s=a; /* s用来存放尚未求出的因子之和,开始时等于a */for (i=1;i<a;i++) /* 检查i是否a的因子*/if (a%i==0) /* 如果i是a的因子*/{n++; /* n加1,表示新找到一个因子*/s=s-i; /* s减去已找到的因子,s的新值是尚未求出的因子之和*/ switch(n) /* 将找到的因子赋给k1...k9,或k10 */{case 1:k1=i; break; /* 找出的笫1个因子赋给k1 */case 2:k2=i; break; /* 找出的笫2个因子赋给k2 */case 3:k3=i; break; /* 找出的笫3个因子赋给k3 */case 4:k4=i; break; /* 找出的笫4个因子赋给k4 */case 5:k5=i; break; /* 找出的笫5个因子赋给k5 */case 6:k6=i; break; /* 找出的笫6个因子赋给k6 */case 7:k7=i; break; /* 找出的笫7个因子赋给k7 */case 8:k8=i; break; /* 找出的笫8个因子赋给k8 */case 9:k9=i; break; /*找出的笫9个因子赋给k9 */case 10:k10=i; break; /* 找出的笫10个因子赋给k10 */}}if (s==0){printf("%d ,Its factors are ",a);if (n>1) printf("%d,%d",k1,k2); /* n>1表示a至少有2个因子*/if (n>2) printf(",%d",k3); /* n>2表示至少有3个因子,故应再输出一个因子*/if (n>3) printf(",%d",k4); /* n>3表示至少有4个因子,故应再输出一个因子*/if (n>4) printf(",%d",k5); /* 以下类似*/if (n>5) printf(",%d",k6);if (n>6) printf(",%d",k7);if (n>7) printf(",%d",k8);if (n>8) printf(",%d",k9);if (n>9) printf(",%d",k10);printf("\n");}}return 0;}5-9-2#include <stdio.h>int main(){int m,s,i;for (m=2;m<1000;m++){s=0;for (i=1;i<m;i++)if ((m%i)==0) s=s+i;if(s==m){printf("%d,its factors are ",m);for (i=1;i<m;i++)if (m%i==0) printf("%d ",i);printf("\n");}}return 0;}5-10#include <stdio.h>int main(){int i,n=20;double a=2,b=1,s=0,t;for (i=1;i<=n;i++){s=s+a/b;t=a,a=a+b,b=t;}printf("sum=%16.10f\n",s);return 0;}5-11#include <stdio.h>int main(){double sn=100,hn=sn/2;int n;for (n=2;n<=10;n++){sn=sn+2*hn; /*第n次落地时共经过的米数*/hn=hn/2; /*第n次反跳高度*/}printf("第10次落地时共经过%f米\n",sn);printf("第10次反弹%f米\n",hn);return 0;}5-12#include <stdio.h>int main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/ x2=x1;day--;}printf("total=%d\n",x1);return 0;}5-13#include <stdio.h>#include <math.h>int main(){float a,x0,x1;printf("enter a positive number:");scanf("%f",&a);x0=a/2;x1=(x0+a/x0)/2;do{x0=x1;x1=(x0+a/x0)/2;}while(fabs(x0-x1)>=1e-5);printf("The square root of %5.2f is %8.5f\n",a,x1); return 0;}5-14#include <stdio.h>#include <math.h>int main(){double x1,x0,f,f1;x1=1.5;do{x0=x1;f=((2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1;}while(fabs(x1-x0)>=1e-5);printf("The root of equation is %5.2f\n",x1);return 0;}5-15#include <stdio.h>#include <math.h>int main(){float x0,x1,x2,fx0,fx1,fx2;do{printf("enter x1 & x2:");scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6;if ((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs (fx0)>=1e-5); printf("x=%6.2f\n",x0); return 0;}5-16#include <stdio.h>int main(){int i,j,k;for (i=0;i<=3;i++){for (j=0;j<=2-i;j++)printf(" ");for (k=0;k<=2*i;k++)printf("*");printf("\n");}for (i=0;i<=2;i++){for (j=0;j<=i;j++)printf(" ");for (k=0;k<=4-2*i;k++)printf("*");printf("\n");}return 0;}5-17#include <stdio.h>int main(){char i,j,k; /*是a的对手;j是b的对手;k是c的对手*/ for (i='x';i<='z';i++)for (j='x';j<='z';j++)if (i!=j)for (k='x';k<='z';k++)if (i!=k && j!=k)if (i!='x' && k!='x' && k!='z')printf("A--%c\nB--%c\nC--%c\n",i,j,k);return 0;}第6章利用数组处理批量数据【第168页】6-1#include <stdio.h>#include <math.h>int main(){int i,j,n,a[101];for (i=1;i<=100;i++)a[i]=i;a[1]=0;for (i=2;i<sqrt(100);i++)for (j=i+1;j<=100;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0;}printf("\n");for (i=2,n=0;i<=100;i++){ if(a[i]!=0){printf("%5d",a[i]);n++;}if(n==10){printf("\n");n=0;}}printf("\n");return 0;}6-2#include <stdio.h>int main(){int i,j,min,temp,a[11];printf("enter data:\n");for (i=1;i<=10;i++){printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");printf("The orginal numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");for (i=1;i<=9;i++){min=i;for (j=i+1;j<=10;j++)if (a[min]>a[j]) min=j;temp=a[i];a[i]=a[min];a[min]=temp;}printf("\nThe sorted numbers:\n"); for (i=1;i<=10;i++)printf("%5d",a[i]);printf("\n");return 0;}6-3#include <stdio.h>int main(){int a[3][3],sum=0;int i,j;printf("enter data:\n");for (i=0;i<3;i++)for (j=0;j<3;j++)scanf("%3d",&a[i][j]);for (i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%6d\n",sum);return 0;}6-4#include <stdio.h>int main(){ int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j;printf("array a:\n");for (i=0;i<10;i++)printf("%5d",a[i]);printf("\n");printf("insert data:");scanf("%d",&number);end=a[9];if (number>end)a[10]=number;else{for (i=0;i<10;i++){if (a[i]>number){temp1=a[i];a[i]=number;for (j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}printf("Now array a:\n");for (i=0;i<11;i++)printf("%5d",a[i]);printf("\n");return 0;}6-5#include <stdio.h>#define N 5int main(){ int a[N],i,temp;printf("enter array a:\n");for (i=0;i<N;i++)scanf("%d",&a[i]);printf("array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);for (i=0;i<N/2;i++) //循环的作用是将对称的元素的值互换{ temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}printf("\nNow,array a:\n");for (i=0;i<N;i++)printf("%4d",a[i]);printf("\n");return 0;}6-6#include <stdio.h>#define N 10int main(){ int i,j,a[N][N];for (i=0;i<N;i++){a[i][i]=1;a[i][0]=1;}for (i=2;i<N;i++)for (j=1;j<=i-1;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for (i=0;i<N;i++){for (j=0;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}printf("\n");return 0;}6-7#include <stdio.h>int main(){ int a[15][15],i,j,k,p,n;p=1;while(p==1){printf("enter n(n=1--15):");scanf("%d",&n);if ((n!=0) && (n<=15) && (n%2!=0))p=0;}for (i=1;i<=n;i++)for (j=1;j<=n;j++)a[i][j]=0;j=n/2+1;a[1][j]=1;for (k=2;k<=n*n;k++){i=i-1;j=j+1;if ((i<1) && (j>n)){i=i+2;j=j-1;}else{if (i<1) i=n;if (j>n) j=1;}if (a[i][j]==0)a[i][j]=k;else{i=i+2;j=j-1;a[i][j]=k;}}for (i=1;i<=n;i++){for (j=1;j<=n;j++)printf("%5d",a[i][j]);printf("\n");}return 0;}#include <stdio.h>#define N 4#define M 5 /* 数组为4行5列*/int main(){int i,j,k,a[N][M],max,maxj,flag;printf("please input matrix:\n");for (i=0;i<N;i++) /* 输入数组*/for (j=0;j<M;j++)scanf("%d",&a[i][j]);for (i=0;i<N;i++){max=a[i][0]; /* 开始时假设a[i][0]最大*/maxj=0; /* 将列号0赋给maxj保存*/for (j=0;j<M;j++) /* 找出第i行中的最大数*/if (a[i][j]>max){max=a[i][j]; /* 将本行的最大数存放在max中*/maxj=j; /* 将最大数所在的列号存放在maxj中*/}flag=1; /* 先假设是鞍点,以flag为1代表*/for (k=0;k<N;k++)if (max>a[k][maxj]) /* 将最大数和其同列元素相比*/{flag=0; /* 如果max不是同列最小,表示不是鞍点令flag1为0 */continue;}if(flag) /* 如果flag1为1表示是鞍点*/{printf("a[%d][%d]=%d\n",i,maxj,max); /* 输出鞍点的值和所在行列号*/break;}}if(!flag) /* 如果flag为0表示鞍点不存在*/printf("It is not exist!\n");return 0;}6-9#include <stdio.h>#define N 15int main(){ int i,number,top,bott,mid,loca,a[N],flag=1,sign;char c;printf("enter data:\n");scanf("%d",&a[0]);while(i<N){scanf("%d",&a[i]);if (a[i]>=a[i-1])i++;elseprintf("enter this data again:\n");}printf("\n");for (i=0;i<N;i++)printf("%5d",a[i]);printf("\n");while(flag){printf("input number to look for:");scanf("%d",&number);sign=0;top=0; //top是查找区间的起始位置bott=N-1; //bott是查找区间的最末位置if ((number<a[0])||(number>a[N-1])) //要查的数不在查找区间内loca=-1; // 表示找不到while ((!sign) && (top<=bott)){mid=(bott+top)/2;if (number==a[mid]){loca=mid;printf("Has found %d, its position is %d\n",number,loca+1);sign=1;}else if (number<a[mid])bott=mid-1;elsetop=mid+1;}if(!sign||loca==-1)printf("cannot find %d.\n",number);;printf("continu or not(Y/N)?");scanf(" %c",&c);if (c=='N'||c=='n')flag=0;}return 0;}6-10#include <stdio.h>int main(){int i,j,upp,low,dig,spa,oth;char text[3][80];upp=low=dig=spa=oth=0;for (i=0;i<3;i++){ printf("please input line %d:\n",i+1);gets(text[i]);for (j=0;j<80 && text[i][j]!='\0';j++){if (text[i][j]>='A'&& text[i][j]<='Z')upp++;else if (text[i][j]>='a' && text[i][j]<='z')low++;else if (text[i][j]>='0' && text[i][j]<='9')dig++;else if (text[i][j]==' ')spa++;elseoth++;}}printf("\nupper case: %d\n",upp);printf("lower case: %d\n",low);printf("digit : %d\n",dig);printf("space : %d\n",spa);printf("other : %d\n",oth);return 0;}6-11#include <stdio.h>int main(){ char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for (i=0;i<5;i++){ printf("\n");printf(" ");for (j=1;j<=i;j++)printf("%c",space);for (k=0;k<5;k++)printf("%c",a[k]);}printf("\n");return 0;}6-12a-c#include <stdio.h>int main(){ int j,n;char ch[80],tran[80];printf("input cipher code:");gets(ch);printf("\ncipher code :%s",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))tran[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z'))tran[j]=219-ch[j];elsetran[j]=ch[j];j++;}n=j;printf("\noriginal text:");for (j=0;j<n;j++)putchar(tran[j]);printf("\n");return 0;}6-12b#include <stdio.h>int main(){int j,n;char ch[80];printf("input cipher code:\n");gets(ch);printf("\ncipher code:%s\n",ch);j=0;while (ch[j]!='\0'){ if ((ch[j]>='A') && (ch[j]<='Z'))ch[j]=155-ch[j];else if ((ch[j]>='a') && (ch[j]<='z'))ch[j]=219-ch[j];elsech[j]=ch[j];j++;}n=j;printf("original text:");for (j=0;j<n;j++)putchar(ch[j]);printf("\n");return 0;}6-13#include <stdio.h>int main(){ char s1[80],s2[40];int i=0,j=0;printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);while (s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("\nThe new string is:%s\n",s1); return 0;}6-14#include <stdio.h>int main(){ int i,resu;char s1[100],s2[100];printf("input string1:");gets(s1);printf("\ninput string2:");gets(s2);i=0;while ((s1[i]==s2[i]) && (s1[i]!='\0'))i++;if (s1[i]=='\0' && s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("\nresult:%d.\n",resu);return 0;}6-15#include <stdio.h>#include <string.h>int main(){ char s1[80],s2[80];int i;printf("input s2:");scanf("%s",s2);for (i=0;i<=strlen(s2);i++)s1[i]=s2[i];printf("s1:%s\n",s1);return 0;}第7章用函数实现模块化程序设计【第218页】7-1-1#include <stdio.h>int main(){int hcf(int,int);int lcd(int,int,int);int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d\n",h);l=lcd(u,v,h);printf("L.C.D=%d\n",l);return 0;}int hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}return(v);}int lcd(int u,int v,int h){return(u*v/h);}7-1-2#include <stdio.h>int Hcf,Lcd;int main(){void hcf(int,int);void lcd(int,int);int u,v;scanf("%d,%d",&u,&v); hcf(u,v);lcd(u,v);printf("H.C.F=%d\n",Hcf); printf("L.C.D=%d\n",Lcd); return 0;}void hcf(int u,int v){int t,r;if (v>u){t=u;u=v;v=t;}while ((r=u%v)!=0){u=v;v=r;}Hcf=v;}void lcd(int u,int v){Lcd=u*v/Hcf;}7-2#include <stdio.h>#include <math.h>float x1,x2,disc,p,q;int main(){void greater_than_zero(float,float);void equal_to_zero(float,float);void smaller_than_zero(float,float);float a,b,c;printf("input a,b,c:");scanf("%f,%f,%f",&a,&b,&c);printf("equation: %5.2f*x*x+%5.2f*x+%5.2f=0\n",a,b,c); disc=b*b-4*a*c;printf("root:\n");if (disc>0){greater_than_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else if (disc==0){equal_to_zero(a,b);printf("x1=%f\t\tx2=%f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%f+%fi\tx2=%f-%fi\n",p,q,p,q);}return 0;}void greater_than_zero(float a,float b){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}void equal_to_zero(float a,float b){x1=x2=(-b)/(2*a);}void smaller_than_zero(float a,float b){p=-b/(2*a);q=sqrt(-disc)/(2*a);}7-3#include <stdio.h>int main(){int prime(int);int n;printf("input an integer:");scanf("%d",&n);if (prime(n))printf("%d is a prime.\n",n); elseprintf("%d is not a prime.\n",n); return 0;}int prime(int n){int flag=1,i;for (i=2;i<n/2 && flag==1;i++)if (n%i==0)flag=0;return(flag);}7-4#include <stdio.h>#define N 3int array[N][N];int main(){ void convert(int array[][3]);int i,j;printf("input array:\n");for (i=0;i<N;i++)for (j=0;j<N;j++)scanf("%d",&array[i][j]); printf("\noriginal array :\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}convert(array);printf("convert array:\n");for (i=0;i<N;i++){for (j=0;j<N;j++)printf("%5d",array[i][j]);printf("\n");}return 0;}void convert(int array[][3]){int i,j,t;for (i=0;i<N;i++)for (j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}#include <stdio.h>#include <string.h>int main(){void inverse(char str[]);char str[100];printf("input string:");scanf("%s",str);inverse(str);printf("inverse string:%s\n",str);return 0;}void inverse(char str[]){char t;int i,j;for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--) {t=str[i];str[i]=str[j-1];str[j-1]=t;}}7-6#include <stdio.h>int main(){void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100];printf("input string1:");scanf("%s",s1);printf("input string2:");scanf("%s",s2);concatenate(s1,s2,s);printf("\nThe new string is %s\n",s);return 0;}void concatenate(char string1[],char string2[],char string[]) {int i,j;for (i=0;string1[i]!='\0';i++)string[i]=string1[i];for(j=0;string2[j]!='\0';j++)string[i+j]=string2[j];string[i+j]='\0';}7-7#include <stdio.h>int main(){void cpy(char [],char []);char str[80],c[80];printf("input string:");gets(str);cpy(str,c);printf("The vowel letters are:%s\n",c);return 0;}void cpy(char s[],char c[]){ int i,j;for (i=0,j=0;s[i]!='\0';i++)if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}7-8#include <stdio.h>#include <string.h>int main(){char str[80];void insert(char []);printf("input four digits:");scanf("%s",str);insert(str);return 0;}void insert(char str[]){int i;for (i=strlen(str);i>0;i--){str[2*i]=str[i];str[2*i-1]=' ';}printf("output:\n%s\n",str);}7-9#include <stdio.h>int letter,digit,space,others;int main(){void count(char []);char text[80];printf("input string:\n");gets(text);printf("string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("\nletter:%d\ndigit:%d\nspace:%d\nothers:%d\n",letter,digit,space,others); return 0;}void count(char str[]){int i;for (i=0;str[i]!='\0';i++)if ((str[i]>='a'&& str[i]<='z')||(str[i]>='A' && str[i]<='Z'))letter++;else if (str[i]>='0' && str [i]<='9')digit++;else if (str[i]==32)space++;elseothers++;}7-10#include <stdio.h>#include <string.h>int main(){int alphabetic(char);int longest(char []);int i;char line[100];printf("input one line:\n");gets(line);printf("The longest word is :");for (i=longest(line);alphabetic(line[i]);i++)printf("%c",line[i]);printf("\n");return 0;}int alphabetic(char c){if ((c>='a' && c<='z')||(c>='A'&&c<='z'))return(1);elsereturn(0);}int longest(char string[]){int len=0,i,length=0,flag=1,place=0,point;for (i=0;i<=strlen(string);i++)if (alphabetic(string[i]))if (flag){point=i;flag=0;}elselen++;else{flag=1;if (len>=length){length=len;place=point;len=0;}}return(place);}7-11#include <stdio.h>#include <string.h>#define N 10char str[N];int main(){void sort(char []);int i,flag;for (flag=1;flag==1;){printf("input string:\n");scanf("%s",&str);if (strlen(str)>N)printf("string too long,input again!");elseflag=0;}sort(str);printf("string sorted:\n");for (i=0;i<N;i++)printf("%c",str[i]);printf("\n");return 0;}void sort(char str[]){int i,j;char t;for(j=1;j<N;j++)for (i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}7-12#include <stdio.h>#include <math.h>int main(){float solut(float a,float b,float c,float d); float a,b,c,d;printf("input a,b,c,d:");scanf("%f,%f,%f,%f",&a,&b,&c,&d); printf("x=%10.7f\n",solut(a,b,c,d)); return 0;}float solut(float a,float b,float c,float d) {float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-3);return(x);}7-13#include <stdio.h>#define N 10#define M 5float score[N][M];float a_stu[N],a_cour[M];int r,c;int main(){ int i,j;float h;float s_var(void);float highest();void input_stu(void);void aver_stu(void);void aver_cour(void);input_stu();aver_stu();aver_cour();printf("\n NO. cour1 cour2 cour3 cour4 cour5 aver\n"); for(i=0;i<N;i++){printf("\n NO %2d ",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f\n",a_stu[i]);}printf("\naverage:");for (j=0;j<M;j++)printf("%8.2f",a_cour[j]);printf("\n");h=highest();printf("highest:%7.2f NO. %2d course %2d\n",h,r,c);printf("variance %8.2f\n",s_var());return 0;}void input_stu(void){int i,j;for (i=0;i<N;i++){printf("\ninput score of student%2d:\n",i+1);for (j=0;j<M;j++)scanf("%f",&score[i][j]);}}void aver_stu(void){int i,j;float s;for (i=0;i<N;i++){for (j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}void aver_cour(void){int i,j;float s;for (j=0;j<M;j++){s=0;for (i=0;i<N;i++)s+=score[i][j];a_cour[j]=s/(float)N;}}float highest(){float high;int i,j;high=score[0][0];for (i=0;i<N;i++)for (j=0;j<M;j++)if (score[i][j]>high){high=score[i][j];r=i+1;c=j+1;}return(high);}float s_var(void){int i;float sumx,sumxn;sumx=0.0;sumxn=0.0;for (i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }7-14#include <stdio.h>#define N 10#define M 5float score[N][M];float a_stu[N],a_cour[M];int r,c;int main(){ int i,j;float h;float s_var(void);float highest();void input_stu(void);void aver_stu(void);void aver_cour(void);input_stu();aver_stu();aver_cour();printf("\n NO. cour1 cour2 cour3 cour4 cour5 aver\n"); for(i=0;i<N;i++){printf("\n NO %2d ",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f\n",a_stu[i]);}printf("\naverage:");for (j=0;j<M;j++)printf("%8.2f",a_cour[j]);printf("\n");h=highest();printf("highest:%7.2f NO. %2d course %2d\n",h,r,c);printf("variance %8.2f\n",s_var());return 0;}void input_stu(void){int i,j;for (i=0;i<N;i++){printf("\ninput score of student%2d:\n",i+1);for (j=0;j<M;j++)scanf("%f",&score[i][j]);}}void aver_stu(void){int i,j;float s;for (i=0;i<N;i++){for (j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}void aver_cour(void){int i,j;float s;for (j=0;j<M;j++){s=0;for (i=0;i<N;i++)s+=score[i][j];a_cour[j]=s/(float)N;}}float highest(){float high;int i,j;high=score[0][0];for (i=0;i<N;i++)for (j=0;j<M;j++)if (score[i][j]>high){high=score[i][j];r=i+1;c=j+1;}return(high);}float s_var(void){int i;float sumx,sumxn;sumx=0.0;sumxn=0.0;for (i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];。
U2case4 lihongzhang’s embarrassmentComment李所说的只是一些惯用的公式化的礼貌表达;几乎所有的中国人都知道这一点,几乎到处都能听到。
就文字意义而言,李的话是不恰当的,但却传达了尊重的含义。
这里的语言形式不同于它的内容,这种现象只存在于东方文化中。
然而,这起案件发生在美国,观众是美国人。
美国是一个高度重视个人主义的国家,他们很直接地表达自己。
他们不理解东方人的谦虚,他们会理解他们所听到的字面上的意思,和冲突是不可避免的。
如果李的话变成:“贵国的菜肴真是好极了,今天能有机会借花献佛,真是荣幸之至......”然后每个人都会很高兴。
1.Formulaic polite expression2.Literary meaning is inappropriate convey the meaning of respectnguage form is different from content in eastern culture4.Happen in USA highly valued individualism express themselves directly5.Easterner’s over-modesty comprehend what they hear literally6.clash is inevitablepany how capable you are8.Reaction of restaurant’s boss is normal9.The cuisine is great my great honor to entertain you with themU3Case1 An invitation to dinnerBart Rapson带着他的家人到菲律宾为一个跨国企业的工作任务。
虽然他们自己并不是特别信教,但在圣诞节和复活节期间,除了天主教的象征外,他们基本上放弃了天主教的做法,但他们仍然觉得他们的孩子可能会从属于教会的利益中受益。
C语⾔:case详解C语⾔虽然没有限制 if else 能够处理的分⽀数量,但当分⽀过多时,⽤ if else 处理会不太⽅便,⽽且容易出现 if else 配对出错的情况。
例如,输⼊⼀个整数,输出该整数对应的星期⼏的英⽂表⽰:1. #include <stdio.h>2. int main(){3. int a;4. printf("Input integer number:");5. scanf("%d",&a);6. if(a==1){7. printf("Monday\n");8. }else if(a==2){9. printf("Tuesday\n");10. }else if(a==3){11. printf("Wednesday\n");12. }else if(a==4){13. printf("Thursday\n");14. }else if(a==5){15. printf("Friday\n");16. }else if(a==6){17. printf("Saturday\n");18. }else if(a==7){19. printf("Sunday\n");20. }else{21. printf("error\n");22. }23. return 0;24. }运⾏结果:Input integer number:3↙Wednesday对于这种情况,实际开发中⼀般使⽤ switch 语句代替,请看下⾯的代码:1. #include <stdio.h>2. int main(){3. int a;4. printf("Input integer number:");5. scanf("%d",&a);6. switch(a){7. case 1: printf("Monday\n"); break;8. case 2: printf("Tuesday\n"); break;9. case 3: printf("Wednesday\n"); break;10. case 4: printf("Thursday\n"); break;11. case 5: printf("Friday\n"); break;12. case 6: printf("Saturday\n"); break;13. case 7: printf("Sunday\n"); break;14. default:printf("error\n"); break;15. }16. return 0;17. }运⾏结果:Input integer number:4↙Thursdayswitch 是另外⼀种选择结构的语句,⽤来代替简单的、拥有多个分枝的 if else 语句,基本格式如下:switch(表达式){case 整型数值1: 语句 1;case 整型数值2: 语句 2;......case 整型数值n: 语句 n;default: 语句 n+1;}它的执⾏过程是:1) ⾸先计算“表达式”的值,假设为 m。
Creating a Competitive Strategy for a Discount Retailer●Step 1: Actively listen to the caseYour client is the largest discount retailer in Canada, with 500 stores spread throughout the country. Let's call it CanadaCo. For several years running, CanadaCo has surpassed the second-largest Canadian retailer (300 stores) in both relative market share and profitability. However, the largest discount retailer in the United States, USCo, has just bought out CanadaCo's competition and is planning to convert all 300 stores to USCo stores. The CEO of CanadaCo is quite perturbed by this turn of events, and asks you the following questions: Should I be worried? How should I react? How would you advise the CEO?●Step 2: Establish understanding of the caseSo, the client, CanadaCo, is facing competition in Canada from a United States competitor. Our task is to evaluate the extent of the threat and advise the client on a strategy. Before I can advise the CEO I need some more information about the situation. First of all, I'm not sure I understand what a discount retailer is!A discount retailer sells a large variety of consumer goods at discounted prices, generally carrying everything from housewares and appliances to clothing. Kmart, Woolworth, and Wal-Mart are prime examples in the United States.●Step 3: Set up the frameworkOh, I see. Then I think it makes sense to structure the problem this way: First, let's understand the competition in the Canadian market and how CanadaCo has become the market leader. Then let's look at the United States to understand how USCo has achieved its position. At the end, we can merge the two discussions to understand whether USCo's strength in the United States is transferable to the Canadian market.That sounds fine. Let's start, then, with the Canadian discount retail market. What would you like to know?●Step 4: Evaluate the case using the frameworkAre CanadaCo's 500 stores close to the competition's 300 stores, or do they serve different geographic areas?The stores are located in similar geographic regions. In fact, you might even see a CanadaCo store on one corner, and the competition on the very next corner.Do CanadaCo and the competition sell a similar product mix?Yes. CanadaCo's stores tend to have a wider variety of brand names, but by and large, the product mix is similar.Are CanadaCo's prices significantly lower than the competition's?No. For certain items CanadaCo is less expensive, and for others the competition is less expensive, but the average price level is similar.Is CanadaCo more profitable just because it has more stores, or does it have higher profits per store?It actually has higher profits than the competition on a per-store basis.Well, higher profits could be the result of lower costs or higher revenues. Are the higher per-store profits due to lower costs than the competition's or the result of higher per-store sales?CanadaCo's cost structure isn't any lower than the competition's. Its higher per-store profits are due to higher per-store sales.Is that because it has bigger stores?No. CanadaCo's average store size is approximately the same as that of the competition.If they're selling similar products at similar prices in similarly-sized stores in similar locations, why are CanadaCo's per-store sales higher than the competition's?It's your job to figure that out!Is CanadaCo better managed than the competition?I don't know that CanadaCo as a company is necessarily better managed, but I can tell you that its management model for individual stores is significantly different.How so?The competitor's stores are centrally owned by the company, while CanadaCo uses a franchise model in which each individual store is owned and managed by a franchisee who has invested in the store and retains part of the profit.In that case, I would guess that the CanadaCo stores are probably better managed, since the individual storeowners have a greater incentive to maximize profit.You are exactly right. It turns out that CanadaCo's higher sales are due primarily to a significantly higher level of customer service. The stores are cleaner, more attractive, better stocked, and so on. The company discovered this through a series of customer surveys last year. I think you've sufficiently covered the Canadian market—let's move now to a discussion of the United States market.How many stores does USCo own in the United States, and how many does the second-largest discount retailer own?USCo owns 4,000 stores and the second-largest competitor owns approximately 1,000 stores.Are USCo stores bigger than those of the typical discount retailer in the United States?Yes. USCo stores average 200,000 square feet, whereas the typical discount retail store is approximately 100,000 square feet.Those numbers suggest that USCo should be selling roughly eight times the volume of the nearest United States competitor!Close. USCo's sales are approximately $5 billion, whereas the nearest competitor sells about $1 billion worth of merchandise.I would think that sales of that size give USCo significant clout with suppliers. Does it have a lower cost of goods than the competition?In fact, its cost of goods is approximately 15 percent less than that of the competition.So it probably has lower prices.Right again. Its prices are on average about ten percent lower than those of the competition.So it seems that USCo has been so successful primarily because it has lower prices than its competitors.That's partly right. Its success probably also has something to do with a larger selection of products, given the larger average store size.How did USCo get so much bigger than the competition?It started by building superstores in rural markets served mainly by mom-and-pop stores and small discount retailers. USCo bet that people would be willing to buy from it, and it was right. As it grew and developed more clout with suppliers, it began to buy out other discount retailers and convert their stores to the USCo format.So whenever USCo buys out a competing store, it also physically expands it?Not necessarily. Sometimes it does, but when I said it converts it to the USCo format, I meant that it carries the same brands at prices that are on average ten percent lower than the competition's.What criteria does USCo use in deciding whether it should physically expand a store it's just bought out?It depends on a lot of factors, such as the size of the existing store, local market competition, local real estate costs, and so on, but I don't think we need to go into that here.Well, I thought it might be relevant in terms of predicting what it will do with the 300 stores that it bought in Canada.Let's just assume that it doesn't plan to expand the Canadian stores beyond their current size.OK. I think I've learned enough about USCo. I'd like to ask a few questions about USCo's ability to succeed in the Canadian market. Does USCo have a strong brand name in Canada?No. Although members of the Canadian business community are certainly familiar with the company because of its United States success, the Canadian consumer is basically unaware of USCo's existence.Does CanadaCo carry products similar to USCo's, or does the Canadian consumer expect different products and brands than the United States discount retail consumer?The two companies carry similar products, although the CanadaCo stores lean more heavily toward Canadian suppliers.How much volume does CanadaCo actually sell?About $750 million worth of goods annually.Is there any reason to think that the costs of doing business for USCo will be higher in the Canadian market?Can you be more specific?I mean, for example, are labor or leasing costs higher in Canada than in the United States?Canada does have significantly higher labor costs, and I'm not sure about the costs of leasing space. What are you driving at?I was thinking that if there were a higher cost of doing business in Canada, perhaps USCo would have to charge higher prices than it does in the United States to cover its costs.That's probably true, but remember, CanadaCo must also cope with the same high labor costs. Can you think of additional costs incurred by USCo's Canadian operations that would not be incurred by CanadaCo?USCo might incur higher distribution costs than CanadaCo because it will have to ship product from its United States warehouses up to Canada.You are partially right. CanadaCo has the advantage in distribution costs, since its network spans less geographic area and it gets more products from Canadian suppliers. However, since CanadaCo continues to get a good deal of product from the United States, the actual advantage to CanadaCo is not great—only about two percent of overall costs.All this suggests that USCo will be able to retain a significant price advantage over CanadaCo's stores: if not ten percent, then at least seven to eight percent.I would agree with that conclusion.Step 5: Summarize and make recommendationsI would tell the CEO the following: In the near term, you might be safe. Your stores have a much stronger brand name in Canada than USCo's, and they seem to be well managed. However, as consumers get used to seeing prices that are consistently seven to eight percent less at USCo, they will realize that shopping at USCo means significant savings over the course of the year.Although some consumers will remain loyal out of habit or because of your high level of service, it is reasonable to expect the discount shopper to shop where prices are lowest. Moreover, over time your brand-name advantage will erode as USCo becomes more familiar to Canadian consumers. You certainly have to worry about losing significant share to USCo stores in the long term. You should probably do something about it now, before it's too late.Can you suggest possible strategies for CanadaCo?Maybe it can find ways to cut costs and make the organization more efficient, so it can keep prices low even if its cost of goods is higher.Anything else?It might consider instituting something like a frequent shopper program, where consumers accumulate points that entitle them to future discounts on merchandise.What might be a potential problem with that?Well, it might not be that cost-effective, since it would be rewarding a significant number of shoppers who would have continued to shop there anyway.Any other suggestions?CanadaCo might want to prepare a marketing or advertising campaign that highlights its high level of service. It might even institute a CanadaCo Service Guarantee that surpasses any guarantees offered by USCo.Assuming the only way to keep customers is through competitive pricing, is there anything CanadaCo can do to appear competitive to the consumer?It might want to consider offering fewer product lines, so that it can consolidate its buying power and negotiate prices with suppliers that are competitive with USCo's. It might lose some customers who want the variety of products that USCo has, but it may be able to retain the customer who is buying a limited array of items and is just looking for the best price.All of your suggestions are interesting, and you would want to analyze the advantages and disadvantages of each in more detail before making any recommendations to the CEO.。
习题 2及其解答2.1选择题1.已知 int i=0, x=1, y=0 ; 在下列选项使i 的值变成1的语句是( c )。
(a) if( x&&y ) i++ ; (b) if( x==y ) i++ ; (c) if( x||y ) i++ ; (d) if( !x ) i++ ;2.设有函数关系为y=⎪⎩⎪⎨⎧>=<-010001x x x ,下面选项中能正确表示上述关系为( c )。
(a) y = 1 ; (b) y = -1 ;if( x >= 0 ) if( x != 0 )if( x == 0 ) y = 0 ; if( x > 0 ) y = 1 ; else y = -1; else y = 0; (c) if( x <= 0 ) (d) y = -1 ; if( x < 0 ) y = -1 ; if( x <= 0 )else y = 0 ; if( x < 0 ) y = -1 ; else y = 1 ; else y = 1 ; 3.假设i=2,执行下列语句后i 的值为( b )。
switch( i ) { case 1 : i ++ ; case 2 : i -- ; case 3 : ++ i ; break ; case 4 : -- i ; default : i ++ ; } (a) 1(b) 2(c) 3(d) 44.已知int i=0,x=0; 下面while 语句执行时循环次数为( d )。
while( !x && i< 3 ) { x++ ; i++ ; } (a) 4(b) 3(c) 2(d) 15.已知int i=3;下面do_while 语句执行时循环次数为( b )。
do{ i--; cout<<i<<endl;}while ( i!= 1 ); (a) 1(b) 2(c) 3(d) 无限6.下面for 语句执行时循环次数为( b )。
C语言程序设计课后习题1-8参考答案习题1参考答案一、简答题1、冯诺依曼计算机模型有哪几个基本组成部分各部分的主要功能是什么?答:冯诺依曼计算机模型是由运算器、控制器、存储器、输入设备、输出设备五大功能部件组成的。
运算器又称算术逻辑部件,简称ALU,是计算机用来进行数据运算的部件。
数据运算包括算术运算和逻辑运算。
控制器是计算机的指挥系统,计算机就是在控制器的控制下有条不紊地协调工作的。
存储器是计算机中具有记忆能力的部件,用来存放程序和数据。
输入设备是用来输入程序和数据的部件。
输出设备正好与输入设备相反,是用来输出结果的部件。
2、简述计算机的工作原理。
答:计算机的工作原理可简单地概括为:各种各样的信息,通过输入设备,进入计算机的存储器,然后送到运算器,运算完毕把结果送到存储器存储,最后通过输出设备显示出来。
整个过程由控制器进行控制。
3、计算机软件系统分为哪几类答:软件内容丰富,种类繁多,通常根据软件用途将其分为两大类:系统软件和应用软件。
系统软件是指管理、监控、维护计算机正常工作和供用户操作使用计算机的软件。
这类软件一般与具体应用无关,是在系统一级上提供的服务。
系统软件主要包括以下两类:一类是面向计算机本身的软件,如操作系统、诊断程序等。
另一类是面向用户的软件,如各种语言处理程序(像BC、VC等)、实用程序、字处理程序等。
应用程序是指某特定领域中的某种具体应用,供最终用户使用的软件,它必须在操作系统的基础上运行。
4、什么叫软件说明软件与硬件之间的相互关系。
答:软件是指计算机程序及有关程序的技术文档资料。
两者中更为重要的是程序,它是计算机进行数据处理的指令集,也是计算机正常工作最重要的因素。
在不太严格的情况下,认为程序就是软件。
硬件与软件是相互依存的,软件依赖于硬件的物质条件,而硬件则需在软件支配下才能有效地工作。
在现代,软件技术变得越来越重要,有了软件,用户面对的将不再是物理计算机,而是一台抽象的逻辑计算机,人们可以不必了解计算机本身,可以采用更加方便、更加有效地手段使用计算机。
[案例4]
S公司8月12日向其客户A公司寄出一份商品目录,介绍了S公司经营的各式男女手套,并附有精美的图片。
8月20日A公司回电表示对其中的货号为308A、309B、311B的女式手套很感兴趣,每个货号订购100打,并要求大、中号各半,10月份交货,请S公司报价。
8月22日S公司发盘如下:报青字牌女式羊毛手套300打,货号308A、309B、311B各100打,大、中号各半,每双CIFI旧金山12美元,纸箱装,10月份装运,即期不可撤销信用证支付,8月30日复到有效。
8月28日A公司回电:你8月22日电悉。
价格过高,每双CIFI旧金山10美元可接受。
次日S公司去电:你28日电悉。
最低价每双CIFI旧金山11美元,9月5日复到有效。
9月3日S公司收到A公司的电开信用证,其中单价为每双11美元,包装条款中注明纸箱装,每箱15打,其它与发盘相符。
S公司审证时发现了A公司对包装条款所作的添加。
S公司的习惯包装是每箱10打,考虑到交货期临近,若提请修改,恐怕难以按时交货,另外,即使按信用证要求包装,也不会增加费用。
但到9月20日,储运部门通报,公司库存中没有可装15打手套的纸箱,现有纸箱一种为可装10打的习惯包装,另一种可装20打。
S公司随即与纸箱厂联系,纸箱很少见,该厂不能供应。
附近的几个纸箱厂也如此答复。
在此情况下,S公司一面四处落买箱源,一面于9月10日去电A公司,表示包装条款不能接受,要求改为每箱装10打或20打。
分析:根据《联合国国际货物销售合同公约》的规定,上述对发价中包装条款的修改属非实质性修改,由于s公司未在不过迟延的时间内向被发价人通知反对意见,则接受有效,据此成立的合同就应以发价内容及附有非实质性修改内容的接受为准,所以纸箱装,每箱15打的包装条款已达成。
【分析】
A国商人将从别国进口的初级产品转卖,向B国商人发盘,B国商人复电,接受发盘,同时要求提供产地证。
两周后,A国商人收到B国商人开来的信用证,正准备按信用证规定发运货物,获商检机构通知,因该货非本国产品,不能签发产地证。
经电请B国商人取消信用证中要求提供产地证的条款,遭到拒绝,于是引起争议。
A国商人提出,其对提供产地证的要求从未表示同意,依法无此义务,而B国商人坚持A国商人有此义务。
请根据《联合国国际货物销售合同公约》(双方所在国均为缔约国)的规定,对此案作出裁决。
(1)A国与B国均为《联合国国际货物销售合同公约》缔约国,由于双方对《公约》的适用未作排除和保留,本案应按《公约》规定办理。
(2)A商在收到B商对其发盘作出附加条件的接受时,未提出任何异议,即接受有效,A商应负有提供产地证的义务。
(3)B商已根据其接受条件开立信用证,A商接受信用证后,又未提出异议,并准备履行信用证的规定交货,后因商检机构不能出证,这与B商无关,构成A商违约。
我出口企业对意大利某商人发盘限10日复到有效,9日意商人用电报通知我方接受该发盘,由于电报局传递延误,我方于11日上午才收到对方的接受通知,而我方在收到接受通前获悉市场价格已上涨,对此,我方应如何处理?
分析:中国与意大利均系《联合国国际货物销售合同公约》缔约国,该案双方洽谈过程中,均未排除或作出任何保留,因此,双方当事人均应受《公约》约束。
按《公约》规定,如果载有逾期接受的信件或其它书面档表明,它是在传递正常能及时传达到发价人的情况下寄发的,则该项逾期接受具有接受效力,除非发盘人毫不迟延地用口头或书面通知被发盘人,他认为发盘已经失效,据此,我方于11月收到意商的接交电报属因传递延误而造成的逾期接受。
因此,如我方不能同意此项交易,应即复电通知对方:我方原发盘已经失效。
如我方鉴于其它原因,愿按原发盘达成交易,订立合同,可回电确信,也可不予答复,予以默认。