(1)
#include <iostream>
using namespace std;
int main()
{
int i, f[10];
f[0] = f[1] = f[2] = 1;
for (i = 3; i < 10; i++)
f[i] = f[i - 1] + f[i - 2] + f[i - 3];
for (i = 0; i < 10; i++)
{
if (i % 4 == 0)
printf("\n");
printf("%4d", f[i]);
}
return 0;
}
(2)二维数组找最小值
#include <iostream>
using namespace std;
int main()
{
int i, j, row, col, min;
int a[3][4] = {
{
1, 2, 3, 4}, {
9, 8, 7, 6}, {
-1, -2, 0, 5}};
min = a[0][0];
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
if (a[i][j] < min)
{
min = a[i][j];
row = i;
col = j;
}
printf("min=%d,row =%d,col=%d\n", min, row, col);
return 0;
}
(3)3、 12名运动员短跑成绩如下图,要求对结果进行冒泡排序,并求出所有运动员的平均成绩和成绩方差。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
double a[12][2] = {
{
1, 13.6}, {
2, 14.9}, {
3, 14.8}, {
4, 12.6}, {
5, 12.0}, {
6, 13.4}, {
7, 12.7}, {
8, 12.5}, {
9, 15.6}, {
10, 15.3}, {
11, 13.4}, {
12, 12.7}};
double t, y, sum = 0, PZ, FC, PF;
int i, j;
for (i = 0; i < 11; i++)
{
for (j = 0; j < 11 - i; j++)
if (a[j][1] > a[j + 1][1])
{
t = a[j][1];
a[j][1] = a[j + 1][1];
a[j + 1][1] = t;
y = a[j][0];
a[j][0] = a[j + 1][0];
a[j + 1][0] = y;
}
}
for (i = 0; i < 12; i++)
{
for (j = 0; j < 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 0; i < 12; i++)
{
sum += a[i][1];
}
PZ = sum / 12;
cout << PZ << endl;
sum = 0;
for (i = 0; i < 12; i++)
{
PF = (a[i][1] - PZ) * (a[i][1] - PZ);
sum += PF;
}
FC = sum / 12;
cout << FC << endl;
return 0;
}
(4)4、 输入任意的5个整数放在一维数组中,假定输入的5个数是:1,2,3,4,5,编写程序打印下面的方阵:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int a[5], i, j, k, t;
for (i = 0; i < 5; i++)
cin >> a[i];
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
cout << a[j] << " ";
}
t = a[0];
for (k = 0; k < 4; k++)
{
a[k] = a[k + 1];
}
a[4] = t;
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a[5] = {
1, 2, 3, 4, 5};
int i, j;
for (i = 0; i < 5; i++)
{
for (j = i; j < i + 5; j++)
{
cout << a[j % 5] << " ";
}
cout << endl;
}
return 0;
}
(5)编一函数,输入一行字符,将此字符串中最长的单词输出。
#include <iostream>
using namespace std;
void findmax(char s[])
{
int index,l_max,i,ii,word=0,len=0;
i=0;
l_max=0;
while(s[i]!='\0')
{
if(s[i]!=' '&&word==0)
{
ii=i;
len=1;
word=1;
}
else if(s[i]!=' '&&word==1)
{
len++;
}
else if(s[i]==' '&&word==1)
{
if(len>l_max)
{
l_max=len;
index=ii;
}
word=0;
}
i++;
}
if(len>l_max)
{
l_max=len;
index=ii;
}
for(i=index;i<index+l_max;i++)
cout<<s[i];
cout<<endl;
}
int main()
{
char ch[80];
cin.getline(ch,80);
findmax(ch);
return 0;
}
(6)已知一维数组a[10]={3,-6,8,10,21,0,-5,7,4,14},使用选择排序法/冒泡排序法对其进行由小到大排序,并设计函数avg( )计算其平均值,要求以数组名作为实参。主函数中自拟数据进行验证。
#include <iostream>
using namespace std;
int avg(int *p, int n)
{
int i, sum, ave;
for (i = 0; i < n; i++)
{
sum += p[i];
}
ave = sum / n;
return ave;
}
int main()
{
int a[10] = {
3, -6, 8, 10, 21, 0, -5, 7, 4, 14};
int b[3] = {
1, 2, 3};
cout << avg(b, 3);
int i, j, t, k; //选择排序
for (i = 0; i < 9; i++)
{
k = i;
for (j = i + 1; j < 10; j++)
if (a[j] < a[k])
k = j;
if (k != i)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
int i, j, t; //冒泡排序
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9 - i; j++)
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
for (i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
(7)
struct student
{ int sid;
char name[30];
double score;
};
对上述student结构体类型定义一个结构体数组stu[4],并初始化。设计求最高分函数smax(),求最低分函数smin()和求最大分数差函数sdiffer(),要求通过函数调用求该结构体数组中的最大分数差。主函数中自拟数据进行验证。
#include <iostream>
using namespace std;
struct student
{
int sid;
char name[30];
double score;
};
double smax(student a[], int n)
{
double max = a[0].score;
int i;
for (i = 1; i < n; i++)
{
if (a[i].score > max)
{
max = a[i].score;
}
}
return max;
}
double smin(student a[], int n)
{
double min = a[0].score;
int i;
for (i = 1; i < n; i++)
{
if (a[i].score < min)
{
min = a[i].score;
}
}
return min;
}
void sdiffer(student b[], int n)
{
double fc, A, B;
A = smax(b, n);
B = smin(b, n);
fc = A - B;
cout << fc << endl;
}
int main()
{
student stu[4] = {
{
01, "a", 100}, {
02, "b", 99}, {
03, "c", 98}, {
04, "d", 97}};
smax(stu, 4);
smin(stu, 4);
sdiffer(stu, 4);
return 0;
}
(8)在一个字符串中(字符串中可能有空格)找出小写元音字母(a,e,i,o,u)分别出现的次数,并输出出现次数最多的字母。
举例: 输入:If so, you already have Google account.
输出:4 3 0 5 2
出现次数最多的字母是:o
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[100], c[5] = {
'a', 'e', 'i', 'o', 'u'};
cin.getline(a, 100);
int i, b[5]={
0};
int u, v, w, x;
for (i = 0; i < strlen(a); i++)
{
if (a[i] == 'a')
b[0]++;
if (a[i] == 'e')
b[1]++;
if (a[i] == 'i')
b[2]++;
if (a[i] == 'o')
b[3]++;
if (a[i] == 'u')
b[4]++;
}
for (i = 0; i < 5; i++)
{
cout << b[i] << '\t';
}
u = (b[0] > b[1]) ? 0 : 1;
v = (b[2] > b[3]) ? 2 : 3;
w = (b[u] > b[v]) ? u : v;
x = (b[w] > b[4]) ? w : 4;
cout << endl;
cout << c[x] << endl;
return 0;
}