本题由三个部分构成
(1) 编写一个函数CountEvensAndOdds,其功能是统计一个整型数组中奇数和偶数的个数。该函数有4个参数,整数型数组、数组的长度和两个指针(用于返回奇数、偶数的个数)
(2) 利用(1)中的CountEvensAndOdds函数,编写一个Partition函数,其功能是找出给一个整型数组中的所有奇数和偶数,并把它们分别保存在两个新动态开辟的整型数组中(提示:因为奇数和偶数的个数不定,为了节省空间,需要按需分配空间,即有多少个奇数就分配一个多大的奇数数组,可以用C语言的库函数malloc函数动态创建数组),然后返回这两个新数组及其长度。该函数的参数有6个,整型数组、数组长度、奇数数组及其长度、偶数数组及其长度。
(3) 编写主函数,要求能够首先输入整数型原始数组的大小,然后动态生成该原始数组的空间,并能够用随机数对该数组进行初始化;然后调用(2)中函数Partition(),将上述原始数组进行分区;然后在主函数中将原始数组、奇数数组、偶数数组分别输出。
要求:每个数组的大小不定,需要动态生成;指针和多级指针作为函数参数。
#include<stdio.h>
#include<stdlib.h>
void CountEvensAndOdds(int arr[], int n,int *even,int *odd) {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) *even= *even +1;// *even++ 이런식으로 작성하면 오류난다 이유: *even 주소에값에다가 더하게 되게 때문이다.
else *odd= *odd +1;
}
}
void Partition(int arr[], int n, int *evens, int* even, int *odds, int* odd) {
int e = 0, o = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
evens[e]=arr[i];
e++;
}
else {
odds[o] = arr[i];
o++;
}
}
}
int main(void) {
int n,even=0,odd=0;
int* arr,*evens,*odds;
printf("input n:");
scanf_s("%d", &n);
arr = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) arr[i] = rand() % 100;
CountEvensAndOdds(arr, n, &even, &odd);
evens = malloc(sizeof(int) * even);
odds = malloc(sizeof(int) * odd);
Partition(arr, n, evens, &even, odds, &odd);
printf("even:");
for (int i = 0; i < even; i++) printf("%d ", evens[i]);
printf("odd:");
for (int i = 0; i < odd; i++) printf("%d ",odds[i]);
free(evens);
free(odds);
free(arr);
return 0;
}
실행결과
编写一个程序,求给定两个字符串的最长的单词,假设单词是由空格间隔的。
假设给定的两个字符串分别为:
This is C Programming text
This is a text for C Programming
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void) {
char str1[100] = { 0 };
char str2[100] = { 0 };
fgets(str1, 100, stdin);
fflush(stdin);
fgets(str2, 100, stdin);
fflush(stdin);
int l1 = strlen(str1)-1;
int l2 = strlen(str2)-1;
int words=0,sub_words=0;
char word1[50][20] = { 0,0 };
char word2[50][20] = { 0,0 };
int i1 = 0, i2 = 0,k=0;
for (int i=0; i < l1; i++) {
if (str1[i] != ' ') word1[i1][k] = str1[i],k++;
else k = 0,i1++;
}
k = 0;
for (int i=0; i < l2; i++) {
if (str2[i] != ' ') word2[i2][k] = str2[i],k++;
else k = 0,i2++;
}
if (i1 > i2) words = i1+1, sub_words = i2+1;
else words = i2+1,sub_words=i1+1;
int maxl = 0;
char answer[20] = { 0 };
for (int i = 0; i < words; i++) {
for (int j = 0; j < sub_words; j++) {
if (words == i1) {
//printf("%s %s", word1[i], word2[j]);
if (!strcmp(word1[i], word2[j])) {
if (maxl < strlen(word1[i]) ) {
maxl = strlen(word1[i]) ;
strcpy_s(answer, 20, word1[i]);
}
}
}
else {
//printf("%s %s", word2[i], word1[j]);
if (!strcmp(word2[i], word1[j])) {
if (maxl < strlen(word1[i]) ) {
maxl = strlen(word1[i]) ;
strcpy_s(answer, 20, word1[i]);
}
}
}
}
}
printf("%s", answer);
return 0;
}
실행화면
编写一个程序,求给定三个字符串的最长公共子串。
假设给定的三个字符串分别为:
What is local bus
Name some local bus
A local bus is high speed I/O bus close to the processor.
이문제는 매우 구현하는데 매우 힘들었다 코드도 깔끔하지않고 개선할 필요가있다.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void) {
char str1[100] = { 0 };
char str2[100] = { 0 };
char str3[100] = { 0 };
fgets(str1, 100, stdin);
fflush(stdin);
fgets(str2, 100, stdin);
fflush(stdin);
fgets(str3, 100, stdin);
fflush(stdin);
int l1 = strlen(str1)-1;
int l2 = strlen(str2)-1;
int l3 = strlen(str3) - 1;
//int words=0,sub_words=0;
char word1[50][25] = { 0,0 };
char word2[50][25] = { 0,0 };
char word3[50][25] = { 0,0 };
int i1 = 0, i2 = 0,i3=0,k=0;
for (int i=0; i < l1; i++) {
if (str1[i] != ' ') word1[i1][k] = str1[i],k++;
else k = 0,i1++;
}
k = 0;
for (int i=0; i < l2; i++) {
if (str2[i] != ' ') word2[i2][k] = str2[i],k++;
else k = 0,i2++;
}
k = 0;
for (int i = 0; i < l3; i++) {
if (str3[i] != ' ') word3[i3][k] = str3[i], k++;
else k = 0, i3++;
}
//if (i1 > i2) words = i1+1, sub_words = i2+1;
//else words = i2+1,sub_words=i1+1;
char answer2[25] = { 0 };
int maxl = 0;
char answer[25] = { 0 };
char space[2] = " ";
for (int i = 0; i < i1; i++) {
for (int j = 0; j < i2; j++) {
if (!strcmp(word1[i], word2[j])) {
for (int k = 0; k < i3; k++) {
if (!strcmp(word1[i], word3[k])) {
int t = 1;
strcpy_s(answer2, 25, word1[i]);
while ((!strcmp(word1[i + t], word3[k + t])) &&
(!strcmp(word1[i + t], word2[j + t]))
) {
strcat_s(answer2, 25, space);
strcat_s(answer2, 25, word1[i + t]);
t++;
if ((i + t > i1) || (k + t > i3) || (j + t > i2)) break;
}
if (maxl < strlen(answer2)) {
maxl = strlen(answer2);
strcpy_s(answer, 25, answer2);
}
}
}
}
}
}
printf("%s", answer);
return 0;
}
실행결과