谭浩强c语言第六章习题
# 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 ;
}
n= 0 ;
for ( i= 2 ; i<= 100 ; i++ )
{
if ( a[ i] != 0 )
{
printf ( "%4d" , a[ i] ) ;
n++ ;
}
if ( n% 10 == 0 )
printf ( "\n" ) ;
}
return 0 ;
}
# include <stdio.h>
# include <math.h>
int main ( )
{
int i, j, min, temp, a[ 10 ] ;
printf ( "请输入10个整数:\n" ) ;
for ( i= 0 ; i<= 9 ; i++ )
{
printf ( "a[%d]=" , i) ;
scanf ( "%d" , & a[ i] ) ;
}
for ( i= 0 ; i<= 9 ; i++ )
{
min = i;
for ( j= i+ 1 ; j<= 9 ; j++ )
{
if ( a[ min] >= a[ j] )
min = j;
temp = a[ i] ;
a[ i] = a[ min] ;
a[ min] = temp;
}
}
printf ( "数据排序为" ) ;
for ( i= 0 ; i<= 9 ; i++ )
{
printf ( "%4d" , a[ i] ) ;
}
return 0 ;
}
# include <stdio.h>
# include <math.h>
int main ( )
{
int i, j, sum= 0 , a[ 3 ] [ 3 ] ;
printf ( "请输入一个3*3的矩阵\n" ) ;
for ( i= 0 ; i<= 2 ; i++ )
for ( j= 0 ; j<= 2 ; j++ )
{
printf ( "a[%d][%d]=" , i, j) ;
scanf ( "%d" , & a[ i] [ j] ) ;
}
for ( i= 0 ; i<= 2 ; i++ )
for ( j= 0 ; j<= 2 ; j++ )
{
if ( i== j|| i== j+ 2 || j== i+ 2 )
sum+= a[ i] [ j] ;
}
printf ( "sum=%d" , sum) ;
return 0 ;
}
6.4 有一个已经排好序的数组,要求输入一个数后,按原来顺序的规律将它插入数组中
# include <stdio.h>
# include <math.h>
int main ( )
{
int i, j, number, n, temp, temp1, a[ 11 ] , numend;
printf ( "现在请按从小到大的顺序依次输入10个数:" , n) ;
for ( i= 0 ; i<= 9 ; i++ )
{
scanf ( "%d" , & a[ i] ) ;
}
printf ( "请输入一个整数number = " ) ;
scanf ( "%d" , & number) ;
numend = a[ 9 ] ;
if ( numend<= number)
a[ 10 ] = number;
else
{
for ( i= 0 ; i<= 9 ; i++ )
{
if ( a[ i] > number)
{
temp = a[ i] ;
a[ i] = number;
for ( j= i+ 1 ; j<= 10 ; j++ )
{
temp1 = a[ j] ;
a[ j] = temp;
temp = temp1;
}
break ;
}
}
}
printf ( "现在的顺序为:\n" ) ;
for ( i= 0 ; i<= 10 ; i++ )
{
printf ( "%3d" , a[ i] ) ;
}
return 0 ;
}
6.5 将一个数组中的值按逆序重新存放。例如:原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。
# include <stdio.h>
int main ( )
{
int i, j, N, temp;
printf ( "请输入少于10个整数个数N=:" ) ;
scanf ( "%d\n" , & N) ;
int a[ N] ;
for ( i= 0 ; i< N; i++ )
{
scanf ( "%d" , & a[ i] ) ;
}
for ( j= 0 ; j< N/ 2 ; j++ )
{
temp = a[ j] ;
a[ j] = a[ N- j- 1 ] ;
a[ N- j- 1 ] = temp;
}
for ( i= 0 ; i< N; i++ )
{
printf ( "%3d" , a[ i] ) ;
}
return 0 ;
}
# include <stdio.h>
# include <math.h>
int main ( )
{
int i, j, n= 10 , a[ 10 ] [ 10 ] ;
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] + a[ i- 1 ] [ j- 1 ] ;
}
for ( i= 0 ; i< n; i++ )
{
for ( j= 0 ; j<= i; j++ )
{
printf ( "%5d" , a[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
return 0 ;
}
6.8 找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小,也可能没有鞍点。
# include <stdio.h>
# include <math.h>
int main ( )
{
int i, j, k, m, n;
printf ( "请输入二维数组的行数m和列数n:" ) ;
scanf ( "%d%d" , & m, & n) ;
int a[ m] [ n] ;
printf ( "请输入二维数组:\n" ) ;
for ( i= 0 ; i< m; i++ )
for ( j= 0 ; j< n; j++ )
{
printf ( "a[%d][%d]=" , i, j) ;
scanf ( "%d" , & a[ i] [ j] ) ;
}
printf ( "输入的矩阵为:\n" ) ;
for ( i= 0 ; i< m; i++ )
{
for ( j= 0 ; j< n; j++ )
{
printf ( "%3d" , a[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
int max, maxj, temp;
for ( i= 0 ; i< m; i++ )
{
max = a[ i] [ 0 ] ;
maxj = 0 ;
for ( j= 0 ; j< n; j++ )
{
if ( a[ i] [ j] > max)
{
max = a[ i] [ j] ;
maxj = j;
}
}
temp = 1 ;
for ( k= 0 ; k< m; k++ )
{
if ( max> a[ k] [ maxj] )
{
temp = 0 ;
continue ;
}
}
if ( temp)
{
printf ( "a[%d][%d] = %d" , i, maxj, max) ;
break ;
}
}
if ( ! temp)
{
printf ( "不存在鞍点" ) ;
}
return 0 ;
}
6.9 有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出"无此数"。
# include <stdio.h>
# include <math.h>
int main ( )
{
int N= 15 ;
int i, a[ N] , m;
int low, high, mid, flag;
low = 0 ;
high = N- 1 ;
printf ( "请按从大到小的顺序输入15个数:\n" ) ;
for ( i= 0 ; i< N; i++ )
{
scanf ( "%d" , & a[ i] ) ;
}
printf ( "输入的15个数为:" ) ;
for ( i= 0 ; i< N; i++ )
{
printf ( "%3d" , a[ i] ) ;
}
printf ( "\n" ) ;
printf ( "请输入一个数m = " ) ;
scanf ( "%d" , & m) ;
while ( low<= high)
{
mid = ( low+ high) / 2 ;
if ( m== a[ mid] )
{
flag = 1 ;
break ;
}
else if ( m> a[ mid] )
{
high = mid- 1 ;
}
else
low = mid+ 1 ;
}
if ( flag)
printf ( "%d是数组中第%d个数.\n" , m, mid+ 1 ) ;
else if ( ! flag)
printf ( "无此数" ) ;
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 enter 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++ ;
else
oth++ ;
}
}
printf ( "\nnupper 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 ( )
{
int i, j;
for ( i = 0 ; i< 5 ; i++ )
{
for ( j= 0 ; j< i; j++ )
{
printf ( " " ) ;
}
printf ( "*****\n" ) ;
}
return 0 ;
}
# include <stdio.h>
int main ( )
{
char s[ 1024 ] = { 0 } ;
scanf ( "%s" , s) ;
int len = strlen ( s) ;
for ( int i = 0 ; i < len; ++ i)
{
if ( s[ i] >= 'a' && s[ i] <= 'z' )
s[ i] = 'a' + 26 - ( s[ i] - 'a' ) - 1 ;
else if ( s[ i] >= 'A' && s[ i] <= 'Z' )
s[ i] = 'A' + 26 - ( s[ i] - 'A' ) - 1 ;
}
printf ( "%s" , s) ;
return 0 ;
}