1.筛求法求素数 有问题
//筛求法 求素数#include <iostream>#include <iomanip>#include <cmath>using namespace std;int main(){ int i, j, a[101]; //赋初值 for (i = 1; i < 101; i++) a[i] = i; a[1] = 0; for (i = 2; i < sqrt(100); i++) { for (j = i + 1; j <= 100; j++) { if (a[i] != 0 && a[j] != 0) { if (a[j] % a[i] == 0) { a[j] = 0; } } } } for (i = 1; i <= 100; i++) { if (a[i] != 0) { cout << setw(3) << i; } } return 0;}
自己的思路:素数还可以乘出来!!!!
#include <iostream>#include <cmath>#include <iomanip>using namespace std;int main(){ // 自己的想法 int arr[101], k; for (int i = 0; i < 101; i++) { arr[i] = 1; } arr[1] = 0; for (int i = 2; i < sqrt(100); i++) { if (arr[i] == 1) { k = 2; while ((i * k) <= 100) { arr[i * k] = 0; k++; } } } for (int i = 1; i < 101; i++) { if (arr[i] == 1) { cout << setw(4) << i; } } return 0;}
思路:

2.选择排序
#include <iostream>#include <iomanip>using namespace std;int main(){ int arrary[] = {5, 1, 9, 4, 0, 6, 3, 8, 7, 2}; int i, j, min, temp; cout << sizeof(arrary) / sizeof(int) << endl; for (i = 0; i < 9; i++) { min = i; for (j = i + 1; j < 10; j++) { if (arrary[min] > arrary[j]) { min = j; } } if (i != min) { temp = arrary[min]; arrary[min] = arrary[i]; arrary[i] = temp; } } for (i = 0; i < 10; i++) { cout << setw(3) << setiosflags(ios::left) << arrary[i]; } return 0;}
3.求3x3的矩阵对角线之和
#include <iostream>using namespace std;int main(){ //初始化数组 int arrary[3][3] = {{1}, {0, 1}, {0, 0, 1}}; int sum, i; for (i = 0; i < 3; i++) { sum += arrary[i][i]; } cout << sum << endl; return 0;}
4.插入元素
#include <iostream>#include <iomanip>using namespace std;int main(){ int arr[10] = {1, 2, 3, 5, 6, 7, 8}; //p - position位置 int i, p, n = 9; //1.找到对应的位置 相同数字插入后面。(边界值问题!!!) 没有处理好 if (n < arr[0]) { p = 0; } else { for (i = 0; i < 7; i++) { if (arr[i] <= n) { p = i + 1; } } } cout << "p" << p; //2.插入相应的数字 //分类讨论 //(1)p>=size arr[p] = n; //(2) p<size 移动数字 插入 if (p >= 7) { arr[p] = n; } else { for (i = 7; i >= p; i--) { arr[i] = arr[i - 1]; } arr[p] = n; } for (i = 0; i < 10; i++) { cout << setw(3) << arr[i]; } return 0;}
另外一种写法,值得学习一下,其中break写的很灵性。
// 数组按照顺序插入数字#include <iostream>#include <iomanip>using namespace std;int main(){ int a[10] = {1, 6, 13, 22, 28, 66, 76, 98, 100}; int i, j, n; cin >> n; if (n > a[8]) { a[9] = n; } else { for (i = 0; i < 10; i++) { if (a[i] > n) { for (j = 8; j >= i; j--) { a[j + 1] = a[j]; } a[i] = n; break; } } } for (i = 0; i < 10; i++) { cout << setw(5) << a[i] << " "; } system("pause"); return 0;}
5.数组中的值逆序存放
#include <iostream>#include <iomanip>using namespace std;int main(){ //int arr[] = {1, 2, 3, 4, 5}; int arr[] = {1, 2, 3, 4, 5, 6}; int start = 0, end = 5, temp; while (start < end) { temp = arr[start]; arr[start++] = arr[end]; arr[end--] = temp; } for (int i = 0; i < 6; i++) { cout << setw(3) << arr[i]; } return 0;}
方法二:
//5-5数组逆序#include <iostream>#include <iomanip>using namespace std;int main(){ int i, j, n, temp; int a[20]; i = j = n = 0; //输入数组 while (cin >> a[n]) { n++; } //展示数组 for (i = 0; i < n; i++) { cout << setw(3) << a[i] << ","; } cout << endl; // 反转数组 for (i = 0; i < n; i++) { if (i > (n - 1 - i)) { break; } temp = a[i]; a[i] = a[n - 1 - i]; a[n - 1 - i] = temp; } //展示数组 for (i = 0; i < n; i++) { cout << setw(3) << a[i] << ","; } system("pause"); return 0;}

6.杨辉三角
#include <iostream>using namespace std;int main(){ const int n = 10; //学习一下 int a[n][n]; int i, j; for (i = 0; i <= n - 1; i++) { for (j = 0; j <= i; j++) { if (j == 0 || j == i) { a[i][j] = 1; } else a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; cout << a[i][j] << "\t"; } cout << "\n"; } return 0;}

7.寻找鞍点
#include<iostream>using namespace std;int main(){ int const n = 3; //定义数组 int arr[n][n]; int i,j,rmax,cmin; //定位行 for(i=0;i<n;i++){ //寻找本行最大的数,初始化rmax rmax = 0; for(j=0;j<n;j++){ if(arr[i][rmax]<arr[i][j]){ rmax = j ; } } //寻找rmax这列最小的,初始化cmin cmin = 0; for(j=0;j<n;j++){ if(arr[rmax][j]<arr[rmax][cmin]){ cmin = j; } } //判断是不是鞍点 if(cmin!=i){ cout<<""<<endl; }else{ cout<<"找到鞍点"<<"arr["<<i<<"]["<<rmax<<"]="<<arr[i][rmax]<<endl; } return 0; }
8.对半查找
自己的思路
//二分查找#include <iostream>using namespace std;int main(){ int find(int *a, int start, int end, int n); int binarySearch(int a[], int target, int len); int n; int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; cin >> n; // int flag = find(a, 1, 9, n); int flag = binarySearch(a, n, 10); if (flag == -1) { cout << "can not find it" << endl; } else { cout << "At" << flag << endl; } system("pause"); return 0;}int find(int *a, int start, int end, int n){ int mind = (end + start) / 2; if (start >= end) return a[mind] == n ? mind : 0; if (a[mind] > n) { return find(a, start, mind - 1, n); } else if (a[mind] == n) { return n; } else { return find(a, mind + 1, end, n); } return -1;}int binarySearch(int a[], int target, int len){ int left = 0; int right = len - 1; while (left <= right) { int mind = (left + right) / 2; if (a[mind] == target) { return mind; } else if (a[mind] > target) { right = mind - 1; } else if (a[mind] < target) { left = mind + 1; } } return -1;}

经典递归框架
int binarySearch(int a[], int target, int len){ int left = 0; int right = len - 1; while (left <= right) { int mind = (left + right) / 2; if (a[mind] == target) { return mind; } else if (a[mind] > target) { right = mind - 1; } else if (a[mind] < target) { left = mind + 1; } } return -1;}
9.拼接字符串
#include <iostream>using namespace std;int main(){ char s1[100], s2[100]; int i = 0; int j = 0; cout << "input s1" << endl; cin >> s1; cout << "input s1" << endl; cin >> s2; while (s1[i] != '\0') { i++; } while (s2[j] != '\0') { s1[i++] = s2[j++]; } // 最后写入'\0'很重要 s1[i] = '\0'; cout << s1 << endl; system("pause"); return 0;}
10.输入n个字符串,将他们按照字符大小排序
#include<iostream>using namespace std;int main(){ void sort(char []); char str[100]="qazwsxedcrfvtgbyhnujmikolp"; sort(str); cout<<str<<endl; return 0;}void sort(char str[]){ int n=0; char temp; //寻找字符串长度 while(str[n++]!='\0'); n--; cout<<"n:"<<n<<endl; //冒泡 for(int i=0;i<n-1;i++){ for(int j=0;j<n-1-i;j++){ if((str[j]>str[j+1])){ temp = str[j]; str[j] = str[j+1]; str[j+1] = temp; } } }}
问题:
- 冒泡排序不熟练。
- 代码有些地方存在不规范,多次修改之后才能能正确运行。
11.输入n个字符串,将以A打头的输出
#include<iostream>using namespace std;int main(){ string str; const int n = 5; for(int i=0;i<n;i++){ cin>>str; if(str[0]='A'){ cout<<str<<endl; } } return 0 ;}
16.将字符串逆序输出
1.字符串数组法
#include<iostream>using namespace std;int main(){ void revert(char * str); char str[] = "summer2077"; revert(str); cout<<str<<endl; return 0;}void revert(char * str){ int n=0; char temp; while(str[n++]!='\0'); n--; for(int i=0;i<(n/2);i++){ temp = str[i]; str[i] = str[n-i-1]; str[n-i-1] = temp; }}
2.string(和上面的没有区别)
#include<iostream>using namespace std;int main(){ void revert(char * str); char str = "abcdefghigklmnopqrsyuvwxyz"; revert(str); cout<<str<<endl; return 0;}