找1000内素数

Int main(){

 int a[1001] = {0};

 for(int I = 2;i<=1000;i++){

 if(a[i] == 0){

 printf("%d\n"ni);

 for(int j = i+I; j<=1000; j = j+i){

 a[j] = 1;

}}}

Return 0;

}

二分法找峰值

#include<stdio.h>

int feng(int* nums, int n) {

int left = 0;

int right = n - 1;

int mid;

while (left < right) {

mid = (left + right) / 2;

if (nums[mid] >= nums[mid + 1]) {

right = mid;

}

else {

left = mid + 1;

}

}

return left;

}

int main(){

int nums[7] = { 0 };

int n = sizeof(nums) / sizeof(int);

for (int i = 0; i < n ; i++) {

scanf("%d", &nums[i]);

}

int a;

a = feng(nums,n);

printf("%d",a );

return 0;

}

二分法找旋转数组最小值

#include<stdio.h>

int feng(int* nums, int n) {

int left = 0;

int right = n - 1;

int mid;

while (left < right) {

mid = (left + right) / 2;

if (nums[mid] > nums[right]) {

left = mid + 1;

}

else if(nums[mid] < nums[right]){

right = mid;

}

else {

right--;

}

}

return nums[left];

}

int main(){

int nums[5] = { 0 };

int n = sizeof(nums) / sizeof(int);

for (int i = 0; i < n ; i++) {

scanf("%d", &nums[i]);

}

int a;

a = feng(nums,n);

printf("%d",a );

return 0;

}

摩尔投票

int moertoupiao(int* nums,int n) {

int key = nums[0];

int count = 0;

for (int i = 0; i < n; i++) {

if (nums[i] == key) {

count++;

}

else {

count--;

}

if (count <= 0) {

key = nums[i + 1];

}

}

return key;

}

三色旗问题

#include<stdio.h>

void exchange(int* a, int* b) {

int c = *a;

*a = *b;

*b = c;

}

void paixu(int* arr,int a,int b) {

int index = 0;

int i = a - 1;

int j = b + 1;

int temp = 1;

while (index<j) {

if (arr[index]==temp) {

index++;

}

else if(arr[index]<temp){

exchange(&arr[++i],&arr[index]);

index++;

}

else if (arr[index]>temp) {

exchange(&arr[--j],&arr[index]);

}

}

}

int main() {

int arr[] = { 1,2,0,2,2,2,1,1,1,0,0,1,2,0,1,2,0,2,1,0,2,1,2,1 };

int n = sizeof(arr) / sizeof(int);

int i = 0;

    paixu(arr,i, n-1);

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

}

return 0;

}

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信