第四章编程题
每一题中,上边为自己写的代码;下边的为Instructor’s Guide的答案。
2.求1~100的质数。
/* 求1~100中的质数。 */ #include <stdio.h> int main(void) { printf("1\n2\n"); int i, j; for (i = 3; i <= 100; i++) { for (j = 2; j <= i; j++) { if (i % j == 0) break; if (j + 1 == i) printf("%d\n", i); } } }
/* **Compute and print all the prime numbersfrom 1 to 100. */ #include <stdio.h> int main () { int number; int divisor; /* **One and two are easy. */ printf("1\n2\n"); /* **No other even numbers are prime;look at the remaining odd ones. */ for (number = 3; number <= 100; number =number + 2) { /* **See if any divisor from 3 up to the number evenly divides the **number. */ for (divisor = 3; divisor < number; divisor = divisor + 2) { if (number % divisor == 0) break; } if (divisor >= number ) printf("%d\n",number); } } /* **tricks:1.去掉偶数提高了效率; ** 2.第二个for语句跟自己的不同(if在{}后面)。 */
3.判断是等边三角形,等腰三角形还是普通三角形。
此题中,自己的代码效率不高,应先比较三条边的大小。
还要注意多个相同类型的scanf的语法。
#include <stdio.h> int main (void) { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a < 0 || b < 0 || c < 0 || a+b<=c || a+c<=b || b+c<=a) { printf("not an triangle.\n"); return 0; } if ( a== b && b == c) printf("equilateral triangle\n"); else if (a == b || b == c || a == c) printf("isoceles triangle\n"); else printf("normal.\n"); return 0; }
/* **Classify the type of a triangle given the lengths of its sides. */ #include <stdio.h> #include <stdlib.h> int main () { float a, b, c, temp; printf("Enter the lengths of the three sides of the triangle:"); scanf("%f %f %f", &a, &b, &c); if (a < b) { temp = a; a = b; b = temp; } if (a < c) { temp = a; a = c; c = temp; } if (b < c) { temp = b; b = c; c = temp; } if (c <= 0 || b + c < a) printf("not an triangle.\n"); else if (a == b & b == c) printf("equilateral.\n"); else if (a == b || b == c) printf("isosceles.\n"); else printf("scalene.\n"); return 0; }
4.这题自己的代码实在没啥出彩的地方。但guide里头的很美。所以就贴一下。尤其值得注意的是如何去补充那个NUL字符。
题:编写函数copy_n(char dst[], char src[], int n);
用于复制数组src到dst,要求如下:必须正好复制n个字符到dst中,不能多也不能少。如果src的字符串长度小于n,就补充足够的NUL字符。
void copy_n (char dst[], char src[], int n) { int dst_index, src_index; src_index = 0; for (dst_index = 0; dst_index < n; dst_index += 1) { dst[dst_index] = src[src_index]; if (src[src_index] != 0) src_index += 1; } }
int substr (char dst[], char src[], int start, int len) { int i; if (src[start] == '\0' || start < 0 || len < 0) { dst[0] = '\0'; return 0; } else { for (i = 0;i < len; i++) { if (!src[i+start]) { dst[i] = src[i+start]; break; } dst[i] = src[i+start]; } dst[i+1] = '\0'; return i;N } }
int substr_1 (char dst[], char src[], int start, int len) { int srcindex; int dstindex; dstindex = 0; if (start >= 0 && len > 0) { for (srcindex = 0; src[srcindex] != '\0' && srcindex < start; srcindex += 1) ; while (src[srcindex] != '\0' && len > 0) { dst[dstindex] = src[srcindex]; dstindex++; srcindex++; len--; } } dst[dstindex] = '\0'; return dstindex; }
void deblank (char string[]) { int i, j, a = 0; char dst[20]; for (i = 0; string[i] != '\0'; i++) { for(j = i; string[j] == '\0'; j++) ; if (j - i < 2) i = j; dst[a] = string[i]; a++; } }
这题答案的代码编译有错???
#define NUL ¡¯\0¡¯ void deblank(char *string) { char *dest; char *src; int ch; /* **Set source and destination pointers to beginning of the string,then **move to 2nd character in string. */ src = string; dest = string++; /* **Examine each character from the source string. */ while((ch = *src++) != NUL) { if(is_white(ch)) { if(src==string||!is_white(dest[¨C1])) *dest++=' '; } else { *dest++ = ch; } } *dest = NUL; //*dest = NUL; } int is_white(int ch) { return ch==' '||ch== '\t'||ch=='\v'||ch=='\f'||ch=='\n'||ch=='\r'; }