zoukankan      html  css  js  c++  java
  • C++的实验记录(1)

    N个整数排序

    #include <iostream>


    using namespace std;


    int *swap(int a[],int _size);//声明函数


    int main()

    {

        int i,N;

        cout << "please input the size of the array: " << endl;

        cin >> N;//输入要输入的个数


        int a[N];


        cout << "Input array elements:" << endl;

        for(i=0;i<N;i++)//用for循环来输入

        {

            cin >> a[i];

        }


        swap(a,N);//调用函数


        cout << "the result of array sorting:" << endl;

        for(int i=0;i<N;i++)//输出

        {

            cout << a[i] <<" ";

        }


        return0;

    }


    int *swap(int a[],int _size)//函数定义

    {

        int i,j,temp;

        for(j=0;j<_size-1;j++)

            for(i=1;i<_size;i++)

            {

                if(a[i]<a[i-1])

                {

                    temp = a[i];

                    a[i] = a[i-1];

                    a[i-1] = temp;

                }

            }

        return a;

    }



    输入一个n×n的矩阵,求出两条对角线元素值之和以及输出矩阵中最大值和最小值的下标。

    #include <iostream>

    #include <ctime>

    #include <cstdlib>


    using namespace std;


    int n;


    int main()

    {

        srand(time(NULL));

        cout << "what do u want for the size of n?" << endl;

        cin >> n;//


        cout << "--------------------------------------------------------------------------------" ;


    /* */

        int **p,i,j;


        p = newint*[n];

        for(int i=0;i!=n;i++)

        {

            p[i] = newint[n];

        }


        for(i=0;i<n;i++)//

        {

            for(j=0;j<n;j++)

            {

                p[i][j] = rand()%100;//

            }

        }


        for(i=0;i<n;i++)// 

        {

            for(j=0;j<n;j++)

            {

                cout << p[i][j] << " ";

            }

        }


        cout << "--------------------------------------------------------------------------------" ;



        int MAX =0;// 

        int x1,y1;

        for(i=0;i<n;i++)

        {

            for(j=0;j<n;j++)

            {

                if(MAX<p[i][j])

                {

                    MAX = p[i][j];

                    x1 = i;

                    y1 = j;

                }

            }

        }

        cout << "the largest one:" << endl;

        cout << "[" << x1 <<"]" << "[" << y1 <<"]" << endl;

        cout << endl;


        int MIN =100;// 

        int x2,y2;

        for(i=0;i<n;i++)

        {

            for(j=0;j<n;j++)

            {

                if(MIN>p[i][j])

                {

                    MIN = p[i][j];

                    x2 = i;

                    y2 = j;

                }

            }

        }

        cout << "the smallest one:" << endl;

        cout << "[" << x2 <<"]" << "[" << y2 <<"]" << endl;

        cout << endl;


        int sum1 =0;//

        for(i=0;i<n;i++)

        {

            for(j=0;j<n;j++)

            {

                if(i==j)

                    sum1 += p[i][i];

            }

        }

        cout << "÷˜∂‘Ω«œfl‘™Àÿ÷Æ∫Õ£∫" << sum1 << endl;


        int sum2 =0;//

        for(i=0;i<n;i++)

        {

            for(j=n-1;j>=0;j--)

            {

                if(i+j==n-1)

                    sum2 += p[i][j];

            }

        }

        cout << "∏±∂‘Ω«œfl‘™Àÿ÷Æ∫Õ£∫" << sum2 << endl;

        delete p;

        return0;

    }




  • 相关阅读:
    大学总结(一)
    关于数组名与指针的相互转换
    错误:无法执行操作,因为未将指定的 Storyboard 应用到此交互控件的对象
    延迟初始化 (Lazy Initialization)
    Sql Server 中 GAM、SGAM、PAM、IAM、DCM 和 BCM 的详解与区别
    Xml格式的字符串(string)到DataSet(DataTable)的转换
    Sql Server 内存用不上的解决办法
    Sql Server 管理区分配(GAM,SGAM)和可用空间(PAM)的原理
    Sql Server LightWeight Pooling(纤程) 选项
    在线 Sql Server 服务无法启动的解决办法
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852199.html
Copyright © 2011-2022 走看看