Created
January 14, 2016 04:26
-
-
Save ecnelises/16439984807cf9bfbeea to your computer and use it in GitHub Desktop.
2016年同济C语言期末考试
This file contains 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
#include <stdio.h> | |
#include <ctype.h> | |
int main(int argc, char const *argv[]) | |
{ | |
char ch, previous = '\0'; | |
FILE *fp_in = fopen("in_1.txt", "r"); | |
FILE *fp_out = fopen("1454001_1_out.txt", "w"); | |
/* | |
* If a variable name starts with an underscore, | |
* it will cast it to an upper letter. | |
*/ | |
while ((ch = fgetc(fp_in)) != EOF) { | |
if (previous == '_' && isalpha(ch)) { | |
fputc(toupper(ch), fp_out); | |
} else if (ch != '_') { | |
fputc(ch, fp_out); | |
} | |
previous = ch; | |
} | |
if (previous == '_') { | |
fputc(previous, fp_out); | |
} | |
fclose(fp_in); | |
fclose(fp_out); | |
return 0; | |
} |
This file contains 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
/* | |
尽管心里活着的还是那个 | |
年轻人 | |
因为不安而频频回首 | |
无知地索求 羞耻于求救 | |
不知疲倦地翻越 每一个山丘 | |
越过山丘 虽然已白了头 | |
喋喋不休 时不我予的哀愁 | |
还未如愿见着不朽 | |
就把自己先搞丢 | |
越过山丘 才发现无人等候 | |
喋喋不休 再也唤不回温柔 | |
为何记不得上一次是谁给的拥抱 | |
在什么时候 | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int power(int num, int exp) | |
{ | |
int res = 1; | |
int i; | |
for (i = 0; i < exp; ++i) { | |
res *= num; | |
} | |
return res; | |
} | |
/* | |
* Fill the array in the way: | |
* | |
* item[2k] = num + 1; | |
* item[2k + 1] = num + 2; | |
* | |
* as if it's a binary tree. | |
*/ | |
void init(int *item, int pos, int num) | |
{ | |
item[pos] = num; | |
if (num < 15) { | |
init(item, pos * 2, num + 1); | |
init(item, pos * 2 + 1, num + 2); | |
} | |
} | |
/* | |
* I'm not sure whether there's problem of floating precise. | |
* | |
* Let's take occasion when n=3 as an example. | |
* f(1, 3, 1) = 2/3 * f(2, 3, 2) + 1/3 * f(3, 3, 2); | |
* f(2, 3, 2) = 8/9 * f(3, 3, 3) + 1/9 * f(4, 3, 3); | |
* f(3, 3, 2) = 2; | |
* f(3, 3, 3) = 3; | |
* f(4, 3, 3) = 0; | |
*/ | |
double find_probability(int current, int n, int quantity) | |
{ | |
double p = 0.0; | |
if (current == n) { | |
return (double)quantity; | |
} else if (current < n) { | |
p = 1.0 / (double)power(3, current); | |
return p * find_probability(current + 2, n, quantity + 1) | |
+ find_probability(current + 1, n, quantity + 1) * (1.0 - p); | |
} else { | |
return 0.0; | |
} | |
} | |
/* | |
* The answer can be shown as a binary tree. | |
* For any node with value n, its leftchild's value is n + 1, | |
* and rightchild's value is n + 2. | |
* So we can find it recursively. | |
*/ | |
int main(int argc, char const *argv[]) | |
{ | |
FILE *fp_in = fopen("in_2.txt", "r"); | |
FILE *fp_out = fopen("1454001_2_out.txt", "w"); | |
int n; | |
/* n<=15, size<=2^0+...+2^15=2^16-1 */ | |
int *res = (int*)malloc(sizeof(int) * 65536); | |
double result; | |
memset(res, 0, sizeof(int) * 65536); | |
fscanf(fp_in, "%d", &n); | |
res[0] = 0; | |
init(res, 1, 1); | |
if (n != 2) { | |
/* | |
* When n==2, there's only one occasion: | |
* 0->1->2 | |
* No probability for 0->2. | |
*/ | |
result = find_probability(1, n, 1); | |
} else { | |
result = 2.0; | |
} | |
fprintf(fp_out, "%.4lf", result); | |
free(res); | |
fclose(fp_in); | |
fclose(fp_out); | |
return 0; | |
} |
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
/* Comparision function for qsort */ | |
int int_cmp(const void *n1, const void *n2) | |
{ | |
return *((const int*)n1) - *((const int*)n2); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
int num = 0; | |
int *score, *type; | |
int *specialists, *masses; | |
/* Loop variables */ | |
int i, s, m; | |
/* Quantity of specialists and masses. */ | |
int specialist_num = 0, mass_num = 0; | |
double specialists_average = 0.0, mass_average = 0.0; | |
FILE *fp_in = fopen("in_3.txt", "r"); | |
FILE *fp_out = fopen("1454001_3_out.txt", "w"); | |
/* Get the number of scores. */ | |
char ch; | |
while ((ch = fgetc(fp_in)) != EOF) { | |
if (ch == ',') { | |
++num; | |
} | |
} | |
num = (num + 2) / 2; | |
rewind(fp_in); | |
score = (int*)malloc(sizeof(int) * num); | |
type = (int*)malloc(sizeof(int) * num); | |
/* Get the scores */ | |
for (i = 0; i < num; ++i) { | |
fscanf(fp_in, "%d", score + i); | |
fgetc(fp_in); | |
} | |
for (i = 0; i < num; ++i) { | |
fscanf(fp_in, "%d", type + i); | |
fgetc(fp_in); | |
} | |
for (i = 0; i < num; ++i) { | |
if (type[i] == 1) { | |
++specialist_num; | |
} else { | |
++mass_num; | |
} | |
} | |
specialists = (int*)malloc(sizeof(int) * specialist_num); | |
masses = (int*)malloc(sizeof(int) * mass_num); | |
for (i = 0, s = 0, m = 0; i < num; ++i) { | |
if (type[i] == 1) { | |
specialists[s++] = score[i]; | |
} else { | |
masses[m++] = score[i]; | |
} | |
} | |
/* Sort the array for removing the largest and smallest */ | |
qsort(specialists, specialist_num, sizeof(int), int_cmp); | |
qsort(masses, mass_num, sizeof(int), int_cmp); | |
/* Calculate the average score */ | |
if (specialist_num <= 2) { | |
fprintf(fp_out, "0"); | |
} else { | |
for (i = 1; i < specialist_num - 1; ++i) { | |
specialists_average += specialists[i]; | |
} | |
specialists_average /= (specialist_num - 2); | |
if (mass_num <= 2) { | |
fprintf(fp_out, "%d", (int)specialists_average); | |
} else { | |
for (i = 1; i < mass_num - 1; ++i) { | |
mass_average += masses[i]; | |
} | |
mass_average /= (mass_num - 2); | |
fprintf(fp_out, "%d", | |
(int)(0.6 * (int)specialists_average + 0.4 * (int)mass_average)); | |
} | |
} | |
free(specialists); | |
free(masses); | |
free(score); | |
free(type); | |
fclose(fp_in); | |
fclose(fp_out); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment