You are on page 1of 4

8.

XX vowels, consonants for (int i = 2; i <= number; i++) {


#include <stdio.h>
#include <string.h> if (number % i == 0 && is_prime(i)) {
largest_prime_factor = i;
int main() {
char line[200] = { 0 }; // current line of input }
char *token; // current token
int num_words = 0; // total number of words }
int num_chars = 0; // total number of chars
printf("The largest prime factor of %d is %d.\n",
int num_lines = 0; // total number of lines
number, largest_prime_factor);
printf("Enter 'END' when you've entered all the
lines.\n\n"); return 0;
do { }
if (strtok(line, " \n")) {
num_words++; 找 1-20 最大質因數
while (strtok(NULL, " \n")) {
num_words++; #include <stdio.h>
} #include <math.h>
}
num_chars += strlen(line); // add number of int is_prime(int n) {
chars in line to total count
if (n <= 1) {
num_lines++; // increment line count
fgets(line, sizeof(line), stdin); return 0;
} while (strcmp(line, "END\n"));
}
printf("\nNumber of lines: %d\n", num_lines);
for (int i = 2; i <= sqrt(n); i++) {
printf("Number of words: %d\n", num_words);
printf("Number of chars: %d\n", num_chars); if (n % i == 0) {
}
return 0;
最大質因數
}
#include <stdio.h>
}
#include <math.h>
return 1;
}
int is_prime(int n) {
int main() {
if (n <= 1) {
for (int number = 1; number <= 20; number++) {
return 0;
// Find the largest prime factor of the
}
number
for (int i = 2; i <= sqrt(n); i++) {
int largest_prime_factor = 1;
if (n % i == 0) {
for (int i = 2; i <= number; i++) {
return 0;
if (number % i == 0 && is_prime(i)) {
}
largest_prime_factor = i;
}
}
return 1;
}
}
printf("The largest prime factor of %d is
int main() {
%d.\n", number, largest_prime_factor);
int number;
}
printf("Enter a number: ");
return 0;
scanf("%d", &number);
}
int largest_prime_factor = 1;
列出九九乘法表 strcat(sentence, " ");
#include <stdio.h> tmp = rand() % 5;
int main() { strcat(sentence, article[tmp]);
for (int j = 1; j <= 9; j++) { strcat(sentence, " ");
for (int i = 1; i <= 9; i++) { tmp = rand() % 5;
printf("%d x %d = %d\t", i, j, i * j); strcat(sentence, noun[tmp]);
}printf("\n"); strcat(sentence, ".");
} sentence[0] = toupper(sentence[0]);
return 0;} printf("%s\n", sentence);
8-11 }
#include "stdio.h" }
#include "string.h" 6.11 自設數列

#include "stdlib.h" #include <stdio.h>


#include "ctype.h" int main()
int main() {
{ int arr[] = {99, 62, 75, 79, 72, 13, 67, 26, 64, 91};
const char* article[] = { "the", "a", "one", int temp = 0;
"some", "any" }; int length = sizeof(arr)/sizeof(arr[0]);
const char* noun[] = { "boy", "girl", "dog",
"town", "car" }; printf("original array: \n");
const char* verb[] = { "drove", "jumped", "ran", for (int i = 0; i < length; i++) {
"walked", "skipped" }; printf("%d ", arr[i]);
const char* preposition[] = { "to", "from", }
"over", "under", "on" }; for (int i = 0; i < length; i++) {
char* sentence = new char[35]; for (int j = i+1; j < length; j++) {
int tmp; if(arr[i] > arr[j]) {
for (int i = 0; i < 20; i++) { temp = arr[i];
tmp = rand() % 5; arr[i] = arr[j];
strcpy(sentence, article[tmp]); arr[j] = temp;
strcat(sentence, " "); }
tmp = rand() % 5; }
strcat(sentence, noun[tmp]); }
strcat(sentence, " ");
tmp = rand() % 5; printf("\n");
strcat(sentence, verb[tmp]); printf("sorted array: \n");
strcat(sentence, " "); for (int i = 0; i < length; i++) {
tmp = rand() % 5; printf("%d ", arr[i]);
strcat(sentence, preposition[tmp]); }
return 0; return 0;
} }
6.11 隨機數列 -tion
#include <stdio.h> #include "stdio.h"
#include <stdlib.h> #include "string.h"
#include <time.h> int main()
void selectionSort(int array[], int n) { {
int i, j, minIndex, temp; char** strings = new char* [20];
for (i = 0; i < n - 1; i++) { char* ptr;
minIndex = i; int n;
for (j = i + 1; j < n; j++) { printf("Enter the number of strings (max. 20): ");
if (array[j] < array[minIndex]) { scanf("%d", &n);
minIndex = j; printf("Enter %d strings:\n", n);
} for (int i = 0; i < n; i++) {
} strings[i] = new char[25];
temp = array[i]; scanf("%s", strings[i]);
array[i] = array[minIndex]; }
array[minIndex] = temp; printf("\nStrings that end with letters 'tion':\n");
} for (int i = 0; i < n; i++) {
} if (ptr = strstr(strings[i], "tion")) {
int main() { if (*(ptr + 4) == '\0')
srand(time(0)); printf("%s\n", strings[i]);
int array[10]; else
for (int i = 0; i < 10; i++) { while(ptr = strstr(ptr + 1, "tion"))
array[i] = rand() % 90 + 10; if (*(ptr + 4) == '\0')
} printf("%s\n", strings[i]);
int n = sizeof(array) / sizeof(array[0]); }
printf("Original array: \n"); }
for (int i = 0; i < n; i++) { }
printf("%d ", array[i]); 一元二次方程式

} #include <stdio.h>
printf("\n"); #include <math.h>
selectionSort(array, n); int main()
printf("Sorted array: \n"); {
for (int i = 0; i < n; i++) { int a, b, c, d;
printf("%d ", array[i]); double root1, root2;
} printf("Enter a, b and c where a*x*x + b*x + c =
printf("\n"); 0\n");
scanf("%d%d%d", &a, &b, &c);
d = b*b - 4*a*c;
if (d >= 0) {
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);
printf("First root = %.2lf\n", root1);
printf("Second root = %.2lf\n", root2);
}
else {
printf("imaginary root");
}
return 0;
}
110 考古 參見第六章 Binary search of sorted array
第 40-68 行 變化題 6-33

size_t binarySearch(const int b[], int searchKey, size_t


low, size_t high)
{
if (low > high)
return -1;
else {
int middle = (low + high) / 2;

printRow(b, low, middle, high);


if (searchKey == b[middle])
return middle;
else if (searchKey < b[middle])
return binarySearch(b, searchKey,
low, middle - 1);
else
return binarySearch(b, searchKey,
middle + 1, high);
}
}

You might also like