C语言第三次作业

发布在 C语言

第一题 C语言成绩


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<string.h>
void swap(int *score_a, int *score_b, char *name_a, char *name_b);
int main (void)
{
int number, count;
while (scanf("%d", &number) != EOF)
{
putchar('\n');
int scores[number];
char names[number][81];
for (count = 0 ; count < number ; ++count)
scanf("%s%d", names[count], &scores[count]);
int i, j;
for (i = 0 ; i < number ; ++i)
for (j = i ; j < number ; ++j)
if (scores[i] < scores[j])
swap(&scores[i], &scores[j], &names[i][0], &names[j][0]);
for (count = 0 ; count < number ; ++count)
printf("%s %d\n", names[count], scores[count]);
putchar('\n');
}
return 0;
}

void swap(int *score_a, int *score_b, char *name_a, char *name_b)
{
int t;
char str_t[81];
t = *score_a;
*score_a = *score_b;
*score_b = t;
strcpy(str_t, name_a);
strcpy(name_a, name_b);
strcpy(name_b, str_t);
return;
}

第二题 冒泡排序


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
int main(void)
{
int sequence[10];
int count;
int i, j;
int temp;
while (scanf("%d", &sequence[0]) != EOF)
{
for (count = 1 ; count < 10 ; ++count)
scanf("%d", &sequence[count]);
for (i = 0 ; i < 10 ; ++i)
{
for (j = 0 ; j < 8 - i ; ++j)
{
if (sequence[j] > sequence[j+1])
{
temp = sequence[j];
sequence[j] = sequence[j+1];
sequence[j+1] = temp;
}
}
}
for(count = 0 ; count < 10 ; ++count)
printf("%d ", sequence[count]);
putchar('\n');
}
return 0;
}

第三题 n人报数(此题可查找“约瑟夫问题”)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
int main (void)
{
int number,count;
int current;
int i;
while (scanf("%d", &number) != EOF)
{
int people[10000];
for (i = 0 ; i < number ; ++i)
people[i] = 1;
count = number;
current = 0;
i = 0;
while (count > 1)
{
if (i >= number)
i = 0;
if (people[i] == 1)
current += 1;
if (count == 1)
break;
if (current == 3)
{
people[i] = 0;
count -= 1;
current = 0;
}
i++;
}
for (i = 0 ; i < number ; ++i)
if (people[i] == 1)
{
printf("%d\n", i + 1);
break;
}
}
return 0;
}

第四题 定义宏交换参数


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#define Swap(x,y) {x = x + y; y = x - y; x = x - y;}

int main (void)
{
int a, b;
int count = 0;
while (scanf("%d%d", &a, &b) != EOF)
{
if (count)
putchar('\n');
count += 1;
printf("Case %d:\n", count);
printf("Before Swap:a=%d b=%d\n", a, b);
Swap(a,b);
printf("After Swap:a=%d b=%d\n", a, b);

}
return 0;
}

第五题 字符串复制


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
void mycpy (char s[], char t[], int n);
int main (void)
{
int number;
char sentence[1001];
char result[1001];
int n;
scanf("%d", &number);
getchar();//读取\n
int count;
for (count = 0 ; count < number ; ++count)
{
fgets(sentence, 1001, stdin);
scanf("%d", &n);
getchar();//读取\n
mycpy(result, sentence, n);
printf("%s\n", result);
}
return 0;
}
void mycpy (char s[], char t[], int n)
{
int i;
for (i = 0 ; i < n ; ++i)
{
s[i] = t[i];
}
s[n] = '\0';
return;
}

第六题 统计


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
int main (void)
{
char currentChar;
int numberCount[10] = {0};
int englishCharCount = 0;
int otherCount = 0;
int count;
while ((currentChar = getchar()) != EOF)
{
switch (currentChar)
{
case '0': numberCount[0] += 1 ; break;
case '1': numberCount[1] += 1 ; break;
case '2': numberCount[2] += 1 ; break;
case '3': numberCount[3] += 1 ; break;
case '4': numberCount[4] += 1 ; break;
case '5': numberCount[5] += 1 ; break;
case '6': numberCount[6] += 1 ; break;
case '7': numberCount[7] += 1 ; break;
case '8': numberCount[8] += 1 ; break;
case '9': numberCount[9] += 1 ; break;
default: break;
}
if ((currentChar >= 'A' && currentChar <= 'Z') || (currentChar >= 'a' && currentChar <= 'z'))
englishCharCount += 1;
else if (!(currentChar >= '0' && currentChar <= '9'))
otherCount += 1;
}
for (count = 0 ; count < 10 ; ++count)
printf("Number %d: %d\n", count, numberCount[count]);
printf("characters: %d\n", englishCharCount);
printf("other: %d\n", otherCount);
return 0;
}

第七题 选择排序


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
int main (void)
{
int sequence[20];
int number;
int count;
int i, j;
int temp;
while (scanf("%d", &number), number != 0)
{
for (count = 0 ; count < number ; ++count)
scanf("%d", &sequence[count]);
for (i = 0 ; i < number ; ++i)
for (j = i ; j < number ; ++j)
if (sequence[i] > sequence[j])
{
temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
}
for (count = 0 ; count < number ; ++count)
{
if (count)
putchar(' ');
printf("%d", sequence[count]);
}
putchar('\n');
}
return 0;
}

评论和共享

  • 第 1 页 共 1 页
作者的图片

码龙黑曜

iOS开发者/计算机科学/兽人控


华中科技大学 本科在读


Wuhan, China