|
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
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
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
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
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
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
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
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
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
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 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 int a[N]; int i,j,temp,flag; printf(\输入待排序数据:\\n\ for(i=0;i scanf(\ for(i=0;i flag=0; for(j=0;j temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; flag=1; }