HZNU C++程序设计——实验5:数组

一、课内实验题(共10小题,100分)

题型得分100
  1. 【描述】
    输入10个整数,存放在一维数组中,找出值最大和最小的元素,输出最大值、最小值及它们所在的元素下标。
    【输入】
    在一行中输入10个整数,整数以空格间隔。
    【输出】
    第一行输出最大值及其所在的元素下标,最大值和下标以空格间隔。
    第二行输出最小值及其所在的元素下标,最小值和下标以空格间隔。
    【输入示例】

    1 3 5 7 9 6 0 8 2 4
    【输出示例】
    9 4
    0 6
    【来源】
    《程序设计基础——以C++为例》第4章实验1。

    (10分)

    我的答案:

    #include<iostream>
    using namespace std;
    int main() {
    	int a[15];
    	for (int i = 0;i < 10;i++) {
    		cin >> a[i];
    	}
    	int max=a[0], max_i=0, min=a[0], min_i=0;
    	for (int i = 0;i < 10;i++) {
    		if (a[i] > max) {
    			max = a[i];
    			max_i = i;
    		}
    		else if (a[i] < min) {
    			min = a[i];
    			min_i = i;
    		}
    	}
    	cout << max << " " << max_i << endl;
    	cout << min << " " << min_i << endl;
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <array>
    #include <algorithm>
    using namespace std;
    int main() {
        array<int, 10> a;
        for(int i = 0; i < 10; ++i)
            cin >> a[i];
        int max = *max_element(a.begin(), a.end());
        int min = *min_element(a.begin(), a.end());
        int maxIndex = max_element(a.begin(), a.end()) - a.begin();
        int minIndex = min_element(a.begin(), a.end()) - a.begin();
        cout << max << " " << maxIndex << endl;
        cout << min << " " << minIndex << endl;
        return 0;
    }
    /*
    #include <iostream>
    using namespace std;
    int main() {
        const int ARRAY_SIZE = 10;
        int a[ARRAY_SIZE];
        for(int i = 0; i < ARRAY_SIZE; ++i)
            cin >> a[i];
        int max = a[0];
        int min = a[0];
        int maxIndex = 0;
        int minIndex = 0;
        for(int i = 1; i < ARRAY_SIZE; ++i) {
            if(a[i] > max) {
                max = a[i];
                maxIndex = i;
            }
            if(a[i] < min) {
                min = a[i];
                minIndex = i;
            }
        }
        cout << max << " " << maxIndex << endl;
        cout << min << " " << minIndex << endl;
        return 0;
    } 
    */
  2. 【描述】
    输入一指定金额(以元为单位),然后输出支付该金额的各种面额的人民币数量,显示100元,50元,20元,10元,5元,1元各多少张,要求尽量使用大面额的。
    【输入】
    输入一个小于1000的正整数。
    【输出】
    分行输出,每行显示一个整数,从上到下分别表示100元,50元,20元,10元,5元,1元人民币的张数。
    【输入示例】

    735
    【输出示例】
    7
    0
    1
    1
    1
    0
    【来源】
    《程序设计基础——以C++为例》第4章实验2。

    (10分)

    我的答案:

    #include<iostream>
    using namespace std;
    int main() {
    	int a, b, c, d, e, f;
    	int x;
    	cin >> x;
    	if (x >= 100) {
    		a = x / 100;
    		x = x - a * 100;
    		cout << a << endl;
    	}
    	else {
    		cout << "0" << endl;
    	}
    	if (x >= 50) {
    		b = x / 50;
    		x = x - b * 50;
    		cout << b << endl;
    	}
    	else {
    		cout << "0" << endl;
    	}
    	if (x >= 20) {
    		c = x / 20;
    		x = x - c * 20;
    		cout << c << endl;
    	}
    	else {
    		cout << "0" << endl;
    	}
    	if (x >= 10) {
    		d = x / 10;
    		x = x - d * 10;
    		cout << d << endl;
    	}
    	else {
    		cout << "0" << endl;
    	}
    	if (x >= 5) {
    		e = x / 5;
    		x = x - e * 5;
    		cout << e << endl;
    	}
    	else {
    		cout << "0" << endl;
    	}
    	if (x >= 1) {
    		f = x / 1;
    		x = x - f * 1;
    		cout << f << endl;
    	}
    	else {
    		cout << "0" << endl;
    	}
    }
    题目得分10

    参考答案:

    #include <iostream>
    using namespace std;
    int main() {
        const int ARRAY_SIZE = 6;    // 货币面额数量
        int a[ARRAY_SIZE] = {100, 50, 20, 10, 5, 1};    // 货币面额(以元为单位)
        int counts[ARRAY_SIZE] = {0};    // 计数器,各货币面额张数
        double money;    // 输入的金额
        cin >> money;
        for(int i = 0; i < ARRAY_SIZE; ++i) {
            while(money >= a[i]) {
                money -= a[i];
                ++counts[i];
            }
        }
        for(int i = 0; i < ARRAY_SIZE; ++i)
            cout << counts[i] << endl;
        return 0;
    }
    
  3. 【描述】
    给定一组整数,要求利用数组把这组数保存起来,实现对数组中的数循环移动。假定共有n个整数,则要使前面各数顺序向后移m个位置,并使最后m个数变为最前面的m个数。
    要求只用一个数组的方式实现,一定要保证在输出结果时,输出的顺序和数组中数的顺序是一致的。
    【输入】
    第一行包含一个正整数n和一个正整数m,n和m以空格间隔。
    第二行包含n个正整数,整数以空格间隔。
    【输出】
    依次输出经过循环移动后数组中元素值,元素值以空格间隔。
    【输入示例】

    11 4
    15 3 76 67 84 87 13 67 45 34 45
    【输出示例】
    67 45 34 45 15 3 76 67 84 87 13

    (10分)

    我的答案:

    #include<iostream>
    #include<vector>
    using namespace std;
    int main() {
    	vector<int>v,v1;
    	int n, m;
    	cin >> n >> m;
    	for (int i = 0;i < n;i++) {
    		int x;
    		cin >> x;
    		v.push_back(x);
    	}
    	for (vector<int>::iterator i =v.end()-m ;i <v.end();i++) {
    		v1.push_back(*i);
    	}
    	for (vector<int>::iterator i = v.begin();i < v.end()-m;i++) {
    		v1.push_back(*i);
    	}
    	for (vector<int>::iterator i = v1.begin();i < v1.end();i++) {
    		cout << *i << " ";
    	}
    }
    题目得分10

    参考答案:

    #include <iostream>
    using namespace std;
    int main() {
        int n, m;
        cin >> n >> m;
        int array[n];
        for(int i = 0; i < n; ++i)
            cin >> array[i];
        for(int i = 1; i <= m; ++i) {
            int temp = array[n - 1];
            for(int j = 1; j < n; ++j) {
                array[n - j] = array[n - j - 1];
            }
            array[0] = temp;
        }
        for(int i = 0; i < n; ++i)
            cout << array[i] << " ";
        cout << endl;
        return 0;
    }
  4. 【描述】
    输入n(1≤n≤100)个正整数(无序的),找出第k(k≤n)大的数。注意,第k大的数意味着从大到小排在第k位置的数。
    【输入】
    n
    k
    a1 a2 a3 a4...an
    【输出】
    b
    【输入示例】

    5
    2
    32 3 12 5 89
    【输出示例】
    32

    (10分)

    我的答案:

    #include<iostream>
    #include<set>
    using namespace std;
    int main() {
    	set<int>s;
    	int n, k;
    	cin >> n >> k;
    	for (int i = 0;i < n;i++) {
    		int x;
    		cin >> x;
    		s.insert(x);
    	}
    	set<int>::iterator i = s.end();
    	for(int j=0;j<k;j++)i--;
    	cout << *i;
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <algorithm>
    // #include <cstdlib>
    using namespace std;
    /*
    int comp(const void *p, const void *q) {
        return *((int *)q) - *((int *)p);
    }
    */
    bool comp(int x, int y) {
        return x > y;
    }
    int main() {
        int n;
        cin >> n;
        int k;
        cin >> k;
        int intArray[100];
        for(int i = 0; i < n; ++i) {
            cin >> intArray[i];
        }
        // qsort(intArray, n, sizeof(int), comp);
        sort(intArray, intArray + n, comp);
        cout << intArray[k - 1] << endl;
        return 0;
    }
  5. 【描述】
    中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数或最中间两个数据的平均值(如果这组数的个数为奇数,则中位数为位于中间位置的那个数;如果这组数的个数为偶数,则中位数是位于中间位置的两个数的平均值)。
    给出一组无序整数,求出中位数,如果求最中间两个数的平均数,向下取整即可。
    【输入】
    包含多组测试数据,每一组测试数据的第一行为n,代表该组测试数据包含的数据个数,1≤n≤10000。
    接着n行为n个数据。n为0时结束输入。
    【输出】
    输出中位数,每一组测试数据输出一行。
    【输入示例】

    4
    10
    30
    20
    40
    3
    40
    30
    50
    4
    1
    2
    3
    4
    0
    【输出示例】
    25
    40
    2

    (10分)

    我的答案:

    #include<iostream>
    #include<map>
    #include<set>
    using namespace std;
    int main() {
    	int x;
    	while (cin >> x, x != 0) {
    		map<int,int>m;
    		set<int>s;
    		for (int i = 0;i < x;i++) {
    			int a;
    			cin >> a;
    			s.insert(a);
    		}
    		set<int>::iterator j = s.begin();
    		for (int i = 0;i < x;i++) {
    			m.insert(pair<int, int>(i, *j));
    			j++;
    		}
    		if (x % 2 == 1) {
    			map<int, int>::iterator it = m.find(x / 2);
    			cout << it->second << endl;
    		}
    		else {
    			map<int, int>::iterator it = m.find(x / 2 );
    			cout << (it->second+(--it)->second)/2 << endl;
    		}
    	}
    	
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <algorithm>
    // #include <cstdlib>
    using namespace std;
    /*
    int comp(const void *p, const void *q) {
        return *(int *)p - *(int *)q;
    }
    */ 
    int main() {
        while(true) {
            int n;
            cin >> n;
            if(!n)
                break;
            int array[n];
            for(int i = 0; i < n; ++i)
                cin >> array[i];
            // qsort(array, n, sizeof(int), comp);
            sort(array, array + n);
            if(n % 2 != 0)
                cout << array[n / 2] << endl;
            else
                cout << (array[n / 2 - 1] + array[n / 2]) / 2 << endl;
        }
        return 0;
    } 
  6. 【描述】
    编写程序,输入10个数,计算这10个数的均值和标准偏差。用下面的公式计算均值mean和标准偏差deviation:

    【输入】
    在第一行中给出10个数,数间以空格间隔。
    【输出】
    第一行为均值。
    第二行为标准偏差。
    【输入示例】

    583 566 58 632 244 485 600 432 88 562
    【输出示例】
    425
    216.476
    【来源】
    《程序设计基础——以C++为例》第4章实验4。

    (10分)

    我的答案:

    double mean(const double x[],int arraySize){
        double sum;
        for(int i=0;i<arraySize;i++){
            sum+=x[i];
        }
        return sum/arraySize;
    }
    double mean1(const double x[],int arraySize){
        double sum;
        for(int i=0;i<arraySize;i++){
            sum+=x[i];
        }
        return sum/(arraySize-1);
    }
    double deviation(const double x[],int arraySize){
        double mean2=mean(x,arraySize);
        double y[arraySize];
        for(int i=0;i<arraySize;i++){
            y[i]=pow(x[i]-mean2,2);
        }
        return sqrt(mean1(y,arraySize));
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <cmath>
    using namespace std;
    double mean(const double x[], int arraySize);
    double deviation(const double x[], int arraySize);
    int main() {
        const int ARRAY_SIZE = 10;
        double array[ARRAY_SIZE];
        for(int i = 0; i < ARRAY_SIZE; ++i)
            cin >> array[i];
        cout << mean(array, ARRAY_SIZE) << endl;
        cout << deviation(array, ARRAY_SIZE) << endl;
        return 0;
    }
    double mean(const double x[], int arraySize) {
        double sum = 0;
        for(int i = 0; i < arraySize; ++i)
            sum += x[i];
        return sum / arraySize;
    }
    double deviation(const double x[], int arraySize) {
        double mean1 = mean(x, arraySize);
        double squareSum = 0;
        for(int i = 0; i < arraySize; ++i)
            squareSum += pow(x[i] - mean1, 2);
        return sqrt(squareSum / (arraySize - 1));
    }
  7. 【描述】
    输入一个正整数n(2≤n≤10)和n×n矩阵a中的元素,如果a是上三角矩阵,输出“Yes”,否则输出“No”。
    【输入】
    第一行为正整数n,表示矩阵大小。
    接着n行,每一行n个整数,整数以空格间隔。
    【输出】
    输出“Yes”或“No”。
    【输入示例】

    3
    3 4 5
    1 2 3
    1 3 4
    【输出示例】
    No
    【提示】
    用二维数组表示n×n矩阵时(i表示行下标,j表示列下标),则:
    主对角线i==j,副对角线i + j == n – 1。
    上三角矩阵i<=j。
    下三角矩阵i>=j。
    【来源】
    《程序设计基础——以C++为例》第4章实验5。

    (10分)

    我的答案:

    #include<iostream>
    #include<vector>
    using namespace std;
    int main() {
    	int n;
    	cin >> n;
    	vector<vector<int>>v(11);
    	for (int i = 0;i < v.size();i++) {
    		v[i].resize(11);
    	}
    	for (int i = 0;i < n;i++) {
    		for (int j = 0;j < n;j++) {
    			cin >> v[i][j];
    		}
    	}
    	int ret = 0;
    	for (int i = 0;i < n;i++) {
    		for (int j = 0;j < i;j++) {
    			if (v[i][j] != 0) {
    				ret = 1;break;
    			}
    		}
    	}
    	if (ret == 1) {
    		cout << "No";
    	}
    	else {
    		cout << "Yes";
    	}
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <array>
    using namespace std;
    int main() {
        array<array<int, 10>, 10> a;
        int n;
        cin >> n;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                cin >> a[i][j];
        bool flag = true;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < i; ++j) {
                if(a[i][j] != 0) {
                    flag = false;
                    break;
                }
            }
        }
        cout << (flag ? "Yes" : "No") << endl;
        return 0;
    }
    /*
    #include <iostream>
    using namespace std;
    int main() {
        const int ARRAY_SIZE = 10;
        int a[ARRAY_SIZE][ARRAY_SIZE];
        int n;
        cin >> n;
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < n; ++j)
                cin >> a[i][j];
        bool flag = true;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < i; ++j) {
                if(a[i][j] != 0) {
                    flag = false;
                    break;
                }
            }
        }
        cout << (flag ? "Yes" : "No") << endl;
        return 0;
    }
    */
  8. 【描述】
    编写程序,创建一个m×n(2≤m、n≤10)的矩阵,输入矩阵的值,找出该矩阵的鞍点,鞍点是指本行最大、本列最小的元素,可能没有鞍点,也可能有多个鞍点。简单起见,只考虑一个鞍点和没有鞍点的情况。且保证矩阵中的值不会全相等。
    【输入】
    第一行输入矩阵的行列数m和n,以空格间隔。
    接着按矩阵的行列数输入矩阵的值。
    【输出】
    按“行 列 鞍点值”的格式输出鞍点,若不存在鞍点,则输出“No saddle point”。
    【输入示例】

    3 4
    41 89 31 39
    96 94 15 20
    40 96 86 11 
    【输出示例】
    0 1 89
    【来源】
    《程序设计基础——以C++为例》第4章实验6。

    (10分)

    我的答案:

    #include<iostream>
    #include<vector>
    using namespace std;
    struct maxv {
    	int max;
    	int max_j;
    }maxV[11];
    struct minv {
    	int min;
    	int min_i;
    }minV[11];
    int main() {
    	int n,m;
    	cin >> n>>m;
    	vector<vector<int>>v(11);
    	for (int i = 0;i < v.size();i++) {
    		v[i].resize(11);
    	}
    	for (int i = 0;i < n;i++) {
    		for (int j = 0;j < m;j++) {
    			cin >> v[i][j];
    		}
    	}
    	for (int i = 0;i < n;i++) {
    		int max = v[i][0],max_j=0;
    		for (int j = 1;j < m;j++) {
    			if (max < v[i][j]) {
    				max = v[i][j];
    				max_j = j;
    			}
    		}
    		maxV[i].max = max;
    		maxV[i].max_j = max_j;
    	}
    	for (int j = 0;j < m;j++) {
    		int min = v[0][j], min_i = 0;
    		for (int i = 1;i < n;i++) {
    			if (min > v[i][j]) {
    				min = v[i][j];
    				min_i = i;
    			}
    		}
    		minV[j].min = min;
    		minV[j].min_i = min_i;
    	}
    	int ret = 0;
    	for (int i = 0;i < n;i++) {
    		if(minV[maxV[i].max_j].min_i==i){
    			cout << i << " " << maxV[i].max_j << " " << maxV[i].max << endl;
    			ret = 1;
    		}
    	}
    	if (ret == 0) {
    		cout << "No saddle point";
    	}
    }
    题目得分10

    参考答案:

    #include <iostream>
    using namespace std;
    int main() {
        const int ROW = 10;
        const int COLUMN = 10;
        int a[ROW][COLUMN];
    	int m, n;
    	cin >> m >> n;
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                cin >> a[i][j];
            }
        }
        bool flag = false;
        int max, min, max_row, max_column;
        for(int i = 0; i < m; ++i) {
            max_row = i;
            max = a[i][0];
            max_column = 0;
            for(int j = 0; j < n; ++j) {
                if(a[i][j] > max) {
                    max = a[i][j];
                    max_column = j;
                }
            }
            min = a[0][max_column];
            for(int j = 0; j < m; ++j) {
                if(a[j][max_column] < min)
                    min = a[j][max_column] ;
            }
            if(max == min) {
                cout << max_row << " " << max_column << " " << max << endl;
                flag = true;
            }
        }
        if(!flag)
            cout << "No saddle point\n";
        return 0;
    }
  9. 【描述】
    编写程序,以左下三角的形式输出前n行杨辉三角形。
    【输入】
    输入在一行中给出n(1≤n≤10)。
    【输出】
    以左下三角的格式输出前n行杨辉三角形。每个数字占固定4位。
    【输入示例】

    5
    【输出示例】
       1
       1   1
       1   2   1
       1   3   3   1
       1   4   6   4   1
    【来源】
    《程序设计基础——以C++为例》第4章实验7。

    (10分)

    我的答案:

    #include<iostream>
    using namespace std;
    int main() {
        int arr[10][10];
        int n;
        cin >> n;
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i + 1; j++) {
                if (j == 0 || j == i) {
                    arr[i][j] = 1; count++;
                }
                else {
                    arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
                    count++;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i + 1; j++) {
                printf("%4d", arr[i][j]);
            }
            cout << endl;
        }
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main() {
        const int ARRAY_SIZE = 10;
        int a[ARRAY_SIZE][ARRAY_SIZE];
        int n;
        cin >> n;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(j == 0 || i == j)
                    a[i][j] = 1;
                if(j > 0 && i > j)
                    a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
                if(i < j)
                    a[i][j] = 0;
            }
        }
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(i >= j) {
                    cout << setw(4) << a[i][j];
                }
            }
            cout << endl;
        }
        return 0;
    }
  10. 【描述】
    拍摄的一张CT照片用一个二维数组来存储,假设数组中的每个点代表一个细胞。每个细胞的颜色用0到255之间(包括0和255)的一个整数表示。定义一个细胞是异常细胞,如果这个细胞的颜色值比它上下左右4个细胞的颜色值都小50以上(包括50)。数组边缘上的细胞不检测。现在的任务是,给定一个存储CT照片的二维数组,写程序统计照片中异常细胞的数目。
    【输入】
    第一行包含一个整数n(2<n≤100)。
    下面有n行,每行有n个0~255之间的整数,整数以空格间隔。
    【输出】
    输出一个整数,即异常细胞的数目。
    【输入示例】

    4
    70 70 70 70
    70 10 70 70
    70 70 20 70
    70 70 70 70
    【输出示例】
    2

    (10分)

    我的答案:

    #include<iostream>
    #include<iomanip>
    using namespace std;
    int main() {
        int a[105][105];
        int n;
        cin >> n;
        for (int i = 0;i < n;i++) {
            for (int j = 0;j < n;j++) {
                cin >> a[i][j];
            }
        }
        int count = 0;
        for (int i = 1;i < n-1;i++) {
            for (int j = 1;j < n-1;j++) {
                if ((a[i - 1][j] - a[i][j]) >= 50 && (a[i][j - 1] - a[i][j]) >= 50 && (a[i + 1][j] - a[i][j]) >= 50 && (a[i][j + 1] - a[i][j]) >= 50) {
                    count++;
               }
            }
        }
        cout << count;
    }
    题目得分10

    参考答案:

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main() {
        int array[100][100];
        int n;
        cin >> n;
        for(int i = 0; i < n; ++i) {
              for( int j = 0; j < n; ++j) {
                 cin >> array[i][j];
             }
        }
        int count = 0;
        for(int i = 1; i < n - 1; ++i) {
              for( int j = 1; j < n - 1; ++j) {
                  bool left = abs(array[i][j] - array[i][j - 1]) >= 50;
                  bool right = abs(array[i][j] - array[i][j + 1]) >= 50;
                  bool top = abs(array[i][j] - array[i - 1][j]) >= 50;
                  bool bottom = abs(array[i][j] - array[i + 1][j]) >= 50;
                  if(left && right && top && bottom)
                     ++count;
             }
        }
        cout << count << endl;
        return 0;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值