- 找出三位自然数中的所有素数, 要求判断x素数⽤自定义函数data(x)实现。
bool data(int x)
{
for (int j = 2; j <= x / 2; j++)
{
if (x % j == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int x = 100;
int sum = 0;
for (; x < 1000; x++)
{
if (data(x))
{
cout<<x<<endl;
sum++;
}
}
cout << sum << endl;
return 0;
}
- 输出n行由“#”组成的矩形, 每行“#”的个数为m个, 其中n、m由终端 (键盘) 输⼊, 要求输出m个“#”功能由函数satr(m)实现。
void satr(int m)
{
for (int i = 0; i < m; i++)
{
cout << "#";
}
}
int main()
{
int n, m;
cout << "输入行和列:" << endl;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
satr(m);
cout << endl;
}
return 0;
}
- 键盘输⼊长⽅体的长宽⾼l、w、h, 求其体积及表面积。要求⽤自定义函数val(l,w,h)计算长⽅体的体积和表面积, 计算结果使⽤全局变量 带回主函数。
int s=0, v=0;
int val(int l, int w, int h)
{
v = l*w*h;
s = 2 * ((l * w) + (l * h) + (w * h));
return v, s;
}
int main()
{
int l, w, h;
cout << "输入长方体的长宽高: " << endl;
cin >> l >> w >> h;
val(l, w, h);
cout <<"长方体体积"<< v <<"长方体表面积"<< s << endl;
return 0;
}
- 斐⽒数列是公元13世纪数学家斐波拉契发明的。即: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …… 实现函数求斐波那契数列的第n项?
void Fei(int n1)
{
int i = 0, f1 = 1, f2 = 2, f = 0;
while (i < n1)
{
if (i == 0)
{
cout << f1 << " ";
}
if (i == 1)
{
cout << f2 << " ";
}
if (i>=2)
{
f = f1;
f1 = f2;
f2 = f + f1;
cout << f2 << " ";
}
i++;
}
}
int main()
{
int n1;
cout << "输入项数" << endl;
cin >> n1;
Fei(n1);
cout << endl;
return 0;
}
- 给定⼀个正整数n, 输出1到n的所有排列,输⼊ 3 输出 123, 132, 213, 231, 312, 321。
void Pailie(int n, int cur)
{
if (cur == n)
{
for (int i = 0; i < n; i++)
cout << arr[i];
cout << " ";
}
else
{
for (int i = 1; i <= n; i++)
{
int flag = 1;
for (int j = 0; j<cur; j++)
{
if (arr[j] == i)
flag = 0;
}
if (flag)
{
arr[cur] = i;
Pailie(n, cur + 1);
}
}
}
}
int main()
{
unsigned int n2;
cout << "输入一个正整数" << endl;
cin >> n2;
for (int i = 0; i < n2; i++)
{
arr[i] = i + 1;
}
Pailie(n2, 0);
return 0;
}
- 实现⼀个函数, 参数为n, 返回1* ( 1 + 2) * …* ( 1 + 2 + … + n)的值。
int Get1(int n)
{
if (1 == n || 0 == n)
return 1;
else
return Get1(n - 1) + n;
}
int Get2(int n )
{
if (1 == n || 0 == n)
return 1;
else
return Get2(n - 1) * Get1(n);
}
int main()
{
int n3;
cout << "输入一个参数" << endl;
cin >> n3;
cout << Get2(n3) << endl;
return 0;
}
- 实现求⼀个整数的⼆进制中有多少个1的函数?
int Get3(int n)
{
int count = 0;
while (n)
{
n = n&(n - 1);
count++;
}
return count;
}
int main()
{
int a;
cout << "输入一个整数" << endl;
cin >> a;
cout << Get3(a) << endl;
}
- 实现⼀个函数, 求某个值在数组中第⼀次出现的位置 (下标), 如果没有则返回-1。
int Get4(int n,int num[10])
{
for (int i = 0; i < 10; i++)
{
if (num[i] == n)
return i;
}
return -1;
}
int main()
{
int a1, num[10] = {1,2,3,4,5,10,1,5,10,3};
cout<<"请输入一个整数" << endl;
cin >> a1;
cout << Get4(a1, num) << endl;
}
- 实现求x的y次⽅的函数。
int Get5(int x, int y)
{
if (0 == y)
return 1;
else if (1 == y)
return x;
else
return Get5(x, y-1)*x;
}
int main()
{
int x1, y1;
cout << "输入一个数及其次幂" << endl;
cin >> x1 >> y1;
cout << Get5(x1, y1) << endl;
}
- 实现⼀个函数, 求数组中的第⼆⼤的数。
int Get6(int num[])
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (num[j] < num[j + 1])
{
int temp = num[j + 1];
num[j+1] = num[j];
num[j] = temp;
}
}
}
return num[1];
}
int main()
{
int num1[10] = { 24, 36, 12, 30, 45, 56, 81, 77, 62, 99 };
cout <<"数组中第2大的数" <<Get6(num1) << endl;
}