//函数打印素数
#include <iostream>
using namespace std;
int is_prime(int n){
for(int j=2;j<n;j++){
if(n%j==0)
return 0;
}
return 1;
}
int main(){
int i=0;
for(int i=100;i<=200;i++){
if(is_prime(i)==1)
printf("%d ",i);
}
return 0;
}
//函数判断闰年
#include <iostream>
using namespace std;
int is_leap_year(int y){
if(y%4==0&&y%100!=0||(y%400==0)){
return 1;
}
else
return 0;
}
int main(){
int year=0;
for(year=1000;year<=2000;year++){
if(is_leap_year(year)==1)
printf("%d ",year);
}
return 0;
}
//函数二分查找
#include <iostream>
using namespace std;
int binary_search(int arr[], int size, int k) {
int left = 0;
int right = size - 1;
while (left <= right) {
int mid = (left + right) / 2;//中间元素的下标
if (arr[mid] < k) {
left = mid + 1;
} else if (arr[mid] > k) {
right = mid - 1;
} else {
return mid;
}
}
return -1;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 7;//查找数字7,最终找到下标6
int size = sizeof(arr) / sizeof(arr[0]);
int result = binary_search(arr, size, k);
if (result == -1) {
cout << "No corresponding number found" << endl;
} else {
cout << "Find the corresponding number at index " << result << endl;
}
return 0;
}
//函数的调用将将num的值增加1
#include <iostream>
using namespace std;
void add(int*p){
(*p)++;
}
int main(){
int num=0;
add(&num);
printf("num=%d\n",num);
add(&num);
printf("num=%d\n",num);
add(&num);
printf("num=%d\n",num);
return 0;
}