求各位大哥看看!急急急!
实验4
#include<iostream>
using namespace std;
int main()
{
float n,i,sum=0;//定义原始数据
cout << "请输入n:";
cin >> n;
if (n < 10)//满足n>=10的条件
cout << "请输入不小于10的数" << endl;
else
{
for (i = 1; i <= n; i++)//计算过程
sum +=( i / ((2 * i - 1)*(2 * i + 1)));
cout << "计算结果为:";//显示结果
cout << sum << endl;
}
system("pause");
return 0;
}
实验3
#include<iostream>
using namespace std;
void sfun(char s1[],char s2[])
{
int i,j;
for (i = 0, j = 0; i <= 99; i++)//通过ASCII码判断数组元素是否为数字
{
if (s1[i] >= 48 && s1[i] <= 57)
{
s2[j] = s1[i];
j++;
}
}
cout << "输出s2数组为:" << s2 << endl;//输出s2数组
}
int main()
{
static char s1[100], s2[100];
cout << "请输入s1数组:";//构建s1数组
cin >> s1;
sfun(s1, s2);
system("pause");
return 0;
}
实验2
#include<iostream>
using namespace std;
int fact(int n)
{
static int arr[50];
int i,j,max;
for (i = 1,j=0; i < n; i++)//计算并记录输入的数的因子
{
if (n%i == 0)
{
arr[j] = i;
j++;
}
}
max = arr[0];//初始化最大值
for (j = 0; j <= 49,arr[j]!=0; j++)//计算最大因子
{
if (arr[j] > max)
max = arr[j];
}
return max;//返回最大因子
}
int main()
{
int n;
cout << "请输入所要计算最大因子的数:";
cin >> n;
cout << "该数最大因子为:";
cout << fact(n) << endl;;
system("pause");
return 0;
}
也许你已经考完了,我还是用C++写了下,祝你考试不挂科
所发顺序按你的图片顺序来,菜鸡编的程序,轻喷
#include<iostream>
using namespace std;
int main()
{
int str[5][5];
int i,j,sum=0;
cout << "请输入数组内容:" << endl;//创建数组内容
for (i = 0; i <= 4; i++)
{
for (j = 0; j <= 4; j++)
cin >> str[i][j];
}
cout << "输出数组结果为:" << endl;//输出数组内容
for (i = 0; i <= 4; i++)
{
for (j = 0; j <= 4; j++)
cout << str[i][j] << " ";
cout << endl;
}
for (i = 1; i <= 4; i++)//计算数组偶数行元素和
{
if (i % 2 == 1)
{
for (j = 0; j <= 4; j++)
sum += str[i][j];
}
}
cout << "所有偶数行元素和为:" << sum << endl;
system("pause");//为了能够查看程序运行结果所加的语句,可忽略
return 0;
}
这个程序为了方便检查运行就没用10×10的数组,用了5×5的,改改数字就行了