C++编程学习阶段性总结
例1:用筛法求100以内的素数。
思路:筛法即为埃拉托斯特尼筛法:如果当前数字是素数(即布尔数组中标记为true),则将其所有的倍数标记为非素数(false)。
#include <iostream>
#include <cmath>
using namespace std;int main()
{bool is_prime[101];for (int i = 0; i <= 100; i++)is_prime[i] = true;is_prime[0] = false;is_prime[1] = false;for (int i = 2; i * i <= 100; i++){if (is_prime[i]){for (int j = i * i; j <= 100; j += i)is_prime[j] = false;}}cout << "100以内所有素数为:";for (int i = 2; i <= 100; i++)if (is_prime[i])cout << i << " ";
}//结果为:100以内所有素数为:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
例2:用选择法对10个整数排序
思路:先求出数组中最小的值,与数组中第一个值互换,再依次求出剩余数据最小的的,与第二个互换,依次类推,对数组数据进行排序后输出。
#include <iostream>
using namespace std;int main()
{int a[10];void sort_num(int a[]);cout << "请输入10个整数:";for (int i = 0; i < 10; i++)cin >> a[i];sort_num(a);cout << "排序结果为:";for (int i = 0; i < 10; i++)cout << a[i] << " ";cout << endl;return 0;
}void sort_num(int a[])
{int i, j, t;for (i = 0; i < 10; i++)for (j = i + 1; j < 10; j++)if (a[i] > a[j]){t = a[i];a[i] = a[j];a[j] = t;}
}
结果如下:
例3:求一个3x3矩阵对角线元素之和
思路:主对角线元素分别为a[i][i],副对角线元素为a[i][3-1-i],本次只求主对角线元素之和。
#include <iostream>
using namespace std;int main()
{int i, j, sum = 0, a[3][3];cout << "请输入九个数据:";for (i = 0; i < 3; i++)for (j = 0; j < 3; j++)cin >> a[i][j];for (i = 0; i < 3; i++)sum = sum + a[i][i];cout << "对角线元素之和为:" << sum << endl;return 0;
}
结果如下:
例4:逆序重新存放数组中的值
思路:对于数组a[n],将a[n-1]的值赋给b[0],a[n-2]的值赋给b[1],依次类推,最后输出数组b的数据。
#include <iostream>
using namespace std;
const int NUM = 6;int main()
{int i, j, a[NUM], b[NUM];cout << "请输入六个数据:";for (i = 0; i < NUM; i++)cin >> a[i];cout << "排序后的数据为:";for (j = 0; j < NUM; j++){b[j] = a[NUM - 1 - j];cout << b[j] << " ";}return 0;
}
结果如下:
例5:打印杨辉三角前十行数据
思路:杨辉三角的特性为:每行首尾元素为1,其余元素为上行同一列元素+上行前一列元素,第一行为1,第二行为1,1。
#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS = 10;int main()
{int i, j;int yanghui[ROWS][ROWS];for (i = 0; i < ROWS; i++){yanghui[i][0] = 1;yanghui[i][i] = 1;for (j = 1; j < i; j++)yanghui[i][j] = yanghui[i - 1][j] + yanghui[i - 1][j - 1];}for (i = 0; i < ROWS; i++){cout << setw((ROWS - i) * 2) << "";for (j = 0; j <= i; j++)cout << setw(3) << yanghui[i][j]<<" ";cout << "\n" << endl;}return 0;
}
结果如下:
例6:输入年、月、日,然后计算这一天是该年的第几天
思路:将十二个月的天数存放在一个数组中,先判断该年是否为闰年,如果是,则将二月置为29天,后面计算此月前面月份总和加上当月天数
#include <iostream>
using namespace std;int main()
{int year, month, day, i, j, sum = 0;int months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };cout << "请输入年份:";cin >> year;cout << "请输入月份:";cin >> month;cout << "请输入日期:";cin >> day;if (month < 1 || month>12 || day < 1 || day>31){cout << "输入的日期不合法!" << endl;return 1;}if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //year % 4 != 0 || (year % 100 == 0 && year % 400 != 0)months[1] = 29;for (i = 0; i < month-1; i++)sum = sum + months[i];sum += day;cout << year << "年" << month << "月" << day << "日是当年的第" << sum << "天" << endl;return 0;
}
结果如下:
例7:输入n个字符串,将它们按字母由小到大的顺序排列并输出
思路:定义字符串数组,对字符串进行排序,输出字符串数组
#include <iostream>
#include <string>
const int NUM = 6;
using namespace std;int main()
{int i, j, n;string temp, str[NUM];cout << "请输入" << NUM << "个字符串:";for(int i=0;i<NUM;i++)cin >> str[i];cout << "排序后字符串为:";for (i = 0; i < NUM; i++){for (j = i + 1; j < NUM; j++)if (str[i] > str[j]){temp = str[i];str[i] = str[j];str[j] = temp;}cout << str[i] << " ";}cout << endl;return 0;
}
结果如下:
例8:输入10个学生的姓名、学号和成绩,将其中不及格的姓名、学号和成绩输出
思路:定义一个10x3的二维数组存储学生数据,遍历数组元素,比较分数是否小于60,输出小于60分的学生信息。其中比较分数时,个位数和100的分数需要单独处理。
#include <iostream>
#include <string>
using namespace std;int main()
{bool result = false, found=false;int i, j;string stu_message[10][3], score="60", best="100";cout << "请输入所有学生的信息"<< endl;for (i = 0; i < 10; i++){cout << "第" << i + 1 << "个学生姓名、学号和成绩:";for (j = 0; j < 3; j++)cin >> stu_message[i][j];}cout << "不及格学生统计:"<< endl;for (i = 0; i < 10; i++){for (j = 0; j < 3; j++){if (stu_message[i][2] != best){if (stu_message[i][2] < score || stu_message[i][2].length() == 1){cout << stu_message[i][j] << " ";result = true;found = true;}elseresult = false;}else result = false;}if (result)cout << endl;}if (!found)cout << "没有不及格学生!";return 0;
}
结果如下: