利用递归求两个整数的最小公倍数:
#include <stdio.h>
int main ( ) {
int m, n, r;
scanf ( "%d,%d" , & m, & n) ;
int p= m* n;
while ( m% n!= 0 ) {
r= m% n;
m= n;
n= r;
}
printf ( "最大公约数:%d" , n) ;
printf ( "最小公倍数:%d" , p/ n) ;
return 0 ;
}
二元一次方程:
#include <stdio.h>
#include <math.h>
int main ( )
{
double a, b, c, d, x1, x2;
scanf ( "%lf,%lf,%lf" , & a, & b, & c) ;
d = b * b - 4 * a * c;
if ( d > 0 ) {
x1 = ( ( - 1 ) * b + sqrt ( d) ) / ( 2 * a) ;
x2 = ( ( - 1 ) * b - sqrt ( d) ) / ( 2 * a) ;
printf ( "x1=%.4f, x2=%.4f\n" , x1, x2) ;
}
else if ( d == 0 ) {
x1 = x2 = ( - 1 ) * b / ( 2 * a) ;
printf ( "x1=x2=%.4f\n" , x1) ;
}
else
printf ( "This equation is unsolvable!\n" ) ;
return 0 ;
}
大整数:
#include <stdio.h>
#include <string.h>
#define N 20
int add ( int a1[ ] , int b1[ ] , int c1[ ] , int nc1) ;
int del ( int a2[ ] , int b2[ ] , int c2[ ] , int nc2) ;
int main ( )
{
int a[ N] , b[ N] , c[ N] ;
char s1[ N] , s2[ N] ;
int l1, l2, na, nb, nc;
memset ( a, 0 , sizeof ( a) ) ;
memset ( b, 0 , sizeof ( b) ) ;
memset ( c, 0 , sizeof ( c) ) ;
scanf ( "%s%s" , s1, s2) ;
l1= strlen ( s1) ;
l2= strlen ( s2) ;
for ( int i= 0 ; i< l1; i++ )
a[ i] = s1[ l1- i- 1 ] - '0' ;
na = l1;
for ( i= 0 ; i< l2; i++ )
b[ i] = s2[ l2- i- 1 ] - '0' ;
nb= l2;
nc= na> nb? na: nb ;
printf ( "相加结果:" ) ;
add ( a, b, c, nc) ;
memset ( c, 0 , sizeof ( c) ) ;
printf ( "相减结果:" ) ;
if ( na< nb || ( ( na= nb) && ( a[ nc- 1 ] < b[ nc- 1 ] ) ) )
{ printf ( "-" ) ;
del ( b, a, c, nc) ;
}
else
del ( a, b, c, nc) ;
return 0 ;
}
int add ( int a[ ] , int b[ ] , int c[ ] , int nc) {
for ( int i= 0 ; i< nc; i++ ) {
c[ i] = c[ i] + a[ i] + b[ i] ;
c[ i+ 1 ] + = c[ i] / 10 ;
c[ i] = c[ i] % 10 ;
}
nc++ ;
while ( c[ nc- 1 ] == 0 ) {
if ( nc == 1 ) break ;
nc-- ;
}
for ( i= nc- 1 ; i>= 0 ; i-- )
printf ( "%d" , c[ i] ) ;
printf ( "\n" ) ;
return 0 ;
}
int del ( int a[ ] , int b[ ] , int c[ ] , int nc) {
for ( int i= 0 ; i< nc; i++ ) {
c[ i] = c[ i] + a[ i] - b[ i] ;
if ( c[ i] < 0 ) {
c[ i] = c[ i] + 10 ;
c[ i+ 1 ] = c[ i+ 1 ] - 1 ;
}
}
while ( c[ nc- 1 ] == 0 ) {
if ( nc == 1 ) break ;
nc-- ;
}
for ( i= nc- 1 ; i>= 0 ; i-- )
printf ( "%d" , c[ i] ) ;
printf ( "\n" ) ;
return 0 ;
}
矩阵乘法:
#include <stdio.h>
#include <string.h>
#define N 2
int main ( ) {
int a[ N] [ N] , b[ N] [ N] ;
int l;
memset ( b, 0 , sizeof ( b) ) ;
printf ( "请输入A矩阵:" ) ;
for ( int i= 0 ; i< N; i++ ) {
for ( int j= 0 ; j< N; j++ ) {
scanf ( "%d" , & a[ i] [ j] ) ;
}
}
for ( i= 0 ; i< N; i++ ) {
for ( int j= 0 ; j< N; j++ ) {
b[ i] [ j] + = a[ i] [ j] * a[ j] [ i] ;
}
}
for ( i= 0 ; i< N; i++ ) {
for ( int j= 0 ; j< N; j++ ) {
printf ( "%d\a" , b[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
}
学生成绩:
#include <stdio.h>
#define N 3
struct stu{
int num;
char name[ 32 ] ;
double score[ 3 ] ;
double ave;
} ;
void input ( struct stu * p, int n) ;
void sort ( struct stu * p, int n) ;
void show ( struct stu * p, int n) ;
int main ( ) {
struct stu a[ N] ;
input ( a, N) ;
sort ( a, N) ;
show ( a, N) ;
return 0 ;
}
void input ( struct stu * p, int n) {
for ( int i= 0 ; i< n; i++ ) {
printf ( "请输入学生%d:\n" , i+ 1 ) ;
printf ( "name: " ) ;
scanf ( "%s" , p[ i] . name) ;
printf ( "请输入成绩:英语、数学、语文 \n" ) ;
for ( int j= 0 ; j< 3 ; j++ ) {
printf ( "score %d:" , j+ 1 ) ;
scanf ( "%lf" , & p[ i] . score[ j] ) ;
}
printf ( "\n" ) ;
}
}
void sort ( struct stu * p, int n) {
int i, j;
double k;
for ( i= 0 ; i< n; i++ ) {
k= p[ i] . score[ 0 ] + p[ i] . score[ 1 ] + p[ i] . score[ 2 ] ;
p[ i] . ave = ( k) / 3 ;
}
for ( i= 0 ; i< n- 1 ; i++ )
for ( j= 0 ; j< n- i- 1 ; j++ ) {
if ( p[ j] . ave< p[ j+ 1 ] . ave) {
struct stu b = p[ j] ;
p[ j] = p[ j+ 1 ] ;
p[ j+ 1 ] = b;
}
}
}
void show ( struct stu * p, int n) {
printf ( "\n NO. \t name \t 平均成绩 \n" ) ;
for ( int i= 0 ; i< n; i++ ) {
printf ( "%d \t %s \t %0.2f\n" , i+ 1 , p[ i] . name, p[ i] . ave) ;
}
}
围圈报数-链表:
#include <stdio.h>
int main ( ) {
int i, m, n, a[ 50 ] ;
scanf ( "%d" , & n) ;
scanf ( "%d" , & m) ;
for ( i= 0 ; i<= n; i++ )
a[ i] = i;
int j= 1 , flag= 0 ;
int k= n;
while ( k!= 1 ) {
if ( j> n)
j= 1 ;
if ( a[ j] != 0 )
flag++ ;
if ( flag== m) {
a[ j] = 0 ;
flag= 0 ;
k-- ;
}
j++ ;
}
printf ( "**************\n" ) ;
for ( i= 0 ; i<= n; i++ ) {
if ( a[ i] != 0 ) {
puts ( "最后一位:" ) ;
printf ( "%d\n" , a[ i] ) ;
}
}
return 0 ;
}