Created
October 24, 2012 05:17
-
-
Save Abreto/3944117 to your computer and use it in GitHub Desktop.
白书第二章上机练习
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - approximation */ | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| double pi_4 = 0.0, t = 0.0; | |
| i = 1; | |
| while( (t=1.0/(2*i-1)) >= 0.000001 ) | |
| pi_4 += ( (i++%2) ? 1 : -1 ) * t; | |
| return !printf("%lf\n", pi_4); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - daffodil */ | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int a = 0, b = 0, c = 0; | |
| for(a = 1;a < 10;a++) | |
| for(b = 0;b < 10;b++) | |
| for(c = 0;c < 10;c++) | |
| if( (100*a+10*b+c) == (a*a*a+b*b*b+c*c*c) ) | |
| printf("%d%d%d ", a, b, c); | |
| return !printf("\n"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - decimal */ | |
| #include <stdio.h> | |
| #include <string.h> | |
| int main(void) | |
| { | |
| int i = 0, j = 0; | |
| int a = 0, b = 0, c = 0; | |
| char r[20]; | |
| scanf("%d %d %d", &a, &b, &c); | |
| sprintf(r, "%lf", (double)a/(double)b); | |
| for(i = 0;i < strlen(r);i++) | |
| { | |
| if( r[i] == '.' ) | |
| { | |
| for(j = 0;j < c;j++) | |
| if( i+j > strlen(r)-1 ) | |
| putchar('0'); | |
| else | |
| putchar(r[i+j]); | |
| if( j > strlen(r)-1 ) | |
| putchar('0'); | |
| else if ( i+j+1 > strlen(r)-1 ) | |
| putchar(r[i+j]); | |
| else | |
| putchar(r[i+j]+((r[i+j+1]>'4')?1:0)); | |
| return !printf("\n"); | |
| } | |
| else | |
| putchar(r[i]); | |
| } | |
| return !printf("\n"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - digit */ | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| int num = 0; | |
| scanf("%d", &num); | |
| while(num >= 1) | |
| i += (num/=10)||1; | |
| return !printf("%d\n", i); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - hanxin */ | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| int a = 0, b = 0, c = 0; | |
| scanf("%d %d %d", &a, &b, &c); | |
| for(i = 10;i < 101;i++) | |
| if( (i%3==a) && (i%5==b) && (i%7==c) ) | |
| return !printf("%d\n", i); | |
| return !printf("No answer\n"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - harmony */ | |
| #include <stdio.h> | |
| #include <math.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| int n = 0; | |
| double sum = 0.0; | |
| scanf("%d", &n); | |
| for(i = 1;i < n+1;i++) | |
| sum += 1.0 / (double)i; | |
| return !printf("%.3lf\n", sum); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - premutation */ | |
| #include <stdio.h> | |
| #define JOIN(a,b,c) ( (a)*100 + (b)*10 + (c) ) | |
| void solve(int start, int end, int top); | |
| int s[9] = {1,2,3,4,5,6,7,8,9}; | |
| int n[9] = {0}; | |
| int main(void) | |
| { | |
| int i = 0; | |
| /* 既然不必太动脑筋.. */ | |
| solve(0, 8, 0); | |
| return 0; | |
| } | |
| void swap(int *a, int *b) | |
| { | |
| int t = *a; | |
| *a = *b; | |
| *b = t; | |
| } | |
| void solve(int start, int end, int top) | |
| { | |
| if( start == end ) | |
| { | |
| int i = 0, j = 0, k = 0; | |
| *(n+top) = *(s+start); | |
| i = JOIN(*(n+0), *(n+1), *(n+2)); | |
| j = JOIN(*(n+3), *(n+4), *(n+5)); | |
| k = JOIN(*(n+6), *(n+7), *(n+8)); | |
| if( j==2*i && k==3*i ) | |
| printf("%d,%d,%d\n", i, j, k); | |
| } else { | |
| int i = 0; | |
| for(i = start;i < end+1;i++) | |
| { | |
| *(n+top) = *(s+i); | |
| swap(s+start, s+i); | |
| solve(start+1, end, top+1); | |
| swap(s+i, s+start); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - stat */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| int n = 0, m = 0; | |
| int *a = NULL; | |
| int count = 0; | |
| scanf("%d", &n); | |
| a = (int *)malloc( n * sizeof(int) ); | |
| for(i = 0;i < n;i++) | |
| scanf("%d", a+i); | |
| scanf("%d", &m); | |
| for(i = 0;i < n;i++) | |
| if( *(a+i) < m ) | |
| count++; | |
| return !printf("%d\n", count); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - subsequence */ | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int i = 0; | |
| int n = 0, m = 0; | |
| double sum = 0.0; | |
| scanf("%d %d", &n, &m); | |
| for(i = n;i < m+1;i++) | |
| sum += 1.0/i/i; // 考虑到i*i可能溢出 | |
| return !printf("%.5lf\n", sum); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Chapter 02 - triangle */ | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int i = 0, j = 0; | |
| int n = 0; | |
| scanf("%d", &n); | |
| for(i = 0;i < n;i++) | |
| { | |
| for(j = i;j > 0;j--) | |
| printf(" "); | |
| for(j = 0;j < (2*n-1-2*i);j++) | |
| printf("#"); | |
| printf("\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment