正三角(反三角)

使用为两个for循环进行嵌套

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{
int a,b,c;
for(a=0;a<6;a++)
{
for(b=0;b<a;b++)
{
printf("$");
}
printf("\n");
}

return 0;
}

金字塔

使用为四个for进行循环思想核心为中间数量为行数n的两倍减去1,外层循环控制换行,内层三个控制空格以及他的打印数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main()
{
int rows,i,j,space,star;

scanf("%d", & rows);
for(int i=1;i<=rows;++i)
{
star = 2*i -1 ; //星号个数
space = rows - i; //空格个数
for(int j=0;j<space;++j)//左边空格
printf(" ");
for(int j=0;j<star;++j) //中间 *
printf("*");
for(int j=0;j<space;++j)//右边空格有没有都可以
printf(" ");
printf("\n");//换行
}

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
#include <stdio.h>
int main()
{
int i, space, rows, k=0;

printf("Enter number of rows: ");
scanf("%d",&rows);

for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}

while(k != 2*i-1)
{
printf("* ");
++k;
}

printf("\n");
}

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>
int main()
{
int i,x;
scanf("%d",&x);
if(x==2) puts("yes!");
if(x>2)
{
for(i=2;i<x;i++)
if(x%i==0)
{
puts("no!");
break;//一旦判断不是,跳出循环
}
if(i==x) puts("yes!");//如果是的话,for循环一定执行到了i=x
}
else puts("no!");

return 0
}

C语言二分查找

找到最左边元素(low)和最右边元素(high),确定中间元素(mid),比较中间元素(mid)和目标元素(k)的大小,调整low和high,再确定新的mid….我们要不断确定mid直到找到k,自然需要用到循环,我们有明确的目标:找到k。因此选择while循环,找到k后循环不再进行,而当low和high之间还有元素,即low在high的左边或与之重合,k就依然可能存在,所以循环条件为low<=high,接下来的问题在于怎样调整low和high的值,mid和k比较无非就三种情况:mid<k,mid>k,mid=k。第一种情况,k在mid的右边,我们将low调整为mid+1,high不用调整;第二种情况,k在mid的左边,我们将high调整为mid-1,low不用调整。最后一种情况最简单,我们已经找到了k,直接将mid打印出来就行了

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
40
41
42
43
#include <stdio.h>

int Bin_Search(int *num,int cnt,int target)
{
int first = 0,last = cnt-1,mid;
int counter = 0;
while(first <= last)
{
counter ++;
mid = (first + last) / 2;//确定中间元素
if(num[mid] > target)
{
last = mid-1; //mid已经交换过了,last往前移一位
}
else if(num[mid] < target)
{
first = mid+1;//mid已经交换过了,first往后移一位
}
else //判断是否相等
{
printf("查找次数:%d\n",counter);
return 1;
}
}
printf("查找次数:%d\n",counter);
return 0;
}

int main(void)
{
int flag = 0,target;
int num[10] = {1,2,3,4,5,6,7,8,10};
while(1)
{
printf("请输入您要查找的数字:\n");
scanf("%d",&target);
flag = Bin_Search(num,10,target);
if(flag) printf("已经找到该数字!!\n");
else printf("无该数字!!\n");
}
return 0;
}

查找数组最大值

循环对比

1
2
3
4
5
6
7
8
9
10
findMaxValue(arr) {
int max = 0; // 最大值
for (int i=0; i<arr.length; ++i) {
if (arr[i] > max) { // 当前值大于最大值,赋值为最大值
max = arr[i];
}
}
return max;
}

字符串排列顺序

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <string.h>
#define LIM 20
#define SIZE 81
#define HALT ""
void stsrt(char *strings [], int num);
char * s_gets(char * st, int n);
int main()
{
char input[LIM][SIZE];
char *ptstr[LIM];
int ct = 0;
int k;

printf("Input up to %d lines, and I will sort them.\n", LIM);
printf("To stop ,press the Enter key at a line's start.\n");
while(ct < LIM && s_gets(input[ct], SIZE) != NULL && input[ct][0] != '\0')
{
ptstr[ct] = input[ct];
ct ++;
}
stsrt(ptstr, ct);
puts("\nHere's the sorted list:\n");
for( k = 0; k < ct; k++)
puts(ptstr[k]);

return 0;
}

void stsrt(char * strings[], int num)
{
char * temp;
int top, seek;

for(top = 0; top < num - 1; top++)
for(seek = top + 1; seek < num; seek++)
if(strcmp(strings[top], strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st , n , stdin);
if(ret_val)
{
while(st[i] != '\n' && st[i] != '\0')
i++;
if(st[i] == '\n')
st[i] = '\0';
else
while(getchar() != '\n')
continue;
}
return ret_val;
}

冒泡排序

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>
void bubble_sort(int* p, int len)//函数实现
{
int i = 0;
int j = 0;
for (i = 0; i < len - 1; i++)//需要进行len-1趟
{
int flag = 1;//flag=1,说明已经排好序
for (j = 0; j < len - 1 - i;j++)//每趟两两比较较未排好序元素个数-1次。
{
if (p[j] > p[j+1])
{
int tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = tmp;
flag = 0;
}
}
if (flag==1)//判断是否排好序
break;
}
}

int main()
{
int arr[] = { 1,5,9,11,46,79,12 };
int sz = sizeof arr / sizeof arr[0];
bubble_sort(arr,sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}