1.筛求法求素数 有问题

  1. //筛求法 求素数
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. using namespace std;
  6. int main()
  7. {
  8. int i, j, a[101];
  9. //赋初值
  10. for (i = 1; i < 101; i++)
  11. a[i] = i;
  12. a[1] = 0;
  13. for (i = 2; i < sqrt(100); i++)
  14. {
  15. for (j = i + 1; j <= 100; j++)
  16. {
  17. if (a[i] != 0 && a[j] != 0)
  18. {
  19. if (a[j] % a[i] == 0)
  20. {
  21. a[j] = 0;
  22. }
  23. }
  24. }
  25. }
  26. for (i = 1; i <= 100; i++)
  27. {
  28. if (a[i] != 0)
  29. {
  30. cout << setw(3) << i;
  31. }
  32. }
  33. return 0;
  34. }

自己的思路:素数还可以乘出来!!!!

  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. // 自己的想法
  8. int arr[101], k;
  9. for (int i = 0; i < 101; i++)
  10. {
  11. arr[i] = 1;
  12. }
  13. arr[1] = 0;
  14. for (int i = 2; i < sqrt(100); i++)
  15. {
  16. if (arr[i] == 1)
  17. {
  18. k = 2;
  19. while ((i * k) <= 100)
  20. {
  21. arr[i * k] = 0;
  22. k++;
  23. }
  24. }
  25. }
  26. for (int i = 1; i < 101; i++)
  27. {
  28. if (arr[i] == 1)
  29. {
  30. cout << setw(4) << i;
  31. }
  32. }
  33. return 0;
  34. }

思路:

image.png

2.选择排序

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. int arrary[] = {5, 1, 9, 4, 0, 6, 3, 8, 7, 2};
  7. int i, j, min, temp;
  8. cout << sizeof(arrary) / sizeof(int) << endl;
  9. for (i = 0; i < 9; i++)
  10. {
  11. min = i;
  12. for (j = i + 1; j < 10; j++)
  13. {
  14. if (arrary[min] > arrary[j])
  15. {
  16. min = j;
  17. }
  18. }
  19. if (i != min)
  20. {
  21. temp = arrary[min];
  22. arrary[min] = arrary[i];
  23. arrary[i] = temp;
  24. }
  25. }
  26. for (i = 0; i < 10; i++)
  27. {
  28. cout << setw(3) << setiosflags(ios::left) << arrary[i];
  29. }
  30. return 0;
  31. }

3.求3x3的矩阵对角线之和

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //初始化数组
  6. int arrary[3][3] = {{1}, {0, 1}, {0, 0, 1}};
  7. int sum, i;
  8. for (i = 0; i < 3; i++)
  9. {
  10. sum += arrary[i][i];
  11. }
  12. cout << sum << endl;
  13. return 0;
  14. }

4.插入元素

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. int arr[10] = {1, 2, 3, 5, 6, 7, 8};
  7. //p - position位置
  8. int i, p, n = 9;
  9. //1.找到对应的位置 相同数字插入后面。(边界值问题!!!) 没有处理好
  10. if (n < arr[0])
  11. {
  12. p = 0;
  13. }
  14. else
  15. {
  16. for (i = 0; i < 7; i++)
  17. {
  18. if (arr[i] <= n)
  19. {
  20. p = i + 1;
  21. }
  22. }
  23. }
  24. cout << "p" << p;
  25. //2.插入相应的数字
  26. //分类讨论
  27. //(1)p>=size arr[p] = n;
  28. //(2) p<size 移动数字 插入
  29. if (p >= 7)
  30. {
  31. arr[p] = n;
  32. }
  33. else
  34. {
  35. for (i = 7; i >= p; i--)
  36. {
  37. arr[i] = arr[i - 1];
  38. }
  39. arr[p] = n;
  40. }
  41. for (i = 0; i < 10; i++)
  42. {
  43. cout << setw(3) << arr[i];
  44. }
  45. return 0;
  46. }

另外一种写法,值得学习一下,其中break写的很灵性。

  1. // 数组按照顺序插入数字
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. int a[10] = {1, 6, 13, 22, 28, 66, 76, 98, 100};
  8. int i, j, n;
  9. cin >> n;
  10. if (n > a[8])
  11. {
  12. a[9] = n;
  13. }
  14. else
  15. {
  16. for (i = 0; i < 10; i++)
  17. {
  18. if (a[i] > n)
  19. {
  20. for (j = 8; j >= i; j--)
  21. {
  22. a[j + 1] = a[j];
  23. }
  24. a[i] = n;
  25. break;
  26. }
  27. }
  28. }
  29. for (i = 0; i < 10; i++)
  30. {
  31. cout << setw(5) << a[i] << " ";
  32. }
  33. system("pause");
  34. return 0;
  35. }

5.数组中的值逆序存放

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. //int arr[] = {1, 2, 3, 4, 5};
  7. int arr[] = {1, 2, 3, 4, 5, 6};
  8. int start = 0, end = 5, temp;
  9. while (start < end)
  10. {
  11. temp = arr[start];
  12. arr[start++] = arr[end];
  13. arr[end--] = temp;
  14. }
  15. for (int i = 0; i < 6; i++)
  16. {
  17. cout << setw(3) << arr[i];
  18. }
  19. return 0;
  20. }

方法二:

  1. //5-5数组逆序
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. int i, j, n, temp;
  8. int a[20];
  9. i = j = n = 0;
  10. //输入数组
  11. while (cin >> a[n])
  12. {
  13. n++;
  14. }
  15. //展示数组
  16. for (i = 0; i < n; i++)
  17. {
  18. cout << setw(3) << a[i] << ",";
  19. }
  20. cout << endl;
  21. // 反转数组
  22. for (i = 0; i < n; i++)
  23. {
  24. if (i > (n - 1 - i))
  25. {
  26. break;
  27. }
  28. temp = a[i];
  29. a[i] = a[n - 1 - i];
  30. a[n - 1 - i] = temp;
  31. }
  32. //展示数组
  33. for (i = 0; i < n; i++)
  34. {
  35. cout << setw(3) << a[i] << ",";
  36. }
  37. system("pause");
  38. return 0;
  39. }

image.png

6.杨辉三角

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. const int n = 10; //学习一下
  6. int a[n][n];
  7. int i, j;
  8. for (i = 0; i <= n - 1; i++)
  9. {
  10. for (j = 0; j <= i; j++)
  11. {
  12. if (j == 0 || j == i)
  13. {
  14. a[i][j] = 1;
  15. }
  16. else
  17. a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
  18. cout << a[i][j] << "\t";
  19. }
  20. cout << "\n";
  21. }
  22. return 0;
  23. }

image.png

7.寻找鞍点

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. int const n = 3;
  5. //定义数组
  6. int arr[n][n];
  7. int i,j,rmax,cmin;
  8. //定位行
  9. for(i=0;i<n;i++){
  10. //寻找本行最大的数,初始化rmax
  11. rmax = 0;
  12. for(j=0;j<n;j++){
  13. if(arr[i][rmax]<arr[i][j]){
  14. rmax = j ;
  15. }
  16. }
  17. //寻找rmax这列最小的,初始化cmin
  18. cmin = 0;
  19. for(j=0;j<n;j++){
  20. if(arr[rmax][j]<arr[rmax][cmin]){
  21. cmin = j;
  22. }
  23. }
  24. //判断是不是鞍点
  25. if(cmin!=i){
  26. cout<<""<<endl;
  27. }else{
  28. cout<<"找到鞍点"<<"arr["<<i<<"]["<<rmax<<"]="<<arr[i][rmax]<<endl;
  29. }
  30. return 0;
  31. }

8.对半查找

自己的思路

  1. //二分查找
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int find(int *a, int start, int end, int n);
  7. int binarySearch(int a[], int target, int len);
  8. int n;
  9. int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  10. cin >> n;
  11. // int flag = find(a, 1, 9, n);
  12. int flag = binarySearch(a, n, 10);
  13. if (flag == -1)
  14. {
  15. cout << "can not find it" << endl;
  16. }
  17. else
  18. {
  19. cout << "At" << flag << endl;
  20. }
  21. system("pause");
  22. return 0;
  23. }
  24. int find(int *a, int start, int end, int n)
  25. {
  26. int mind = (end + start) / 2;
  27. if (start >= end)
  28. return a[mind] == n ? mind : 0;
  29. if (a[mind] > n)
  30. {
  31. return find(a, start, mind - 1, n);
  32. }
  33. else if (a[mind] == n)
  34. {
  35. return n;
  36. }
  37. else
  38. {
  39. return find(a, mind + 1, end, n);
  40. }
  41. return -1;
  42. }
  43. int binarySearch(int a[], int target, int len)
  44. {
  45. int left = 0;
  46. int right = len - 1;
  47. while (left <= right)
  48. {
  49. int mind = (left + right) / 2;
  50. if (a[mind] == target)
  51. {
  52. return mind;
  53. }
  54. else if (a[mind] > target)
  55. {
  56. right = mind - 1;
  57. }
  58. else if (a[mind] < target)
  59. {
  60. left = mind + 1;
  61. }
  62. }
  63. return -1;
  64. }

image.png

image.png

经典递归框架

  1. int binarySearch(int a[], int target, int len)
  2. {
  3. int left = 0;
  4. int right = len - 1;
  5. while (left <= right)
  6. {
  7. int mind = (left + right) / 2;
  8. if (a[mind] == target)
  9. {
  10. return mind;
  11. }
  12. else if (a[mind] > target)
  13. {
  14. right = mind - 1;
  15. }
  16. else if (a[mind] < target)
  17. {
  18. left = mind + 1;
  19. }
  20. }
  21. return -1;
  22. }

9.拼接字符串

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char s1[100], s2[100];
  6. int i = 0;
  7. int j = 0;
  8. cout << "input s1" << endl;
  9. cin >> s1;
  10. cout << "input s1" << endl;
  11. cin >> s2;
  12. while (s1[i] != '\0')
  13. {
  14. i++;
  15. }
  16. while (s2[j] != '\0')
  17. {
  18. s1[i++] = s2[j++];
  19. }
  20. // 最后写入'\0'很重要
  21. s1[i] = '\0';
  22. cout << s1 << endl;
  23. system("pause");
  24. return 0;
  25. }

10.输入n个字符串,将他们按照字符大小排序

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. void sort(char []);
  5. char str[100]="qazwsxedcrfvtgbyhnujmikolp";
  6. sort(str);
  7. cout<<str<<endl;
  8. return 0;
  9. }
  10. void sort(char str[]){
  11. int n=0;
  12. char temp;
  13. //寻找字符串长度
  14. while(str[n++]!='\0');
  15. n--;
  16. cout<<"n:"<<n<<endl;
  17. //冒泡
  18. for(int i=0;i<n-1;i++){
  19. for(int j=0;j<n-1-i;j++){
  20. if((str[j]>str[j+1])){
  21. temp = str[j];
  22. str[j] = str[j+1];
  23. str[j+1] = temp;
  24. }
  25. }
  26. }
  27. }

问题:

  1. 冒泡排序不熟练。
  2. 代码有些地方存在不规范,多次修改之后才能能正确运行。

11.输入n个字符串,将以A打头的输出

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. string str;
  5. const int n = 5;
  6. for(int i=0;i<n;i++){
  7. cin>>str;
  8. if(str[0]='A'){
  9. cout<<str<<endl;
  10. }
  11. }
  12. return 0 ;
  13. }

16.将字符串逆序输出

1.字符串数组法

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. void revert(char * str);
  5. char str[] = "summer2077";
  6. revert(str);
  7. cout<<str<<endl;
  8. return 0;
  9. }
  10. void revert(char * str){
  11. int n=0;
  12. char temp;
  13. while(str[n++]!='\0');
  14. n--;
  15. for(int i=0;i<(n/2);i++){
  16. temp = str[i];
  17. str[i] = str[n-i-1];
  18. str[n-i-1] = temp;
  19. }
  20. }

2.string(和上面的没有区别)

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. void revert(char * str);
  5. char str = "abcdefghigklmnopqrsyuvwxyz";
  6. revert(str);
  7. cout<<str<<endl;
  8. return 0;
  9. }