ccc
课本第291页第4题 #include<stdio.h> void main() { int n, m, i, k; int p_begin; int arr[100]; scanf("%d", &n); for(i = 0; i < n; i ++) scanf("%d", &arr[i]); scanf("%d", &m); p_begin = 0; p_begin = p_begin - m < 0? -(p_begin - m) % n : (p_begin - m) % n; for(i = p_begin, k = 0; k < n; k++){ if(i >= n) i = 0; printf("%d ", arr[i++]); } } 课本第291页第11题 #include<stdio.h> void main() { int i = 0; int strcmp(char *, char *); void sort(char *p[], int n); char s[100][100]; char *p[10]; // input strings for(i = 0; i < 10; i ++) scanf("%s", s[i]); for(i = 0; i < 10; i ++) p[i] = s[i]; sort(p ,10); // display outputs on console for(i = 0; i < 10; i ++) printf("%s\n", p[i]); } int strcmp(char *s, char *t) { for( ; *s == *t; s++, t++) if(*s == '\0') return 0; return *s - *t; } void sort(char *p[], int n) { char *temp = "hello world!"; for(int i = 0; i < n ; i ++) for(int j = 0; j < n - i - 1; j ++) if(strcmp(p[j] , p[j+1]) > 0){ temp = p[j]; p[j] = p[j+1]; p[j+1] = temp; } return; } 课本习题 #include<stdio.h> #include<ctype.h> void main() { int i, j; char c; char s[100][100]; i = 0; c = 'a'; while(c != EOF){ while(!isdigit(c = getchar())) ; j = 0; s[i][j++] = (char)c; // 为了判断是不是数字要多读一个。 while(isdigit(c = getchar())) s[i][j++] = (char)c; s[i++][j] = '\0'; } printf("%d digits\n", i); for(int k = 0; k < i; k ++) printf("%s\n", s[k]); } 课本第292页 18题 #include<stdio.h> void main() { char *p[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int month; scanf("%d", &month); printf("%s\n", p[month-1]); }
习题六 int strcmp(char *s, char *t) { for( ; *s == *t; s++, t++) if(*s == '\0') return 0; return *s - *t; } 习题四 #include<stdio.h> void main() { int n, m, i, k; int p_begin; int arr[100]; scanf("%d", &n); for(i = 0; i < n; i ++) scanf("%d", &arr[i]); scanf("%d", &m); p_begin = 0; p_begin = p_begin - m < 0? -(p_begin - m) % n : (p_begin - m) % n; for(i = p_begin, k = 0; k < n; k++){ if(i >= n) i = 0; printf("%d ", arr[i++]); } } D:\lab_>a 3 1 2 3 1 2 3 1 习题三 #include<stdio.h> int main() { int arr[10]; int m, n, i; int sum = 0; for(i = 0; i < 10; i ++) scanf("%d", arr+i); scanf("%d %d", &m, &n); for(i = m; i <= n; i ++) sum += *(arr+i); printf("sum = %d", sum); return 0; } D:\lab_>a 1 2 3 4 5 6 7 8 9 0 2 3 sum = 7 习题七 #include<stdio.h> #define MAXSIZE 1000 void insert(char *, char *, char c); int length(char *s); char ori_str[MAXSIZE]; void main() { char insert_str[MAXSIZE]; char c; // inputs gets(ori_str); gets(insert_str); scanf("%c", &c); insert(ori_str, insert_str, c); // outputs printf("%s", ori_str); } void insert(char s[], char t[], char c) { int k, i, j; int p_last = length(s) + 1; int length_t = length(t); k = 0; //printf("a"); while(s[k] != '\0'){ if(s[k] == c){ for(i = p_last; i >= k+1; i --) s[i+length_t] = s[i]; //printf("b"); j = 0; for(i = k+1; i <= k+length_t; i ++) s[i] = t[j ++]; } k += length_t; } } int length(char *s) { int n; for(n = 0; *s != '\0'; s++, n++) ; return n; } D:\lab_>a aaaaa d a adadadad