21.父亲今年30岁,儿子今年6岁,问多少年后,父亲的年龄是 儿子的2倍?
#include <iostream>
using namespace std;
class character
{
public:
int age;
};
int main (void)
{
character father;
character son;
father.age = 30;
son.age = 6;
cout << "The father's age is: " << father.age << endl;
cout << "The son's age is: " << son.age << endl;
int year = 0;
while (1)
{
if (father.age + year == 2 * (son.age + year))
{
printf ("%d years later, the father is twice as old as the son. \n", year);
break;
}
year ++;
}
system ("pause");
return 0;
}
22.将1元人民币换成5分、2分、1分的硬币有多少种换法?并输 出每种组合的情况。
#include <iostream>
using namespace std;
#define YUAN 100
int main (void)
{
int index = 1;
for (int i = 0; i <= YUAN; i ++)
{
for (int j = 0; j <= YUAN / 2; j ++)
{
for (int k = 0; k <= YUAN / 5; k ++)
{
if ((i + j * 2 + k * 5) == YUAN)
{
printf ("The %03d way is: ", index ++);
printf ("%3d 1-cent %2d 2-cent %2d 5-cent add to 1 YUAN \n", i, j, k);
}
}
}
}
system ("pause");
return 0;
}
23.一只猴子摘了一堆桃,每天吃一半再加1个,第10天去吃时 只剩1个桃,问第一天的一堆桃有多少?
#include <iostream>
using namespace std;
#define DAY 10
int peachSum (int day);
int main (void)
{
cout << "The original peach sum is: " << peachSum (DAY) << endl;
return 0;
}
int
peachSum (int day)
{
if (day == 1)
{
return 1;
}
return (peachSum (day - 1) + 1) * 2;
}
24.一个4位数,逆向排列后是原4位数的倍数,求出符合条件的 4位数。
#include <iostream>
using namespace std;
int main (void)
{
int index = 1;
for (int a = 1; a < 10; a ++)
{
for (int b = 0; b < 10; b ++)
{
for (int c = 0; c < 10; c ++)
{
for (int d = 1; d < 10; d ++)
{
int number1 = a * 1000 + b * 100 + c * 10 + d;
int number2 = d * 1000 + c * 100 + b * 10 + a;
int multiple = number1 / number2;
if (number1 == multiple * number2)
{
printf ("Match condition %02d: ", index ++);
printf ("The number %d / %d = %d \n\n", number1, number2, multiple);
}
}
}
}
}
system ("pause");
return 0;
}
25.一个球从100米高度落下,每次反弹回原高度的一半,求第 10次落地时球的行程及下次反弹的高度。
#include <iostream>
#include <iomanip>
using namespace std;
#define MAX 10
int main (void)
{
double distance = 100.0;
double hight = 100.0;
for (int i = 0; i < MAX; i ++)
{
if (i == 0)
{
printf ("In %d bounce, distance is %.6f, jump hight is 0.0000000 \n", i, distance);
distance += hight;
hight *= 0.5;
continue;
}
printf ("In %d bounce, distance is %.6f, jump hight is %.7f \n", i, distance, hight);
distance += hight;
hight *= 0.5;
}
cout << "After 10 bounces, the distance of total is: " << setprecision(8) << distance << endl;
cout << "The last bounce-hight is: " << setprecision (8) << hight << endl;
system ("pause");
return 0;
}
26.输出公元1000年至今所有闰年。
#include <iostream>
using namespace std;
bool isLeap (int year);
int main (void)
{
int index = 1;
for (int year = 1000; year <= 2021; year ++)
{
if (isLeap (year) == true)
{
printf ("The %03d year %d is Leap year! \n", index ++, year);
}
}
system ("pause");
return 0;
}
bool
isLeap (int year)
{
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
{
return true;
}
return false;
}
27. 不通过系统函数计算(禁止使用任何数学函数 )。
#include <iostream>
#include <iomanip>
using namespace std;
#define EPS 1.0e-6
int main (void)
{
int sign = 1;
int denominator = 0;
double sum = 0.0;
cout << "PI / 4 = ";
for (int i = 1; 1.0 / (2.0 * i - 1.0) > EPS; i ++)
{
denominator = (2.0 * i - 1.0) * sign;
printf ("(1/%d) ", denominator);
if (1.0 / (2.0 * (i + 1) - 1.0) > EPS)
{
printf ("+ ");
}
sum += 1.0 / denominator;
sign = ~sign + 1;
}
cout << '\n' << "The value of PI is: " << setprecision(10) << sum * 4.0 << endl;
system ("pause");
return 0;
}
28.用连分数求 根号2
#include <iostream>
#include <iomanip>
using namespace std;
double myFabs (double n);
double mySqrt (double x);
#define EPS 1.0e-8
int main (void)
{
double a = 0.5, b = 1.0;
for(;;)
{
a = 1.0 / (2.0 + a);
if (myFabs (mySqrt (2.0) - b - a) < EPS)
{
cout << "The result is: " << setprecision (8) << a + b << endl;
break;
}
}
system ("pause");
return 0;
}
double
myFabs (double n)
{
return n > 0 ? n : -n;
}
double
mySqrt(double x)
{
double ans = 1.0;
while (myFabs (ans * ans - x) > EPS)
{
ans = (ans + x / ans) / 2.0;
}
return ans;
}
29.某地刑警大队对涉及6个嫌疑人的一桩疑案进行分析:
⑴ A、B至少有1人作案;
⑵ A、E、F ,3人中至少有2人参与作案;
⑶ A、D不可能是同案犯;
⑷ B、C或同时作案,或都与本案无关;
⑸ C、D中有且仅有一人作案;
⑹ 如果D没有参与作案,则E也不可能参与作案。
编程找出作案人,通过位运算实现可加分。
#include<iostream>
using namespace std;
int main (void)
{
int A, B, C, D, E, F;
cout << "(1) A and B at least one person committed the crime." << endl;
cout << "(2) A, E, F, at least two of them were involved in the crime." << endl;
cout << "(3) A and D can't have been partners in the crime." << endl;
cout << "(4) B and C either committed the crime at the same time, or neither of them is related to the case." << endl;
cout << "(5) Only one of C and D committed the crime." << endl;
cout << "(6) If D did not participate in the crime, E could not have done so." << endl;
cout << "\n Who are the perpetrators? \n" << endl;
for (int i = 0; i < 64; i ++)
{
A = i & 1;
B = (i & 2) >> 1;
C = (i & 4) >> 2;
D = (i & 8) >> 3;
E = (i & 16) >> 4;
F = (i & 32) >> 5;
bool s1, s2, s3, s4, s5, s6;
s1 = A || B;
s2 = (A && E) || (A && F) || (E && F);
s3 = !(A && D);
s4 = (B && C)||(!B && !C);
s5 = (C && !D) || (!C && D);
s6 = D || (!D && !E);
if (s1 && s2 && s3 && s4 && s5 && s6)
{
cout << "A" << (A == 1 ? " is" : " is not") << " Criminal" << endl;
cout << "B" << (B == 1 ? " is" : " is not") << " Criminal" << endl;
cout << "C" << (C == 1 ? " is" : " is not") << " Criminal" << endl;
cout << "D" << (D == 1 ? " is" : " is not") << " Criminal" << endl;
cout << "E" << (E == 1 ? " is" : " is not") << " Criminal" << endl;
cout << "F" << (F == 1 ? " is" : " is not") << " Criminal" << endl;
}
}
system ("pause");
return 0;
}
30.阿米巴用简单分裂的方式繁殖,它每分裂一次要用 3 分钟。将若干个阿米巴放在一个盛满营养参液的容器内, 45 分钟后容器内充满了阿米巴。已知容器最多可以装阿米巴 220(2的20次方)个。试问,开始的时候往容器内放了多少个阿米巴?
#include <iostream>
using namespace std;
#define SPLIT_TIME 3
#define TIME 45
int main (void)
{
int times = TIME / SPLIT_TIME;
int sum = 1;
cout << "The amoeba reproduces by simple division, which takes three minutes each time.";
cout << "Several amoebas are placed in a container filled with a nutrient-rich liquid, and after 45 minutes the container is filled with amoebas.";
cout << "We know that a container can hold up to two to the twentieth amoeba.";
cout << "How many amoebas did you put into the container at the beginning? \n" << endl;
for (int i = 1; 2 << i < 2 << 20; i ++)
{
sum = 2 << i;
int temp = sum;
for (int j = 0; j < times; j ++)
{
temp <<= 1;
}
if (temp == (2 << 19))
{
cout << "The initial value is: " << sum << endl;
}
}
system ("pause");
return 0;
}
31.求n < 20的梅森素数。 梅森素数:当n是素数时,2n-1(2的n次方)也是素数。
#include <iostream>
using namespace std;
bool isPrime (int x);
int main (void)
{
for (int i = 1; i < 20; i ++)
{
int num = (2 << (i + 1)) - 1;
if (isPrime (i) == true && isPrime (num) == true)
{
printf ("2^%d - 1 = %.0d is Mersenne Number!\n", i + 2, num);
}
}
return 0;
}
bool
isPrime (int x)
{
for (int i = 2; i * i < x; i ++)
{
if (x % i == 0)
{
return false;
}
}
return true;
}
32. s = 1/(1×1+1) - 1/(2×2-1) + 1/(3×3+1)-....
从键盘输入精度p,要求最后项的绝对值小于p,输出级数的值(禁止使用任何数学函数)。
#include <iostream>
#include <iomanip>
using namespace std;
int main (void)
{
int sign = 1;
int p;
cout << "Please input the setprecision: " << endl;
cin >> p;
double EPS = 1.0;
for (int i = 0; i < p; i ++)
{
EPS /= 10.0;
}
cout << '\n' <<"s = ";
double sum = 0.0;
for (int i = 1; 1.0 / (i * i + (1 * sign)) > EPS; i ++)
{
sum += 1.0 / (i * i + (1 * sign)) * sign;
printf ("1/(%d*%d+(%d))" ,i, i, sign);
if (1.0 / ((i + 1) * (i + 1) + (1 * sign)) > EPS)
{
if (sign == -1)
{
printf (" + ");
}
else if (sign == 1)
{
printf (" - ");
}
}
sign = ~sign + 1;
}
cout << '\n' <<"The sum is: " << setprecision (8) << sum << endl;
return 0;
}