本文全部公式采用mathjax渲染,观感极佳
第一章:
problems 1.7:
\[ 0xABCD + (feed)_{16} =
(AABA)_{16}\] \[ 0xABCD - (feed)_{16}
= (ACE0)_{16}\]
problems 1.8:
\[ (251)_{10} = (1111\;1011)_2 = (373)_8 =
(FB)_{16} \]
problems 1.9:
\[157原码:0000\;0000\;1001\;1101补码:0000\;0000\;1001\;1101\]
\[-153原码:1000\;0000\;1001\;1001补码:1111\;1111\;0110\;0111\]
problems 1.12:
\[ANS = (53)_{ASCII} =(5)_{char}
\]
第二章:
problems 2.3:
1 2 3 4 5 6 7 8 9 10 11 4 th 不是 , 以数字开头sizeof 不是 , 标准C的关键字x*y 不是,* 非法 temp-2 不是,- 非法 isn't 不是,' 非法 enum 不是, 标准C的关键字
problems 2.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 2 L 合法,long长整型''' 不合法,单引号组中的单引号前需要转义字符 .12 合法,double双精度浮点型 0x1ag 不合法,g不是16进制数中的符号,也不表示任何类型 33333 合法,int整形 " a" 合法,字符串常量 " " 合法,字符串常量 0.L 合法,long double长双精度浮点型 E20 不合法,缺少尾数部分 0377UL 合法,unsigned long无符号长整型 ' \18 ' 不合法,存在非8进制位 ' \0xa ' 不合法,不符合十六进制字符码表示规则 0x9cfU 合法,unsigned int无符号整形 ' \45 ' 合法,char字符型 1.E-5 合法,double双精度浮点型 ' \0 ' 合法,char字符型 3.F 合法,float浮点型 " 3 '4" " 不合法,缺少转义符 ' "' 合法,char字符型'\a' 合法,char字符型
problems 2.6:
1 2 3 4 5 6 7 8 9 10 11 (1 )int a; b = 5 ; 第一个分号改为逗号int a, b=5 ; (2 )doubel h; 关键字错误double h; (3 )int x = 2.3 ; 类型错误float x = 2.3 ; (4 )const long y; 需要赋初值const long y = 0 ; (5 )float a = 2.5 *g; g未定义变量 (6 )int a = b = 2 ; b未定义变量int a = 2 , b = 2 ;
problems 2.7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ++-- - > + - > - + - > ++, + - > , + . , < - > - > ++ . --++++ + + - > + + - > , --, + ++- -
problems 2.8:
problems 2.10:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 (1 )~x &x -> 0 (2 )v= ~x -2 (3 )w= ~x ^x 65535 (4 )x |y&x |z 5 (5 )w= y|z, (w<<3 )+(w<<1 ) 60 (6 )w= x |y&x |z<>x 113 (7 )v= -1 , v<<= 1 -2 (8 )v= ~x |x -1 (9 )w= x ^~y 65532 (10 )x |y|z>>2 3
problems 2.11:
problems 2.16:
1 ( a > b ) ? ( a > c ? a : c ) : ( c > b ? c : b )
problems 2.18:
1 ( ( a % 3 == 0 ) && (a % 10 == 5 ) ) ? 1 : 0
第三章:
problems 3.5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> int main () { char c ; scanf ("%c" ,&c) ; if (c >= 'A' && c <= 'F' ) { int tmp = c-'A' +10 ; printf ("%d" ,tmp) ; } else if ( c >= 'a' && c <= 'f' ) printf ("%d" ,c-'a' +10 ) ; else printf ("%c" ,c) ; return 0 ; }
problems 3.7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> int main () { unsigned short int k , ans ; scanf ("%u" ,&k) ; unsigned short int tmp1 = 0xf000 , tmp2 = 0x000f , tmp3 =0x0ff0 ; tmp1 = (tmp1 & k ) >> 12 ; tmp2 = (tmp2 & k ) << 12 ; tmp3 = (tmp3 & k ) ; ans = ( tmp1 | tmp2 ) | tmp3 ; printf ("%d" ,ans) ; return 0 ; }
第四章:
problems 4.1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <stdio.h> int a , b , c ;int max ( int x , int y ) {return x > y ? x : y ; }int min ( int x , int y ) {return x < y ? x : y ; }int main () { scanf ("%d%d%d" ,&a , &b , &c ) ; int maxx = max(max(a,b),c) ; int minn = min(min(a,b),c) ; if ( a == b && a < c ) { printf ("A %d" ,a) ; return 0 ; } if ( a < maxx && a > minn ) printf ("A %d" ,a) ; if ( b < maxx && b > minn ) printf ("B %d" ,b) ; if ( c < maxx && c > minn ) printf ("C %d" ,c) ; return 0 ; }
problems 4.3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> #include <stdlib.h> #define re register #define maxn 15 int year , month , date , ans ;int a[maxn] ;int judge ( int x ) { if ( x % 4 != 0 ) return 0 ; else { if ( x % 100 != 0 ) return 1 ; else { if ( x % 400 == 0 ) return 1 ; else return 0 ; } } } int main () { scanf ("%d%d%d" , &year , &month , &date ) ; a[1 ] = a[3 ] = a[5 ] = a[7 ] = a[8 ] = a[10 ] = a[12 ] = 31 ; a[4 ] = a[6 ] = a[9 ] = a[11 ] = 30 ; if ( judge(year) ) a[2 ] = 29 ; else a[2 ] = 28 ; for (re int i = 1 ; i <= month - 1 ; i ++ ) ans += a[i] ; ans += date ; printf ("%d\n" ,ans) ; return 0 ; }
problems 4.5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> #define re register double x , y ;char c ;int main () { scanf ("%lf%lf %c" ,&x,&y,&c) ; if ( c == '+' ) printf ("%.6lf" ,x+y) ; if ( c == '-' ) printf ("%.6lf" ,x-y) ; if ( c == '*' ) printf ("%.6lf" ,x*y) ; if ( c == '/' ) { if ( !y ) printf ("Error" ) ; else printf ("%.6lf" ,x/y) ; } if ( c == '&' ) printf ("Error" ) ; return 0 ; }
problems 4.9:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <stdio.h> #define re register #define maxn 1005 int n ;int a[maxn] ;int main () { scanf ("%d" ,&n) ; a[1 ] = a[2 ] = 1 ; for ( re int i = 3 ; i <= n ; i++ ) a[i] = a[i-1 ] + a[i-2 ] ; int cnt = 0 ; for ( re int i = 1 ; i <= n ; i++ ) { printf ("%6d" ,a[i]) ; if ( ++ cnt == 8 ) { putchar ('\n' ) ; cnt = 0 ; } } return 0 ; }
problems 4.10:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <stdio.h> #include <math.h> #define re register double pi = 0 , tmp = 1.0 , sign = -1.0 ; int main () { while ( fabs ( 1 / tmp ) > 1e-5 ) { sign *= (-1 ) ; pi += sign / tmp ; tmp += 2 ; } printf ("%lf" , pi * 4 ) ; return 0 ; }
problems 4.11:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <stdio.h> #include <stdlib.h> #include <math.h> #define re register #define ll long long #define maxn 1005 int n ;int a[maxn] ;int gcd ( int x , int y ) {return y ? gcd (y,x%y) : x ;}int main () { int a , b ; scanf ("%d%d" ,&a,&b) ; int tmp = gcd ( a , b ) ; printf ("%d %d" , tmp , a*b/tmp ) ; return 0 ; }
problems 4.17:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <stdio.h> #define re register int main () { int tmp , sum , cnt ; for ( re int i = 1 ; i <= 10 ; i++ ) { scanf ("%d" , &tmp) ; if ( tmp > 0 ) { cnt ++ ; sum += tmp ; } } if ( cnt ) printf ("numbers = %d , average = %lf \n" , cnt , ( 1.0 * sum ) / cnt ) ; return 0 ; }
第五章:
problems 5.1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include <stdio.h> #include <stdlib.h> #include <time.h> #define re register int sum , first , second , point ;void game ( ) { first = rand() % 6 ; second = rand() % 6 ; sum = first + second ; if ( sum == 7 || sum == 11 ) { printf ("WIN!\nGama Over ." ) ; return ; } if ( sum == 2 || sum == 3 || sum == 12 ) { printf ("Lost!\nGame over ." ) ; return ; } point += sum ; if ( point == 7 ) { printf ("Lost!\nGame over ." ) ; return ; } if ( point == sum ) { printf ("WIN!\nGame Over ." ) ; return ; } game() ; } int main () { srand( (unsigned )time(NULL ) ) ; game() ; return 0 ; }
problems 5.6:
1 不能,swap 传入的是形参,只能在swap 函数中使用,无法传回原函数。
problems 5.7:
problems 5.8:
1 2 3 4 5 6 7 8 9 10 11 int i ;void fun ( long i ) { long l = i ; { float i ; i = 3.4 ; } l = i + 2 ; } int *p = &i ;
1 2 3 4 5 6 Answer: int 型i存储类型为extern : 第1 行声明(定义),11 行使用; long 、float 型i存储类型为auto : long 型i在第二行声明(定义),4 、9 行使用; float 型i在第六行声明(定义),7 行使用。
problems 5.11:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <stdio.h> #define re register #define maxn 1005 int n , cnt , tmp ;int a[maxn] ;void hailstone ( int x ) { printf ("%4d" ,x) ; if ( ++tmp == 6 ) putchar ('\n' ) , tmp = 0 ; if ( x == 1 ) { cnt ++ ; return ; } if ( x % 2 == 0 ) { hailstone ( x / 2 ) ; cnt ++ ; } else { hailstone ( 3 *x + 1 ) ; cnt ++ ; } } int main () { scanf ("%d" ,&n) ; printf ("Hailstone generated by %d:\n" ,n) ; hailstone ( n ) ; if ( cnt % 6 != 0 ) putchar ('\n' ) ; printf ("Number of hailstone generated:%d" ,cnt) ; return 0 ; }
problems 5.13:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <stdio.h> #include <string.h> #define re register #define maxn 1005 int k ;char s[maxn] ;int digit ( char s[] , int k ) { int cnt = strlen (s) ; if ( k > cnt ) return -1 ; else return (s[ cnt - k ] - '0' ) ; } int main () { scanf ("%s %d" ,s,&k) ; printf ("%d\n" ,digit( s , k ) ) ; return 0 ; }
problems 12.1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <stdio.h> #include <time.h> #include <conio.h> #define re register #define ll long long #define il inline int n ;inline ll fib ( int n ) { if ( n == 1 || n == 2 ) return 1 ; return fib(n - 1 ) + fib(n - 2 ) ; } int main () { time_t first , second ; first = time(NULL ) ; printf ("%lld\n" ,fib(40 )) ; second = time(NULL ) ; printf ("The difference is: %f seconds" , difftime(second,first)) ; return 0 ; }
problems 12.3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <stdio.h> #include <string.h> #define re register #define maxn 10005 char s[maxn] ;int cnt ;void solve ( int pos , char s[] ) { if ( pos == 0 ) { printf ("%c" , s[pos] ) ; return ; } printf ("%c" , s[pos] ) ; solve( pos - 1 , s ) ; } int main () { scanf ("%s" ,s) ; solve( strlen (s) - 1 , s ) ; return 0 ; }
第六章
problems 6.1:
1 2 3 (2 ) 编译时将 NUMBER 换成 = 20 (3 ) 编译时将 ident 换成 (x) x (5 ) 编译时可能忽略优先级
problems 6.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #define swap(x,y) t=x,x=y,y=t int t ;int main () { int a , b ; scanf ("%d%d" ,&a , &b ) ; swap(a,b) ; printf ("%d %d" ,a,b ) ; return 0 ; }
第七章:
problems 7.7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <stdio.h> #define re register #define maxn 100005 int n ;int a[maxn] , b[maxn] ; int max ( int x , int y ) {return x > y ? x : y ;} void swap ( int *a , int *b ) { int tmp = *a ; *a = *b ; *b = tmp ; } int main () { scanf ("%d" ,&n) ; for ( re int i = 1 ; i <= n ; i++ ) scanf ("%d" , a + i ) ; for ( re int i = 1 ; i <= n ; i++ ) { int maxx = i ; for ( re int j = i + 1 ; j <= n ;j ++ ) { if ( a[j] < a[maxx] ) maxx = j ; } swap ( &a[i] , &a[maxx] ) ; } for ( re int i = 1 ; i <= n ; i++ ) printf ("%d " ,a[i] ) ; return 0 ; }
problems 7.8:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <stdio.h> #define re register #define maxn 10005 int n , k ;int a[maxn] ;int main () { scanf ("%d%d" , &n , &k ) ; for ( re int i = 0 ; i < n ; i++ ) { scanf ("%d" , &a[i]) ; } for ( re int i = k ; i < n ; i++ ) printf ("%d " , a[i] ) ; for ( re int i = 0 ; i < k ; i++ ) printf ("%d " , a[i] ) ; return 0 ; }
problems 7.9:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdio.h> #include <string.h> #define re register #define maxn 10005 int cnt , num , alp , other ;char s[maxn] ;int main () { scanf ("%s" ,s) ; int cnt = strlen (s) ; for ( re int i = 0 ; i < cnt ; i++ ) { if ( s[i] >= '0' && s[i] <= '9' ) num++ ; else if ( ( s[i] >= 'a' && s[i] <= 'z' ) || ( s[i] >= 'A' && s[i] <= 'Z' ) alp ++ ; else other++ ; } printf ("%d %d %d" , num , alp , other ) ; return 0 ; }
problems 7.13:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <stdio.h> #include <string.h> #define re register #define maxn 10005 int n ;char s[maxn] , t[maxn] ;int min ( int x , int y ) { return x < y ? x : y ;}void strncpy ( char a[] , char b[] , int x ) { int len_a = strlen (a) , len_b = strlen (b) ; for ( re int i = 0 ; i < min( x , len_b ) ; i++ ) { a[i] = b[i] ; } return ; } int main () { scanf ("%s%s%d" , s , t , &n ) ; strncpy (s,t,n) ; printf ("%s" ,s) ; return 0 ; }
problems 7.15:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio.h> #include <string.h> #include <math.h> #define re register #define maxn 10005 int n ;char s[maxn] ;int change (char c) { if (c >= 'A' && c <= 'F' ) return c-'A' +10 ; else if ( c >= 'a' && c <= 'f' ) return c-'a' +10 ; else return c-'0' ; } int main () { int n ; scanf ("%d" ,&n) ; while ( n-- ) { scanf ("%s" ,s) ; int cnt = strlen (s) , ans = 0 , tmp = 0 ; for ( re int i = cnt - 1 ; i > 1 ; i-- ) ans += change(s[i]) * pow (16 ,tmp++) ; printf ("%d " ,ans ) ; } return 0 ; }
problems 12.5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <stdio.h> #include <string.h> #define re register char s[10005 ] ;int flag = 1 ;int l = 0 ;void judge ( char s[] , int l , int r ) { if ( l + 1 == r) { if ( s[l] != s[r] ) flag = 0 ; return ; } if ( l == r ) return ; if ( s[l] != s[r] ) flag = 0 ; judge ( s , l + 1 , r - 1 ) ; } int IsPalindrom ( char arr[] , int len ) { if ( l + 1 == len) { if ( s[l] != s[len] ) return 0 ; return 1 ; } if ( l == len ) return 1 ; if ( s[l] != s[len] ) return 0 ; l++ ; IsPalindrom( arr , len - 1 ) ; } int main () { scanf ("%s" ,s) ; printf ("%d\n" ,IsPalindrom( s , strlen (s) - 1 ) ) ; return 0 ; }