|
发表于 2020-11-17 20:38:12
|
显示全部楼层
本帖最后由 jackz007 于 2020-11-17 20:39 编辑
第1题
- #include <stdio.h>
- void show(int d[] , int n)
- {
- int i ;
- printf("%d" , d[0]) ;
- for(i = 1 ; i < n ; i ++) printf("\t%d" , d[i]) ;
- printf("\n") ;
- }
- void dedup(int d[] , int m)
- {
- int i , j , k , n ;
- for(n = m , i = 0 ; i < n - 1 ; i ++) {
- for(j = n - 1 ; j > i ; j --) {
- if(d[j] == d[i]) {
- for(k = j + 1 ; k < n ; k ++) d[k - 1] = d[k] ;
- n -- ;
- }
- }
- }
- for(; n < m ;) d[n ++] = 0 ;
- }
- int main(void)
- {
- int d[40] = {18 , 22 , 55 , 53 , 12 , 18 , 89 , 98 , 77 , 33 ,\
- 52 , 73 , 77 , 26 , 13 , 88 , 17 , 19 , 53 , 18 ,\
- 18 , 22 , 55 , 53 , 12 , 18 , 89 , 98 , 77 , 33 ,\
- 52 , 73 , 77 , 26 , 13 , 88 , 17 , 19 , 53 , 18} ;
- show(d , 40) ;
- dedup(d , 40) ;
- printf("\n") ;
- show(d , 40) ;
- }
复制代码
编译、运行实况
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>x
- 18 22 55 53 12 18 89 98 77 33
- 52 73 77 26 13 88 17 19 53 18
- 18 22 55 53 12 18 89 98 77 33
- 52 73 77 26 13 88 17 19 53 18
- 18 22 55 53 12 89 98 77 33 52
- 73 26 13 88 17 19 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0
- 0 0 0 0 0 0 0 0 0 0
- D:\00.Excise\C>
复制代码
第2题
- #include <stdio.h>
- int main(void)
- {
- int d[20] , e[10] = {0} , f , k , m ;
- printf("请输入20个值为 0 - 9 的整数 : ") ;
- for(k = 0 ; k < 20 ; k ++) scanf("%d" , & d[k]) ;
- for(f = 1 , k = 0 ; k < 20 && f ; k ++) {
- if(d[k] >= 0 && d[k] <= 9) {
- e[d[k]] ++ ;
- } else {
- printf("数值超限!") ;
- f = 0 ;
- }
- }
- if(f) for(k = 0 ; k < 10 ; k ++) if(e[k]) printf("%d : %d\n" , k , e[k]) ;
- }
复制代码
编译、运行实况
- D:\00.Excise\C>g++ -o x1 x1.c
- D:\00.Excise\C>x1
- 请输入20个值为 0 - 9 的整数 : 1 5 7 8 9 2 3 4 5 4 0 2 5 6 1 3 2 4 8 9
- 0 : 1
- 1 : 2
- 2 : 3
- 3 : 2
- 4 : 3
- 5 : 3
- 6 : 1
- 7 : 1
- 8 : 2
- 9 : 2
- D:\00.Excise\C>
复制代码 |
|