【C++】考研复习

1二进制转十进制输出

#include<iostream>
using namespace std;
double Power(double x, int n);//计算x的n次方

int main()
{
double result=0.0;
cout << "请输入八位二进制";
for (int i = 7; i >=0; i--)
{
char ch8;
cin >> ch8;
if (ch8=='1')
{
result+=static_cast<int>(Power(2, i));
}
}
cout << "十进制结果：" << result;
system("pause");
return 0;
}

double Power(double x, int n)
{
double val = 1.0;
while (n--)
{
val *= x;
}
return val;
}


2判断回文数

#include<iostream>
using namespace std;
bool Symm(unsigned n);
int main()
{
for (int m = 11; m < 1000; m++)
{
if (Symm(m)&&Symm(m*m)&&Symm(m*m*m))
{
cout << "m：" << m << endl;
cout << "m平方：" << m * m << endl;
cout << "m立方：" << m * m*m << endl;
}
}
return 0;
}
bool Symm(unsigned n)
{
unsigned i = n;
unsigned m = 0;
while (i>0)
{
m = m * 10 + i % 10;
i /= 10;
}
return m==n;//相等则为回文数则返回true 否则返回false
}


3泰勒展开式e^x,sinx

2020东软cpp第一题

#include<iostream>
#include<cmath>
using namespace std;
const double Tiny_value = 1e-9;
double Ex(double x);
double Sinx(double x);
int main()
{
//输入1.2，输出3.32012
//输入1，  输出2.71828
cout << Ex(1) << endl;
double r, s,k;
cin >> r >> s;
//r=5,s=8,k=1.37781
if (r*r<=s*s)
{
k = sqrt(Sinx(r)*Sinx(r) + Sinx(s)*Sinx(s));
}
if (r*r>s*s)
{
k = Sinx(r*s) / 2;
}
cout << "结果" << k;
return 0;
}
double Ex(double x)
{
double g = 1;
int n = 1;
double t = x;
do
{
g += t;
n++;
t = t * x / n;
} while (fabs(t)>=1e-10);
return g;
}
double Sinx(double x)
{
double g = 0;
int n = 1;
double t = x;
do
{
g += t;
n++;
t = -t *x*x / (2 * n - 1) / (2 * n - 2);
} while (fabs(t)>=Tiny_value);
return g;
}



5掷色子

#include<iostream>
//#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
//srand((unsigned int)time(NULL));
unsigned int t;
cout << "请输入作为随机种子:";
cin >> t;
srand((unsigned int)t);
int p,sum=0;
int c=1;//表示游戏轮次
int flag = 1;
int res = 0;//每一次摇色子的结果
while (flag)
{
for (int i = 0; i < 2; i++)
{
p = 1 + rand() % 6;//产生1-6的随机数
cout << "产生了:" << p<<" ";
res += p;//每轮的点数
}
cout <<"点数和为"<<res<< endl;
if (c == 1)
{
if (res == 7 || res == 11)
{
cout << "点数为" << res << endl;
cout << "第" << "1" << "轮" << "you win";
flag = 0;
}
if (res == 2 || res == 3 || res == 12)
{
cout << "点数为" << res << endl;
cout << "第1" << "轮" << "you lose";
flag = 0;
}
}
if (c > 1 )
{
if (res == 7)
{
cout << "第" << c << "轮出现7 you lose";
flag = 0;
}
if (res == sum)
{
cout << "此轮点数等于上一轮和数为" << sum << endl;
cout << "第" << c << "轮" << "you win";
flag = 0;
}
}
sum += res;
res = 0;
c++;//累计轮次
}
return 0;
}


08-17
10-12

02-21
03-26
01-06
11-12 1391
03-07 1901
01-25 771