《数组与指针程序设计》——小组作业(1-10)

⒈.狐狸捉兔子问题

围绕着山顶有10个洞,一只兔子和一只狐狸分别住在洞里,狐狸总想吃掉兔子,一天,兔子对狐狸说:你想吃掉我有一个条件,先把洞顺序编号,你从最后一个洞出发,第一次先到第一个洞找我,第二次隔一个洞找我,第三次隔两个洞找我,第四次隔三个洞找我,……依此规律类推,寻找次数不限,我躲在一个洞里不动,只要你找到我,就可以吃掉我。结果狐狸跑断了腿也没有找到兔子。请问,兔子躲在哪个洞里?假设狐狸找了1000次。

#include <iostream>

using namespace std;

#define MAXN 1000

int main (void)
{
    int hole[10] = {0};
    int iCount = 0;
    int treat = 0;

    for (int i = 0; iCount < MAXN; iCount ++)
    {
        treat = (treat + i) % 10;

        hole[treat] ++;
        i ++;
    }

    for (int j = 0; j < 10; j ++)
    {
        printf ("The %d hole arrived in %d times ! \n", j, hole[j]);
    }

    system ("pause");
    return 0;
}

⒉.已知一个班有36(测试用5个)个学生。用n数组存放学号,a数组存放物理成绩,b数组存放数学成绩。要求计算:

①数学及物理课程的平均成绩;

②输出两门课程都低于平均成绩的同学的学号和成绩;

③对数学成绩从高到低排序。

#include <iostream>

using namespace std;

#define MAXN 5

void MySwap (double *p1, double *p2);
void MySort (double *p);

unsigned int n[MAXN] = {0}; // 学号
double a[MAXN] = {0}; // 物理
double b[MAXN] = {0}; // 数学

int main (void)
{
    double mAver = 0.0;
    double pAver = 0.0;

    double mSum = 0.0;
    double pSum = 0.0;

    for (int i = 0; i < MAXN; i ++)
    {
        printf ("Please input the %d student ID: \n", i + 1);
        cin >> n[i];

        cout << "Please input the math score: \n";
        cin >> b[i];

        mSum += b[i];

        cout << "Please input the physice score: \n";
        cin >> a[i];

        pSum += a[i];
    }

    mAver = mSum / MAXN;
    pAver = pSum / MAXN;

    printf ("The average score on math test is %.2f \n", mAver);
    printf ("The average score on physics test is %.2f \n", pAver);

    printf ("Under average both physics and math score as: \n");

    for (int j = 0; j < MAXN; j ++)
    {
        if (a[j] < pAver && b[j] < mAver)
        {
            printf ("ID: %d \t math score: %.2f \t physice score: %.2f \n", n[j], b[j], a[j]);
        }
    }

    printf ("Math test scores range from high to low: \n");

    MySort (b);

    for (int k = 0; k < MAXN; k ++)
    {
        printf ("%.2f \n", b[k]);
    }

    system ("pause");
    return 0;
}

void
MySwap (double *p1, double *p2)
{
    double temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

void
MySort (double *p)
{
    double *p1 = NULL;
    double *p2 = NULL;
    double *pPos = NULL;

    for (p1  = p; p1 < p + MAXN - 1; p1 ++)
    {
        pPos = p1;

        for (p2 = p1 + 1; p2 < p + MAXN; p2 ++)
        {
            if (*pPos < *p2)
            {
                pPos = p2;
            }
        }

        if (pPos != p1)
        {
            MySwap (pPos, p1);
        }
    }

}

⒊.打印“杨辉”三角形(七阶)

1

1    1

1    2     1

1    3     3    1

1    4     6    4     1

1    5    10  10    5    1

1    6    15  20  15    6    1   

#include <iostream>

using namespace std;

#define MAX_ROW 7 //最大行数

int YangTriangle (int i, int j); // 递归实现

int main (void)
{
    for (int i = 1; i <= MAX_ROW; i ++)
    {
        for (int j = 1; j <= i; j ++)
        {
            printf ("%4d", YangTriangle (i, j));
        }

        printf ("\n");
    }

    system ("pause");
    return 0;
}

int
YangTriangle (int i, int j)
{
    return (j == 1 || i == j) ? 1 : (YangTriangle (i - 1, j - 1) + YangTriangle (i - 1, j));
}

⒋.求1,3,6,4,8,9,10的逆序排列。

逆序 10,9,8,4,6,3,1

#include <iostream>

using namespace std;

template <class T>
int GetNumsSize (T &array)
{
    return sizeof (array) / sizeof (array[0]);
}

int main (void)
{
    int array[] = {1, 3, 6, 4, 8, 9, 10};
    int numsSize = GetNumsSize (array);

    printf ("The array include: ");
    for (int i = 0; i < numsSize; i ++)
    {
        printf ("%d ", array[i]);
    }

    printf ("\n");

    int *p = NULL;

    printf ("Reversed result is: ");
    for (p = array + numsSize - 1; p >= array; p --)
    {
        printf ("%d ", *p);
    }

    system ("pause");
    return 0;
}

⒌.输入一串字符,将其中的奇数下标位置的小写字母表换成对应的大写字母,其余不变,并输出结果。

#include <iostream>

using namespace std;

void LowtoUp (char *str);

int main (void)
{
    char str[80];

    printf ("Please input string (< 80): \n");
    cin.getline (str, 80);

    LowtoUp (str);

    cout << "After exchanging \n" << str << endl;

    system ("pause");
    return 0;
}

void
LowtoUp (char *str)
{
    char *p = NULL;
    p = str;

    int i = 0;
    while (*(p + i) != '\0')
    {
        char cCur = *(p + i);

        if (cCur >= 'a' && cCur <= 'z' && i % 2 == 1) // 取奇数
        {
            *(p + i) -= 32;
        }

        i ++;
    }
}

6.统计一行(<80)英文文字中特定字符的数量,字符串和特定 字符从键盘输入。如统计 This is a testing program! 中s的数量。

#include <iostream>

using namespace std;

int SerchStr (char *str, char target);

int main (void)
{
    char str[80];
    cout << "Please input string (< 80): " << endl;
    cin.getline (str, 80);

    char target;
    cout << "Please enter the target: ";
    target = getchar ();

    printf ("The target %c appear %d times! \n", target, SerchStr (str, target));

    system ("pause");
    return 0;
}

int
SerchStr (char *str, char target)
{
    int iCount = 0;

    char *p = NULL;
    p = str;

    while (*p != '\0')
    {
        if (*p == target)
        {
            iCount ++;
        }

        p ++;
    }

    return iCount;
}

⒎定义一个十个元素组成的整型数组,从键盘输入各个元素。 让数组的最大值和第一个元素交换,最小值和最后一个元素交换。

#include <iostream>

using namespace std;

#define MAXN 10

void MySwap (int *p1, int *p2);
int *GetMaxPos (int array[MAXN]);
int *GetMinPos (int array[MAXN]);
void oSwap (int array[MAXN]);

int main (void)
{
    int array[MAXN];

    for (int i = 0; i < MAXN; i ++)
    {
        cin >> array[i];
    }

    oSwap (array);

    printf ("\n");

    for (int i = 0; i < MAXN; i ++)
    {
        printf ("%d ", array[i]);
    }

    return 0;
}

void
MySwap (int *p1, int *p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

int
*GetMaxPos (int array[MAXN])
{
    int *pPos = NULL;
    int *p = NULL;

    for (pPos = p = array; p < array + MAXN; p ++)
    {
        if (*pPos < *p)
        {
            pPos = p;
        }
    }

    return pPos;
}

int
*GetMinPos (int array[MAXN])
{
    int *pPos = NULL;
    int *p = NULL;

    for (pPos = p = array; p < array + MAXN; p ++)
    {
        if (*pPos > *p)
        {
            pPos = p;
        }
    }

    return pPos;
}


void
oSwap (int array[MAXN])
{
    MySwap (GetMaxPos (array), array);
    MySwap (GetMinPos (array), array + MAXN - 1);
}

⒏.编写程序生成4x4的上三角阵,要求各元素的值为其行号与 列号和的平方根。以数学表示方式输出结果。 

PS:

主对角线以下都是零的方阵称为上三角矩阵。上三角矩阵具有行列式对角线元素相乘、上三角矩阵乘以系数后也是上三角矩阵、上三角矩阵间的加减法和乘法运算的结果仍是上三角矩阵等性质

#include <iostream>

using namespace std;

#define ROW 4
#define COL 4

int main (void)
{
    int matrix[ROW][COL];

    printf ("Creat a %d x %d upper triangular matrix, requiring the value of each element "
            "to be the square root of the sum of its row and column numbers. \n\n", ROW, COL);

    for (int i = 0; i < ROW; i ++)
    {
        for (int j = 0; j < COL; j ++)
        {
            if (i > j)
            {
                matrix[i][j] = 0;

                printf ("%2d ", matrix[i][j]);
            }
            else
            {
                matrix[i][j] = ((i + 1) + (j + 1)) * ((i + 1) + (j + 1));

                printf ("%2d ", matrix[i][j]);
            }
        }

        printf ("\n");
    }

    return 0;
}

9.统计一个字符串中的单词数,单词以空格、逗号、句号、分号分隔。

#include <iostream>

using namespace std;

int WordBook (char *str);
bool MyisAlpha (char ch);

int main (void)
{
    char str[80];
    cout << "Please input string (< 80): " << endl;
    cin.getline (str, 80);

    cout << "There are " << WordBook (str) << " word \n" << endl;

    system ("pause");
    return 0;
}

bool
MyisAlpha (char ch)
{
    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
    {
        return true;
    }

    return false;
}

int
WordBook (char *str)
{
    int i = 0;
    int iCount = 0;

    char *p = NULL;
    p = str;

    while (*p != '\0')
    {
        if (MyisAlpha (*p))
        {
            char *p1 = p + 1;

            while (MyisAlpha (*p1 ++));

            iCount ++;

            p = p1;
        }

        p ++;
    }

    return iCount;
}

⒑编写程序求方阵主对角线上的最大值及其位置,输入a[4][4] 输出结果。

#include <iostream>

using namespace std;

#define MAXN 4

void FindMax (int (*p)[MAXN]);

int main (void)
{
    int a[MAXN][MAXN];

    printf ("Please input number in %d x %d matrix: \n", MAXN, MAXN);
    for (int i = 0; i < MAXN; i ++)
    {
        for (int j = 0; j < MAXN; j ++)
        {
            cin >> a[i][j];
        }
    }

    FindMax (a);

    system ("pause");
    return 0;
}

void
FindMax (int (*p)[MAXN])
{
    int iRow = 0;
    int iCol = 0;

    int (*q)[MAXN] = NULL;
    int iMax = *(*p);

    int i = 0;
    for (q = p; i < MAXN; i ++, *q ++)
    {
        int iCur = *((*q) + i);

        if (iCur > iMax)
        {
            iRow = q - p;
            iCol = q - p;

            iMax = iCur;
        }
    }

    printf ("The max number located in (%d, %d) \n", iRow, iCol);
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的企鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值