#C++ Day11 October 21 2025

//6 – 2 – 1 while

#include <iostream>

#include <string>

using namespace std;

int main() {

/*int count = 0;

while (count < 1000) {

cout << “I love you!” << endl;

count += 1;

}*/

/*while (1) {

cout << “I love you!” << endl;

count += 1;

//dead repeat

}*/

//string a;

//while (cin >> a) {

// cout << a+ ” I love you!” << endl;

//}

int a,b;

while (cin >> a >> b) {

cout << a+ b << endl;

}

return 0;

}

//6 – 2 – 2 do-while

#include <iostream>

using namespace std;

int main() {

/*

do {

} while (condition);

*/

//if the condition is true, keep on execution

/*int a = 0;

do {

cout << a << endl;

a += 1;

} while (a < 3);

a = 0;

while (a < 3) {

cout << a << endl;

a += 1;

}*/

int a = 3;

do {

cout << a << ” haha” << endl;

a += 1;

} while (a < 3); 

//do-while will first execute the do case at least once,then continue to  judge the while’s conditon 

cout << “—–” << endl;

a = 3;

//The while will judge the condition at first, if the condition is false, the sentence won’t be executed

while (a < 3) {

cout << a << ” gg” << endl;

a += 1;

}

return 0;

}

AI:

等差数列求和公式详解

要理解等差数列的求和公式,首先需要明确等差数列的基本定义,再通过推导过程掌握公式的本质,最终学会根据已知条件选择合适的公式计算。

一、等差数列的基本概念

在等差数列中,有几个核心量需要先明确,后续公式均围绕这些量展开:

  • 首项:数列的第一个数,记为 ( a_1 )(例如数列 ( 2,5,8,11\cdots ) 中,( a_1=2 ));
  • 公差:相邻两项的差值(后项减前项),记为 ( d )(上述数列中,( d=5-2=3 ),且所有相邻项差值恒定);
  • 项数:数列中元素的个数,记为 ( n )(例如求“前5项和”时,( n=5 ));
  • 末项:数列的第 ( n ) 项,记为 ( a_n )(可由首项和公差推导:( a_n = a_1 + (n-1)d ),上述数列第5项为 ( a_5=2+(5-1)\times3=14 ));
  • 前( n )项和:数列前 ( n ) 个元素的总和,记为 ( S_n )(本文核心求算目标)。

二、等差数列的两个核心求和公式

等差数列求和公式有两种形式,分别适用于“已知末项”和“未知末项”的场景,本质是相通的(可相互推导)。

公式1:已知首项、末项和项数(最直观的形式)

公式表达式:

[ S_n = \frac{n(a_1 + a_n)}{2} ]

推导过程(倒序相加法,高斯求和的本质):

以“求 ( 1+2+3+\cdots+100 ) 的和”为例,高斯的思路可推广到所有等差数列:

  1. 写出前 ( n ) 项和的正序:( S_n = a_1 + (a_1+d) + (a_1+2d) + \cdots + a_n );
  2. 写出前 ( n ) 项和的倒序:( S_n = a_n + (a_n-d) + (a_n-2d) + \cdots + a_1 );
  3. 将正序和倒序对应项相加,每一组的和均为 ( a_1 + a_n )(例如正序第1项+倒序第1项:( a_1+a_n );正序第2项+倒序第2项:( (a_1+d)+(a_n-d)=a_1+a_n ),以此类推);
  4. 总共有 ( n ) 组这样的和,因此:( 2S_n = n(a_1 + a_n) ),两边除以2即得公式1。

适用场景:

已知 ( a_1 )(首项)、( a_n )(末项)、( n )(项数),直接代入计算。

示例:求等差数列 ( 3,7,11,15 ) 的前4项和。

已知 ( a_1=3 ),( a_4=15 ),( n=4 ),代入公式:

( S_4 = \frac{4\times(3+15)}{2} = 4\times9 = 36 )(验证:( 3+7+11+15=36 ),结果正确)。

公式2:已知首项、公差和项数(无需先求末项)

公式表达式:

[ S_n = na_1 + \frac{n(n-1)d}{2} \quad \text{或} \quad S_n = \frac{n\left[2a_1 + (n-1)d\right]}{2} ]

推导过程(由公式1转化而来):

因为等差数列的末项 ( a_n = a_1 + (n-1)d ),将其代入公式1:

[ S_n = \frac{n\left[a_1 + a_1 + (n-1)d\right]}{2} = \frac{n\left[2a_1 + (n-1)d\right]}{2} ]

展开后即可得到 ( S_n = na_1 + \frac{n(n-1)d}{2} )(两种形式等价,前者更便于分步计算)。

适用场景:

已知 ( a_1 )(首项)、( d )(公差)、( n )(项数),无需先求末项,直接代入计算。

示例:求等差数列 ( 2,5,8,11\cdots ) 的前6项和。

已知 ( a_1=2 ),( d=3 ),( n=6 ),代入公式:

( S_6 = 6\times2 + \frac{6\times(6-1)\times3}{2} = 12 + \frac{6\times5\times3}{2} = 12 + 45 = 57 )(验证:( 2+5+8+11+14+17=57 ),结果正确)。

三、公式选择与注意事项

  1. 公式选择原则
    1. 若已知“末项 ( a_n )”,优先用公式1(计算更简洁);
    2. 若未知“末项 ( a_n )”,但已知“公差 ( d )”,用公式2(避免额外计算末项)。
  2. 关键易错点
    1. 项数 ( n ) 的确定:例如“求数列 ( 4,7,10\cdots,31 ) 的项数”,需用 ( a_n = a_1 + (n-1)d ) 反推:( 31 = 4 + (n-1)\times3 ),解得 ( n=10 )(注意:不是 ( (31-4)\div3=9 ),需加1);
    2. 公差 ( d ) 的符号:若数列为递减数列(如 ( 10,7,4,1\cdots )),公差 ( d=-3 ),代入公式时需保留负号,避免符号错误。

四、总结

等差数列求和的核心是“利用数列的对称性(倒序相加)”或“用首项、公差表示所有项”,两个公式的关系可总结为:

[ \boxed{S_n = \frac{n(a_1 + a_n)}{2} \xleftrightarrow{\text{代入 } a_n = a_1 + (n-1)d} S_n = \frac{n\left[2a_1 + (n-1)d\right]}{2}} ]

记忆时无需死记硬背,理解推导逻辑后,可根据已知条件灵活选择公式。

//6 – 2 – 3 for

#include <iostream>

using namespace std;

int main() {

//a is initial condition

//b is the stop step condition

//c is the sentence that will be executed when each time we finished the {} sentence’s execution

/*for (a;b ; c)

{

}*/

/*int sum = 0;

for (int i =1; i <= 5; ++i)

{

sum += i;

}

cout << sum << endl;*/

/*int n;

//method 1

while (cin >> n) {

int sum = 0;

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

sum += i;

}

cout << sum << endl;

}*/

/*int n;

//method 2

while (cin >> n) {

int sum = 0;

int i = 1;

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

sum += i;

}

cout << sum << endl;

}*/

/*

int n;

//method 3

while (cin >> n) {

int sum;

int i;

for (sum = 0, i = 1; i <= n; ++i) {

sum += i;

}

cout << sum << endl;

}

*/

/*

int n;

//4.dead repeat

while (cin >> n) {

int sum;

int i;

for (sum = 0, i = 1;; ++i) {

sum += i;

}

cout << sum << endl;

}

*/

int n;

//4.dead repeat

while (cin >> n) {

int sum;

int i;

for (sum = 0, i = 1;i<=n; ) {

sum += i;

++i;

}

cout << sum << endl;

}

//while repeat doesn’t have the initial step

return 0;

}

//6-3-1 break

#include <iostream>

using namespace std;

int main() {

//1. the break in while;

int cnt = 0;

while (1) {

cnt++;

cout << “current number is ” << cnt << endl;

if (cnt > 20) {

break;

//break is jump out this repeat

}

}

cout << “——-” << endl;

//2. the break in for;

cnt = 20;

for (int i = 1; ;++i) {

cout << “current number is ” << i << endl;

if (i > cnt) {

break;

//break is jump out this repeat

}

//if this if sentence wrote in the for condition 2’s place, the code will be more complicative

}

cout << “——-” << endl;

//3. the break in switch case;

int  a = 1;

switch (a) {

case 1:

cout << “Vito” << endl;

break; //if we aren’t all this break,the least included default will be outputed until met the break again

case 2:

cout << “Algorithm” << endl;

default:

cout << “Algorithm2” << endl;

}

return 0;

}

//6 – 3 – 2 continue

#include <iostream>

using namespace std;

int main() {

/*

for (int i = 0; i < 10; ++i)

{

if (i < 3) {

continue;

}

cout << i << endl;

}

*/

/*

for (int i = 0; i < 10; )

{

if (i < 3) {

continue;

//after the continue, the program will judege the for’s condition 2

}

cout << i << endl;

++i;

//if we put the ++i on this place, this repetition will be the dead repetition, we must pay more attention when continue existed in the for sentence

}

*/

int a;

while (cin >> a){

if (a == 0) {

continue;

//continue is end up this time’s repetition

}

cout << a << endl;

}

return 0;

}