1.数组运算
![](https://i-blog.csdnimg.cn/blog_migrate/0d5ae688fa766ff78c6053b2107ec9b2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b1aab7d8dc156844457f5e9310f5ec80.png)
若数组未进行初始化
![](https://i-blog.csdnimg.cn/blog_migrate/e4b75de0e748d8f5069014534528a6bf.png)
给数组中的第一个初始化,后面全初始为0
![](https://i-blog.csdnimg.cn/blog_migrate/67dfce2c5fc338375fdc7d56a0f236fc.png)
给数组中指定位置赋值
![](https://i-blog.csdnimg.cn/blog_migrate/e5ea32fa342db2c519266e3606c85ca9.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cd78b4dce6e087a4dbb1bb97f5cd3f67.png)
![](https://i-blog.csdnimg.cn/blog_migrate/69ba6d70dafac1ca3add0a69361c6bfc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c26782fa710c66eef84db6e954f5fd1d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a17389ac40fe9b9362ef7ef32ee55e95.png)
例:判断数字在不在数组中
#include <stdio.h>
int f(int x, int a[], int length);
int main(void)
{
int x = 0;
int a[] = { 10,1,4,3,2,5,6,7,8,9 };
int loc = 0;
printf("请输入一个数字");
scanf_s("%d", &x);
loc = f(x, a, sizeof(a) / sizeof(a[0]));
if (loc != -1) {
printf("数字%d在数组中的位置为第%d位", x, loc);
}
else {
printf("这个数字在数组中不存在");
}
return 0;
}
int f(int x, int a[], int length)
{
int i = 0;
int ret = -1;
for (i = 0; i < length; i++) {
if (a[i] == x) {
ret = i;
}
}
return ret;
}
![](https://i-blog.csdnimg.cn/blog_migrate/c6e0150999605453f2cd84f010211df6.png)
构造素数表 1.构造100个素数
#include <stdio.h>
int sushu(int x);
int main()
{
int a[100] = { 2 };
int i = 0;
int count = 1;
for (i = 3; count < 100; i++) {
if (sushu(i) == 1) {
a[count] = i;
count++;
}
}
int f = 0;
for (f = 0; f < 100; f++) {
printf("%d\t", a[f]);
}
}
int sushu(int x)
{
int i = 0;
int ret = 1;
for (i = 2; i < x; i++) {
if (x % i == 0) {
ret = 0;
}
}
return ret;
}
![](https://i-blog.csdnimg.cn/blog_migrate/00c987b8c5b68f0fab492ef8ba99b7fb.png)
2.构造100以内的素数
![](https://i-blog.csdnimg.cn/blog_migrate/086a41de629b3b9fc83dbfb751e28f35.png)
#include <stdio.h>
int sushu(int x);
int main()
{
int prime[100];
int i = 0;
int count = 0;
for (i = 0; i < 100; i++) {
prime[i] = 1;
}
int x = 2;
for (x = 2; count < 100; x++) {
if (sushu(x) == 1) {
int i = 2;
for (i = 2; i * x < 100; i++) {
prime[i * x] = 0;
count++;
}
}
}
int k = 0;
for (k = 2; k < 100; k++) {
if (prime[k] == 1) {
printf("%d\t",k);
}
}
return 0;
}
int sushu(int x)
{
int i = 0;
int ret = 1;
for (i = 2; i < x; i++) {
if (x % i == 0) {
ret = 0;
}
}
return ret;
}
二分搜索
#include <stdio.h>
int search(int x, int a[], int length);
int main()
{
int x = 0;
int a[] = { 2,6,9,19,27,37,49,54,67,86,96 };
printf("请输入数字:");
scanf_s("%d", &x);
int result = search(x, a, sizeof(a) / sizeof(a[0]));
if (result != -1) {
printf("数字%d在第%d位", x, result);
}
else {
printf("在数组中没有找到这个数");
}
return 0;
}
int search(int x, int a[], int length)
{
int ret = -1;
int left = 0;
int right = length - 1;
while (right > left) {
int mid = (right + left) / 2;
if (a[mid] < x) {
left = mid + 1;
}
else if (a[mid] > x) {
right = mid - 1;
}
else {
ret = mid;
break;
}
}
return ret;
}
选择排序
#include <stdio.h>
int max(int a[], int length);
int main()
{
int a[] = { 2,12,45,32,13,42,65,69,86 };
int length = sizeof(a) / sizeof(a[0]);
int f = length-1;
for (f=length-1; f > 0; f--) {
int maxid = max(a, f+1);
int t = a[f];
a[f] = a[maxid];
a[maxid] = t;
}
int i = 0;
for (i = 0; i < length; i++) {
printf("%d\t", a[i]);
}
}
int max(int a[], int length)
{
int maxid = 0;
int i = 0;
for (i = 0; i < length; i++) {
if (a[i] > a[maxid]) {
maxid = i;
}
}
return maxid;
}