好文档 - 专业文书写作范文服务资料分享网站

程序设计基础——基于-C语言(第2版~)课后习题参考-答案~

天下 分享 时间: 加入收藏 我要投稿 点赞

|

printf(\ scanf(\ printf(\ disc=b*b-4*a*c;

if (fabs(disc)<=1e-7)

printf(\ else

if (disc>1e-7) {

x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a);

printf(\ } else {

realpart=-b/(2*a);

imagepart=sqrt(-disc)/(2*a); printf(\

printf(\

printf(\ } }

6. #include void main() {

int i,j;

for (i=1;i<=5;i++) {

for (j=1;j<=i;j++) printf(\ printf(\ }

for (i=5;i>=1;i--) {

for (j=i;j>=1;j--) printf(\ printf(\ } }

7.#include #define EPS 1e-7 void main() {

int n;

|

double x,an,sum; printf(\ scanf(\ x=x*3.1415926/180; sum=0; an=x; n=1; do {

sum+=an;

n=n*(n+1)*(n+2); an*=(-x*x)/n;

printf(\ }while(fabs(an)>=EPS);

printf(\}

8.#include void main() {

unsigned int n; char c; n=0;

printf(\ while((c=getchar())!='\\n') {

switch (c) {

case '0': n=n*2+0; break; case '1': n=n*2+1; break; } }

printf(\}

9.#include void main() {

int i,j;

for (i=1;i<10;i++) printf(\ printf(\

|

for (i=1;i<10;i++) {

printf(\ for (j=1;j<=i;j++)

printf(\ printf(\ } }

10.#include void main() {

int n,m,z,x,y;

printf(\ scanf(\ m=n; z=x=y=1; while (n>1) {

z=x+y; x=y; y=z; n--; }

printf(\}

11.#include #include void main() {

int i,j; int flag;

for (i=1;i<=100;i++) {

flag=1;

for (j=2;j<=sqrt(i);j++) if (i%j==0) {

flag=0; break; }

if (flag==1)

printf(\ } }

|

12.#include void main() {

int i,sum; sum=0;

for (i=1;i<=1000;i++)

if (i%3==0 && i%5==0 && i%7==0) sum+=i;

printf(\}

13.#include void main() {

int x; long f;

printf(\ scanf(\ if (x>=1 && x<=3) f=2*x+3;

else if (x>=4 && x<=10) f=3*(x+4); else

f=x*x+3*x-6;

printf(\}

习题6参考答案

6.1 &b[i][j]=3001+i*16+j*4;

6.2求一维数组各元素的最大值、最小值及所有元素的乘积 #include #define N 10 void main( ) {

int a[N];

int max,min,i,product;

printf(\输入%d个数组元素:\\n\ for(i=0;i

scanf(\ max=a[0]; min=a[0]; product=a[0]; for(i=1;i

product*=a[i];

|

if(maxa[i])min=a[i]; }

printf(\各元素最大值为:%d\\n\ printf(\各元素最小值为:%d\\n\ printf(\各元素的乘积为:%d\\n\}

6.3已知数列a0=0, a1=1,an= an-2+ an-2an-1,求数列的前10个元素 #include #define N 10 void main( ) {

int a[N]={0,1}; int i;

for(i=2;i

a[i]=a[i-2]+a[i-2]*a[i-1]; for(i=0;i

if(i%5==0)printf(\ printf(\ }

printf(\}

6.4用改进的冒泡算法对N个数由小到大排序 #include #define N 10 void main( ) {

int a[N];

int i,j,temp,flag;

printf(\输入待排序数据:\\n\ for(i=0;i

scanf(\ for(i=0;i

flag=0;

for(j=0;ja[j+1]) {

temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; flag=1; }

程序设计基础——基于-C语言(第2版~)课后习题参考-答案~

|printf(\scanf(\printf(\disc=b*b-4*a*c;if(fabs(disc)<=1e-7)printf(\elseif(disc>1e-7){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);
推荐度:
点击下载文档文档为doc格式
6nob56hfw73z01x0bvw21wxgu8k84a00ngh
领取福利

微信扫码领取福利

微信扫码分享