生活随笔
收集整理的這篇文章主要介紹了
C语言题目练习100例——题目+题目分析+源代码(91—100)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【題目91】
題目:在屏幕中輸入一個數(shù)字,編程求出其平方根。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# include "math.h" int main ( )
{ double a
, x0
, x1
; printf ( "Please input a number: \n" ) ; scanf ( "%lf" , & a
) ; if ( a
< 0 ) { printf ( "Error! \n" ) ; } else { x0
= a
/ 2 ; x1
= ( x0
+ a
/ x0
) / 2 ; do { x0
= x1
; x1
= ( x0
+ a
/ x0
) / 2 ; } while ( fabs ( x0
- x1
) >= 1e-6 ) ; } printf ( "Result: \n" ) ; printf ( "sqrt(%g)=%g \n" , a
, x1
) ; getchar ( ) ; return 0 ;
}
【題目92】
題目:用計算機隨機生成1~100的一個數(shù)字,然后由用戶來猜這個數(shù),根據(jù)用戶猜測的次數(shù)分別給出不同的提示。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# include "time.h"
# include "stdlib.h" int main ( )
{ int n
, m
, i
= 0 ; srand ( time ( NULL ) ) ; n
= rand ( ) % 100 + 1 ; do { printf ( "輸入你猜的數(shù)字: " ) ; scanf ( "%d" , & m
) ; i
++ ; if ( m
> n
) printf ( "錯誤!數(shù)太大了! \n" ) ; else if ( m
< n
) printf ( "錯誤!數(shù)太小了! \n" ) ; } while ( m
!= n
) ; printf ( "回答正確! \n" ) ; printf ( "共猜測了%d次。 \n" ) ; if ( i
<= 5 ) { printf ( "你太聰明了,這么快就猜出了! " ) ; } else if ( i
> 5 ) { printf ( "還需改進方法,以便更快猜出來!" ) ; } getchar ( ) ; return 0 ;
}
【題目93】
題目:由用戶輸入骰子數(shù)量和參賽人數(shù),然后由計算機隨機生成每一粒骰子的點數(shù),再累加得到每一個選手的總點數(shù)。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# include "time.h"
# include "stdlib.h" void play ( int n
)
{ int i
, m
= 0 , t
= 0 ; for ( i
= 0 ; i
< n
; i
++ ) { t
= rand ( ) % 6 + 1 ; m
+= t
; printf ( "\t第%d粒:%d; \n" , i
+ 1 , t
) ; } printf ( "\t總點數(shù)為:%d \n" , m
) ;
}
int main ( )
{ int c
; int n
; int i
, m
; do { srand ( time ( NULL ) ) ; printf ( "設(shè)置骰子數(shù)量(輸入0則退出):" ) ; scanf ( "%d" , & n
) ; if ( n
== 0 ) break ; printf ( "\n輸入本輪參賽人數(shù)(輸入0則退出): " ) ; scanf ( "%d" , & c
) ; if ( c
== 0 ) break ; for ( i
= 0 ; i
< c
; i
++ ) { printf ( "\n第%d位選手?jǐn)S出的骰子為: \n" , i
+ 1 ) ; play ( n
) ; } printf ( "\n" ) ; } while ( 1 ) ; return 0 ;
}
【題目94】
題目:創(chuàng)建一個鏈表。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# include "stdlib.h"
# include "malloc.h" typedef struct LNode { int data
; struct LNode * next
;
} LNode
, * LinkList
; LinkList
CreateList ( int n
) ;
void print ( LinkList h
) ; int main ( )
{ LinkList Head
= NULL ; int n
; scanf ( "%d" , & n
) ; Head
= CreateList ( n
) ; printf ( "剛剛建立的各個鏈表元素的值為:\n" ) ; print ( Head
) ; printf ( "\n\n" ) ; system ( "pause" ) ; return 0 ;
}
LinkList
CreateList ( int n
)
{ LinkList L
, p
, q
; int i
; L
= ( LNode
* ) malloc ( sizeof ( LNode
) ) ; if ( ! L
) return 0 ; L
-> next
= NULL ; q
= L
; for ( i
= 1 ; i
<= n
; i
++ ) { p
= ( LinkList
) malloc ( sizeof ( LNode
) ) ; printf ( "請輸入第%d個元素的值:" , i
) ; scanf ( "%d" , & ( p
-> data
) ) ; p
-> next
= NULL ; q
-> next
= p
; q
= p
; } return L
;
}
void print ( LinkList h
)
{ LinkList p
= h
-> next
; while ( p
!= NULL ) { printf ( "%d " , p
-> data
) ; p
= p
-> next
; }
}
【題目95】
題目:動態(tài)分配13個整型儲存區(qū)域,然后賦值并輸出 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# include "stdlib.h" int main ( )
{ int count
, * array
; if ( ( array
= ( int * ) malloc ( 13 * sizeof ( int ) ) ) == NULL ) { printf ( "Cannot succeed the assignment storage space. " ) ; exit ( 1 ) ; } for ( count
= 0 ; count
< 13 ; count
++ ) { array
[ count
] = count
; } for ( count
= 0 ; count
< 13 ; count
++ ) { printf ( "%4d" , array
[ count
] ) ; } return 0 ;
}
【題目96】
題目:通過malloc函數(shù)分配一個大的內(nèi)存,然后再分配一個小的內(nèi)存并查看是否分配成功,如果不成功則使用free來釋放。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# include "stdlib.h" int main ( )
{ long * buf1
, * buf2
; long size
= 13000 * sizeof ( long ) ; buf1
= ( long * ) malloc ( size
) ; if ( buf1
!= NULL ) { printf ( "\n Allocation of %ld bytes successful. \n" , size
) ; } else { printf ( "\n Attempt to allocate %ld bytes failed. \n" , size
) ; exit ( 1 ) ; } buf2
= ( long * ) malloc ( size
) ; if ( buf2
!= NULL ) { printf ( "\n Second allocation of %ld bytes successful. \n" , size
) ; exit ( 0 ) ; } else { printf ( "\n Second attempt to allocate %ld bytes failed. \n" , size
) ; } free ( buf1
) ; printf ( "\n Freeing first block. \n" ) ; if ( ( buf2
= ( long * ) malloc ( size
) ) != NULL ) { printf ( "\n After free(),allocation of %ld bytes successful. \n" , size
) ; } return 0 ;
}
【題目97】
題目:計算學(xué)生的平均成績和不及格的人數(shù) 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
struct stu { int num
; char * name
; char sex
; float score
;
} boy
[ 3 ] = { { 101 , "Li ping" , 'F' , 45 } , { 102 , "zhang san" , 'M' , 122 } , { 101 , "Wang er" , 'M' , 80 } ,
} ;
int main ( )
{ int i
, c
= 0 ; float ave
, s
= 0 ; for ( i
= 0 ; i
< 3 ; i
++ ) { s
+= boy
[ i
] . score
; if ( boy
[ i
] . score
< 60 ) c
+= 1 ; } printf ( "s=%f \n" , s
) ; ave
= s
/ 3 ; printf ( "average=%f \ncount=%d \n" , ave
, c
) ; return 0 ;
}
【題目98】
題目:用指針變量輸出結(jié)構(gòu)數(shù)組. 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
struct stu { int num
; char * name
; char sex
; float score
;
} boy
[ 2 ] = { { 101 , "Li ping" , 'F' , 45 } , { 103 , "Wang er" , 'M' , 80 } , } ;
int main ( )
{ struct stu * ps
; printf ( "No\tName\t\tSex\tScore\t\n" ) ; for ( ps
= boy
; ps
< boy
+ 2 ; ps
++ ) { printf ( "%d\t%s\t\t%c\t%f\t\n" , ps
-> num
, ps
-> name
, ps
-> sex
, ps
-> score
) ; } return 0 ;
}
【題目99】
題目:用結(jié)構(gòu)體存放學(xué)生的信息(包括學(xué)號、姓名、性別、家庭住址,輸出該學(xué)生的信息。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h" typedef struct student { int ID
; char Name
[ 10 ] ; char Sex
; char Add
[ 20 ] ;
} student
;
int main ( )
{ student stu1
= { 1 , "夏侯惇" , 'M' , "王者峽谷666號" } ; printf ( "\n 學(xué)號:%d 姓名:%s 性別:%c 家庭住址:%s \n" , stu1
. ID
, stu1
. Name
, stu1
. Sex
, stu1
. Add
) ; return 0 ;
}
【題目100】
題目:手機信息系統(tǒng)。 1.題目分析: 2.題目源代碼如下:
# include "stdio.h"
# define MAX_TITLE_SIZE 30
# define MAX_AUTHOR_SIZE 40
# define MAX_SIZE 2
struct book
{ char title
[ MAX_TITLE_SIZE
] ; char author
[ MAX_AUTHOR_SIZE
] ; float price
;
} ;
int main ( )
{ int count
= 0 ; int index
= 0 ; struct book lib
[ MAX_SIZE
] ; printf ( "手機信息錄入系統(tǒng) \n" ) ; while ( count
< MAX_SIZE
&& printf ( "手機型號是:" ) && gets ( lib
[ count
] . title
) != NULL && lib
[ count
] . title
[ 0 ] != '\n' ) { printf ( "制造廠商: \t" ) ; gets ( lib
[ count
] . author
) ; printf ( "價格: \t" ) ; scanf ( "%f" , & lib
[ count
] . price
) ; count
++ ; while ( getchar ( ) != '\n' ) { continue ; } if ( count
< MAX_SIZE
) { printf ( "輸入下一款手機信息\n" ) ; } } if ( count
> 0 ) { printf ( "下面是手機列表 \n" ) ; for ( index
= 0 ; index
< count
; index
++ ) { printf ( "手機型號是%s制造廠商是%s價格是%f \n" , lib
[ index
] . title
, lib
[ index
] . author
, lib
[ index
] . price
) ; } } return 0 ;
}
總結(jié)
以上是生活随笔 為你收集整理的C语言题目练习100例——题目+题目分析+源代码(91—100) 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。