宣老第三章程序
爱因斯坦阶梯
#include <iostream>
using namespace std;
int main(void)
{
int k = 7;
while( !((k % 3 == 2) && (k % 5 == 4) && (k % 6 == 5)))
{
k+=14;
}
cout << "k = " << k << endl;
return 0;
}
素数
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int n, j, s;
for(n = 101; n < 200; n += 2)
{
s = (int) sqrt((double)n);
for(j = 2; j <= s; ++j)
{
if(n % j == 0)
{
break;
}
}
if(j > s)
{
cout << n << endl;
}
}
return 0;
}
倍级数
#include <iostream>
using namespace std;
int main(void)
{
float s, t;
int i, a, n;
do{
cin >> a >> n;
if (a < 1 || a > 9)
{
cout << "Data Input Error !\nInput again.";
}
}while( a < 1 || a > 9);
s = 0.0f;
t = a;
for(i = 1; i <= n; i++)
{
s += t;
t = t * 10 + a;
}
cout << "s = " << s << endl;
return 0;
}
二分法求根
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
float x1, x2, x, f1, f2, f;
cin >> x1 >> x2;
f1 = x1 * x1 * x1 - 6.0f * x1 - 1.f; //解析式
f2 = x2 * x2 * x2 - 6.0f * x2 - 1.f;
cout << x1 << " " << x2 << " " << f1 << " " << f2 << endl;
do {
x = (x1 + x2) / 2.f; //中点
f = x * x * x - 6.0f * x - 1.f;
if(f * f1 >= 0.f)
{
f1 = f;
x1 = x;
}
else
{
f2 = f;
x2 = x;
}
}while(fabs(x1 - x2) >= 1e-6f);
cout << "Root is :" << (x1 + x2) / 2.f << endl;
return 0;
}
水仙花数
#include <iostream>
using namespace std;
int main(void)
{
int n, a, b, c;
for(n = 100 ; n <= 999 ; n++)
{
a = n / 100;
b = n % 100 / 10;
c = n % 10;
if(a * a * a + b * b * b + c * c * c == n)
{
cout << n << "锛氭槸姘翠粰鑺辨暟銆? << endl;
}
}
return 0;
}
一元二次方程求根
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
int main(void){
float a,b,c,d;
cout << "璇蜂互绌烘牸鍒嗛殧鎸夐『搴忚緭鍏ヤ竴鍏冧簩娆℃柟绋嬬殑绯绘暟锛?;
cin >> a >> b >> c;
d = b * b - 4.0f * a * c;
if((float)fabs(d) < 1.0e-6f)
{
cout << "x1 = x2 = " << -b/(2.0f*a) << endl;
}
else
{
if(d > 0)
{
cout << fixed << setprecision(4);
cout << " x1=" << (-b + (float)sqrt(d) ) / (2.0f * a) << endl;
cout << " x2=" << (-b - (float)sqrt(d) ) / (2.0f * a) << endl;
}
else
{
cout << fixed << setprecision(4);
cout << "x1=" << -b/(2.0f*a) << " + i" << (float)sqrt(-d)/(2.0f*a) << endl;
cout << "x1=" << -b/(2.0f*a) << " - i" << (float)sqrt(-d)/(2.0f*a) << endl;
}
}
system("pause");
return 0;
}
求pi
/********************************************************************
created: 2021/09/22
created: 22:3:2019 9:46
filename: PI.CPP
file path: D:\CAIWorkSpace\2018~2019第二学期教学文档\CCAI14FIN
file base: PI
file ext: CPP
author: Xuan
purpose: 本程序用于输入精度并计算圆周率统计计算时间次数。
*********************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(void)
{
clock_t start, end;
double dItem, dPi, dPrecision, dSign;
long long llCount = 0ll;
cout << "Input precision: ";
cin >> dPrecision;
dItem = 1.0;
dPi = 0.0;
dSign = 1.0;
start = clock();
do{
dPi += 1.0 / dItem * dSign;
dItem += 2.;
dSign = -dSign;
llCount++;
}while(1. / dItem > dPrecision / 4.);
end = clock();
cout << fixed << setprecision(16);
cout << "PI = " << setw(20) << 4.0 * dPi << endl;
cout << "Number = " << llCount << endl;
cout << "Time consuming = " << setprecision(3) <<((float)(end - start) / CLK_TCK) << 's' << endl;
system("pause");
return 0;
}
积分
#include <iostream>
using namespace std;
int main(void){
float a, b, f0, f1, h, x, s = 0.0f;
int n, i;
cin >> a >> b >> n;
h = (b - a) / n;
x = a;
f0 = x * x + 12.0f * x + 4.0f;
for(i = 0; i < n; i++)
{
x = x + h;
f1 = x * x + 12.0f * x + 4.0f;
s += (f0 + f1) * h / 2.0f;
f0 = f1;
}
cout << "s = " << s << endl;
return 0;
}