免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 吴秦

[C] C语言经典题目及解题思路,持续更新中。。。 [复制链接]

论坛徽章:
0
发表于 2009-07-19 00:50 |显示全部楼层
第一题的矩阵解法,把以前PKU3070的代码改了以下发上来
#include <iostream>
#include <assert.h>
#include<cstdlib>
#include<cstring>
#ifndef _MYMATRIX_HPP_
#define _MYMATRIX_HPP_
#include <vector>
using namespace std;
namespace myLib{
        template <class __elemT>
        class myMatrix{
                private:
                        int m;//How many columns does the matrix have
                        int n;//How many rows does the matrix have
                        __elemT **table;//The elements in the matrix
                public:
                        myMatrix(int,int);
                        myMatrix(const myMatrix<__elemT> &;
                        myMatrix<__elemT>& operator= (const myMatrix<__elemT>&;
                        myMatrix<__elemT> operator* (const myMatrix<__elemT>&;
                        myMatrix<__elemT> pow(const int &k);
                        bool setValue(vector<__elemT *> matrix);
                        bool setValue(int v);
                        void print();
                        ~myMatrix();
        };
};
#endif
#include <cstdlib>
template <class __elemT>
myLib::myMatrix<__elemT>::myMatrix(int m=0,int n=0){
        assert( (m >= 0) && (n>=0) );
        this->m=m;
        this->n=n;
        this->table=new __elemT*[this->m];
        for(int i=0;i<m;i++){
                this->table=new __elemT[n];
                memset(this->table,0,sizeof(__elemT)*n);
        }
}
template <class __elemT>
myLib::myMatrix<__elemT>::myMatrix(const myLib::myMatrix<__elemT> &in){
        this->m=in.m;
        this->n=in.n;
        this->table=new __elemT*[in.m];
        for(int i=0;i<m;i++){
                this->table=new __elemT[n];
                memcpy(this->table,in.table,sizeof(__elemT)*n);
        }
}
template <class __elemT>
myLib::myMatrix<__elemT>::~myMatrix(){
        assert( (m >= 0) && (n>=0) );
        for(int i=0;i<m;i++){
                delete this->table;
        }
        delete this->table;
}
template <class __elemT>
myLib::myMatrix< __elemT>& myLib::myMatrix<__elemT>:perator=(const myMatrix<__elemT> &in){
         
         if(this==&in)
                 return *this;// identity test: if a self-assignment,
         assert(this->m>=0 && this->n>=0);
         for(int i=0;i<m;i++){
                 delete this->table;
         }
         delete this->table;
         this->m=in.m;
         this->n=in.n;
         this->table=new __elemT*[m];
         for(int i=0;i<m;i++){
                 this->table=new __elemT[n];
                 memcpy(this->table,in.table,sizeof(__elemT)*n);
         }
         return *this;
}
template <class __elemT>
bool myLib::myMatrix<__elemT>::setValue(vector<__elemT *> matrix)
{
        if( this->m != matrix.size() )
        {
                return false;
        }
        for(int i = 0; i < this->m ; i++ )
        {
                for( int j = 0; j < this->n ; j++ ){
                        this->table[j] = matrix.at(i)[j];
                }
        }
        return true;
}
template <class __elemT>
bool myLib::myMatrix<__elemT>::setValue(int v)
{
        for(int i = 0; i < this->m ; i++ )
        {
                for( int j = 0; j < this->n ; j++ ){
                        this->table[j] = v;
                }
        }
        return true;
}
template <class __elemT>
void myLib::myMatrix<__elemT>::print(){
        cout<<this->table[0][0]<<endl;
        /*
        for(int i=0;i<this->m;i++){
                for(int j=0;j<this->n;j++){
                        cout<<this->table[j]<<" ";
                }
                cout<<endl;
        }*/
}
template <class __elemT>
                myLib::myMatrix<__elemT>  
                myLib::myMatrix<__elemT>:perator * (const myLib::myMatrix<__elemT> & in)
{
        assert(this->n == in.m);
        myMatrix re(this->m,in.n);
        for(int i=0;i<this->m;i++){
                for(int j=0;j<in.n;j++){
                        for(int k=0;k<in.m;k++){
                                int t=this->table[k]*in.table[k][j]%10000;
                                re.table[j]=(re.table[j]+t)%10000;
                        }
                }
        }
        return re;
}
//ToDo The E=M^0 is not implemented.
template <class __elemT>
                myLib::myMatrix<__elemT>
                myLib::myMatrix<__elemT>::pow(const int &k)
{
        assert(this->m==this->n);
        myLib::myMatrix<__elemT> re;
        bool start=false;
        for(int i=1<<30;i;i>>=1){
                if(start)
                        re=re*re;
                if(i&k)
                        if(start) re=re*(*this);
                else {re=(*this);start=true;}
        }
        return re;
}       

using namespace std;
using namespace myLib;

int main(){
       
        int m=2,n=2;
        vector<int *> matrix;
        for(int i=0;i<m;i++){
                int *t=new int[n];
                for(int j=0;j<n;j++){
                        t[j]=1;
                }
                matrix.push_back(t);
        }
        matrix.at(1)[1]=0;
        myMatrix<int> M=myMatrix<int>(m,n);
        M.setValue(matrix);
        int c;
        while(cin>>c){
                if(c==-1)
                        break;
                if(c==0)
                        cout<<c<<endl;
                else if(c==1)
                        cout<<c<<endl;
                else {M.pow(c).print();}
        }
        for(int i=0;i<matrix.size();i++){
                delete matrix.at(i);
        }
}

论坛徽章:
29
技术图书徽章
日期:2013-09-02 19:59:502015元宵节徽章
日期:2015-03-06 15:51:332015小元宵徽章
日期:2015-03-06 15:57:20操作系统版块每日发帖之星
日期:2015-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17操作系统版块每日发帖之星
日期:2015-09-21 06:20:002015亚冠之水原三星
日期:2015-10-30 00:06:07数据库技术版块每日发帖之星
日期:2015-12-24 06:20:0015-16赛季CBA联赛之上海
日期:2016-01-07 10:32:07操作系统版块每日发帖之星
日期:2016-01-08 06:20:00操作系统版块每日发帖之星
日期:2016-05-18 06:20:00IT运维版块每日发帖之星
日期:2016-07-23 06:20:00
发表于 2009-07-20 21:08 |显示全部楼层
很好很好  过几天 我也整理一下,贴几个

招聘 : Java研发
论坛徽章:
0
发表于 2009-07-20 22:29 |显示全部楼层

回复 #1 吴秦 的帖子

楼主加油!
很好的帖子,收藏了!

论坛徽章:
0
发表于 2009-07-21 00:48 |显示全部楼层
标记下学习

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
发表于 2009-07-21 09:33 |显示全部楼层
学习。

论坛徽章:
0
发表于 2009-07-26 18:57 |显示全部楼层
原帖由 mfzz1134 于 2009-7-18 15:52 发表
第一题就是斐波那契数列,用矩阵是最好的方法,有logn的时间复杂度。


不是很明白跟斐波那契数列的关系,求明示,谢谢啊。

我只看出来类似动态规划的子问题重叠的解决方法可以改进,
count(n) = count(n-1) + count(n-2)
count(n-1) = count(n-2) + count(n-3)
....
可以看到有重复的count(n-2)子问题,可看出还有count(n-3), count(n-4)等重复子问题.
可使用Memoization的技术.

int count(int n)
{
     if (memo[n] == 0)
         memo[n] = count(n-1) + count(n-2);

    return memo[n]
}

memo[1] 和memo[2]可初始化为1和2,memo[3 ... n]初始化为0.

论坛徽章:
0
发表于 2009-07-26 19:21 |显示全部楼层
原帖由 mfzz1134 于 2009-7-18 15:52 发表
第一题就是斐波那契数列,用矩阵是最好的方法,有logn的时间复杂度。


想了下,果然一针见血啊,佩服佩服,多谢了。
1           2           3              4             5
-------------------------------------------------------
1           1           2              3             5           ......
NA   count(1)  count(2)  count(3)  count(4)    ......
-------------------------------------------------------

count(N)就是Fibonacci的第N+1项,也就是矩阵
[1 1] ^ n   
[1 0]
的最左上角的那一项。

论坛徽章:
0
发表于 2009-07-31 13:37 |显示全部楼层
非常好

第二题:
 caution:其实要判断这三个数是否由1~9组成且各个数组不相同,即这三个数的各位数是否覆盖了1~9,只要判断各位数字的积是否等于9!且各位数字的和是否等于45。

  能否解释下?
第七题就是一个排序问题。

对回溯法非常不熟悉。

学习了

[ 本帖最后由 aobai 于 2009-7-31 17:23 编辑 ]

论坛徽章:
0
发表于 2009-08-01 13:55 |显示全部楼层
mark一下 慢慢看!
多谢楼主!

论坛徽章:
0
发表于 2009-08-04 22:33 |显示全部楼层
mark一下先
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP