几个C语言程序
QSLS QvQ

冒泡排序

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
# include <stdio.h>
int main()
{
int a[10];
int i,j,t;
printf("10 numbers ");
for(i = 0;i < 10;i++){
scanf("%d",&a[i]);
}
printf("\n");
for(j = 0;j < 9;j++){
for(i = 0;i < 9-j;i++){
if(a[i]>a[i+1]){
t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
}
}
printf("the sorted numbers: ");
for(i = 0;i < 10;i++){
printf("%d",a[i]);
}
printf("\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
#include<stdio.h>

void sort(int array[], int n)
{
int i, j, k, t;
for (i = 0; i < n - 1; i++) {
k = i;
for (j = i + 1; j < n; j++) {
if (array[j] < array[k]) {
k = j;
}
}
t = array[k];
array[k] = array[i];
array[i] = t;
}
}
int main()
{
int a[10], i;
printf("enter array:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
sort(a, 10);
printf("the sorted array:\n");
for (i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n");
}

字符倒序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>

void inverse(char s[])
{
int l = strlen(s);
for (int i = 0; i < l / 2; i++) {
char t = s[i];
s[i] = s[l - i - 1];
s[l - 1 - i] = t;
}
}

int main()
{
char s1[100];
printf("please input string:");
gets(s1);
inverse(s1);
printf("inverse srting:%s", s1);
printf("\n");
}

指针排两大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>

void swap(int *p1, int *p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
}
int main()
{
int a, b;
int *pointer_1,*pointer_2;
printf("a and b:");
scanf("%d %d", &a, &b);
pointer_1 = &a;
pointer_2 = &b;
if (a < b) {
swap(pointer_1, pointer_2);
}
printf("max=%d,min=%d", a, b);
printf("\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
#include <stdio.h>

void swap(int *p1, int *p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
}

int main()
{
int a, b, c;
int *s1, *s2, *s3;
printf("输入3个数:");
scanf("%d %d %d", &a, &b, &c);
s1 = &a;
s2 = &b;
s3 = &c;
if (*s1 > *s2)
{
swap(s1, s2);
}
if (*s1 > *s3)
{
swap(s1, s3);
}
if (*s2 > *s3)
{
swap(s2, s3);
}
printf("小到大为: %d %d %d", a, b, c);
printf("\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
#include <stdio.h>

int main()
{
int a, b;
double c;
char op;
printf("整数a和b和运算符:");
scanf("%d %d", &a, &b);
printf("输入运算符:");
scanf(" %c", &op);

switch (op)
{
case '+':
printf("a+b=%d\n", (a + b));
break;
case '-':
printf("a-b=%d\n", (a - b));
break;
case '*':
printf("a*b=%d\n", (a * b));
break;
case '/':
if (b == 0)
{
printf("除数不能为零!\n");
}
else
{
printf("a/b=%.2f\n",(double)a / (double)b);
}
break;
default:
printf("输入的运算符无效!\n");
break;
}
}

判断闰年

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main()
{
int year;
printf("请输入年份:");
scanf("%d", &year);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
printf("%d 是闰年\n", year);
else
printf("%d 不是闰年\n", year);
return 0;
}
由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
总字数 9.6k