C语言程序设计教程 课后习题参考答案 下载本文

内容发布更新时间 : 2024/11/1 7:31:03星期一 下面是文章的全部内容请认真阅读。

for( row = 1; row <= n; ++row) { for( spaceCount = 1; spaceCount <= n - row ; ++ spaceCount) printf(\ //打印出某行上星号前的空格 for( starCount = 1; starCount <= 2* row - 1; ++starCount ) printf(\ //打印出某行上的所有星号 printf(\换行 }

//打印下半部分

for(row=1; row

#include main( ) {

int n;

int g,s,b,q; int t;

scanf(\ g = n % 10; s = n / 10 % 10; b = n /100 % 10; q = n/ 1000;

g = (g+5); s = (s+5); b = (b+5); q = (q+5);

//第1位和第4位交换 t = g; g = q; q = t;

//第2位和第3位交换

t = s; s = b; b = t;

printf(\}

第6章 1.

(1)无返回值 (2)double 2

(3)无限循环(死循环) (4)result = 720 2. (1)

#include void print1( int n );

main( ) {

int n;

scanf(\ print1( n ); }

void print1( int n ) {

int row, col;

for(row = 1; row<=n; row++) { for(col=1; col<=row; col++) printf(\ printf(\ } }

(2)

#include void print2( int n );

main( ) {

int n;

scanf(\ print2( n ); }

void print2( int n ) {

int row, col;

for(row = 1; row<=n; row++) { for(col=1; col<=2*row-1; col++) printf(\ printf(\ } }

(3)

#include

int yearOld(int byear, int bmonth, int bday, int nyear, int nmonth, int nday); main( ) {

int nowy,nowm,nowd; int by, bm, bd; int age;

printf(\请输入生日:\

scanf(\ printf(\请输入现在日期:\

scanf(\ age= yearOld(by,bm,bd,nowy,nowm,nowd); printf(\}

int yearOld(int byear, int bmonth, int bday, int nyear, int nmonth, int nday) {

int age;

age = nyear - byear; if( nmonth

else if( nmonth == bmonth && nday>bday) age--; return age; }

(4)

#include int sum( int n ); main( )

{

int n,s;

scanf(\ s = sum( n );

printf(\}

int sum( int n ) {

int s=0; while ( n ) { s += n % 10; n /= 10; }

return s; }

(5)

#include double sumfac( int n ); main( ) {

int n;

scanf(\

printf(\}

double sumfac( int n ) {

double f=1.0, s = 0.0;

int i;

for(i=1;i<=n; i++) { f *= i; s += f; }

return f; }

(6)

#include int gcd(int m , int n); main( )

{

int m, n;

scanf(\ printf(\}

int gcd(int m, int n) {

int t,r; if( m < n )

{ t = m; m= n; n = t; }

r = m % n; while( r ) { m = n; n = r; r = m % n; }

return n; }

(7)

#include int gcd(int m , int n); int lcm(int m, int n); main( ) {

int m, n;

scanf(\ printf(\}

int gcd(int m, int n) {

int t,r; if( m < n )

{ t = m; m= n; n = t; }

r = m % n; while( r ) { m = n; n = r; r = m % n; }