zoukankan      html  css  js  c++  java
  • 算法学习1——矩阵转置

    C++tip1:^符号的使用

    任何数^0=任何数;相同数^=0

    1)两个数交换

    a=a^b;结果存2个数相同的位为0,不同的位为1

    b=a^b;0表示相同,b的对应位不变,b的位与1^,1^1=0,0^1=1

    a=a^b;

    #include <iostream>
    using namespace std;
    int main()
    {
        int a=4;
        int b=6;
        cout << "before: a=" << a << " b=" << b <<endl;
        a=a^b;
        b=a^b;
        a=a^b;
        cout << "after: a=" << a << " b=" << b <<endl;
        return 0;
    }

    2)一组数中与所有数异或都为1,则这个数只有他一个

    #include <iostream>
    using namespace std;
    int main()
    {
        int arry[5]={1,2,3,4,5};
        int input;
        cout << "Input:";
        cin >> input;
        int i=0;
        bool flag=true;
        while(i<5)
        {
            if((arry[i]^input) == 0)
            {
                flag=false;
                break;
            }
            i++;
        }
        if(flag)
            cout << "The only one." << endl;
        else
            cout << "Not the only one." << endl;
    
        return 0;
    }

    3)判断两个数是否相等

    #include <iostream>
    using namespace std;
    int main()
    {
        int a, b;
        cin >> a >> b;
        if(a^b)
            cout << "a != b" << endl;
        else
            cout << "a == b" << endl;
    
        return 0;
    }

     【华为杯】题目:

    问题描述将一个N*N矩阵的行列互换要求实现函数

    void MatrixTranspose (const char *InArr, unsigned int n, const char *OutArray) 

    【输入】InArr:

    输入的字符矩阵n:N*N矩阵的行数

    【输出】OutArray:转置后的字符矩阵

    【返回】无。注:输入输出的矩阵都是以一维形式保存的二维数组,比如输入为{‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’}

    #include <iostream>
    #include <cmath>
    using namespace std;
    void swap(int &a,int &b)
    {
        a ^= b;
        b ^= a;
        a ^= b;
    }
    void MatrixTrasport(int *martrix,int size)
    {
        int xy = sqrt(size);
        for(int i=0;i<xy;i++)
            for(int j=0;j<i;j++)
                swap(martrix[i*3+j],martrix[j*3+i]);
    }
    void output(int *martrix,int size)
    {
        for(int i=0; i<size;i++)
            cout << martrix[i];
        cout <<endl;
    }
    int main()
    {
        int martrix[9]={1,2,3,4,5,6,7,8,9};
        output(martrix,9);
        MatrixTrasport(martrix,9);
        output(martrix,9);
        return 0;
    }
  • 相关阅读:
    Elastic-Job
    Redis之Ubuntu下Redis集群搭建
    设计模式之单例模式
    设计模式之简单工厂模式
    Java集合(一)HashMap
    数据结构和算法(四)归并排序
    数据结构和算法(三)希尔排序
    数据结构和算法(二)插入排序
    博客转移通知
    C语言回调函数总结
  • 原文地址:https://www.cnblogs.com/HackHer/p/5479407.html
Copyright © 2011-2022 走看看