17HNUCM计科练习6题解(棋盘覆盖)

目录

问题 A: 神秘的数字

问题 B: 象棋算式 

问题 C: 棋盘覆盖问题

 问题 D: 大整数乘法

问题 E: 矩阵乘法

 问题 F: Matrix multiplication


问题 A: 神秘的数字

题目描述

又一个神秘的数字,它是一个4位数,该4位数的千位上的数字和百位上的数字都被擦掉了,知道十位上的数字是1、个位上的数字是2, 
又知道这个数字减去7就能被7整除,减去8就能被8整除,减去9就能被9整除。 

输入

输出

输出这个数

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    int i,j;
    for(i=1;i<=9;i++){
        for(j=1;j<=9;j++){
            int c=i*1000+j*100+12;
            if((c-7)%7==0&&(c-9)%9==0&&(c-8)%8==0){
                cout<<c<<endl;
            }
        }
    }
    return 0;
}

问题 B: 象棋算式 

 

题目描述

又一个神秘的数字,它是一个4位数,该4位数的千位上的数字和百位上的数字都被擦掉了,知道十位上的数字是1、个位上的数字是2, 
又知道这个数字减去7就能被7整除,减去8就能被8整除,减去9就能被9整除。 

输入

输出

输出这个数

//别问我怎么直接输出,稍微推理一下就出结果了,没有什么算法可言
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,m,z,c;
    b=5;
    z=0;
    c=1;
    m=4;
    p=2;
    printf("%d %d %d %d %d",b,p,m,z,c);
    return 0;
}

问题 C: 棋盘覆盖问题

题目描述

在一个n×n (n = 2k)个方格组成的棋盘中,恰有一个方格与其他方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。 
在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。 

 

输入

多组测试用例,每组测试用例包括两部分, 
第一部分为方格的宽度n, 
第二部分则为方格,特殊方格为-1,其他方格为0。 
 

输出

输出覆盖后的方案

样例输入 Copy

4
-1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

样例输出 Copy

-1 2 4 4
2 2 1 4
3 1 1 5
3 3 5 5

 

#include <bits/stdc++.h>
using namespace std;
int a[100][100];
int k=0;
int aa(int tr,int tc,int dr,int dc,int size){
    if(size==1){
        return 0;
    }
    int t=++k;
    int s=size/2;
    if(dr<tr+s&&dc<tc+s){
        aa(tr,tc,dr,dc,s);
    }else{
        a[tr+s-1][tc+s-1]=t;
        aa(tr,tc,tr+s-1,tc+s-1,s);
    }
    if(dr>=tr+s&&dc<tc+s){
        aa(tr+s,tc,dr,dc,s);
    }else{
        a[tr+s][tc+s-1]=t;
        aa(tr+s,tc,tr+s,tc+s-1,s);
    }
    if(dr<tr+s&&dc>=tc+s){
        aa(tr,tc+s,dr,dc,s);
    }else{
        a[tr+s-1][tc+s]=t;
        aa(tr,tc+s,tr+s-1,tc+s,s);
    }
    if(dr>=tr+s&&dc>=tc+s){
        aa(tr+s,tc+s,dr,dc,s);
    }else{
        a[tr+s][tc+s]=t;
        aa(tr+s,tc+s,tr+s,tc+s,s);
    }
}
int main()
{
    int n;
    while(cin>>n){
        int x,y;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin>>a[i][j];
                if(a[i][j]==-1){
                    x=i;
                    y=j;
                }
            }
        }
        aa(0,0,x,y,n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<a[i][j]<<" ";
                a[i][j]=0;
            }
            cout<<endl;
        }
        k=0;
    }
    return 0;
}

 

 问题 D: 大整数乘法

题目描述

使用分治算法实现两个大整数相乘。

输入

两个十进制大整数,满足每一个整数长度为2^n且两个大整数的长度相等。(多组数据)

 

输出

两个大整数的乘积。

样例输入 Copy

1234 5678

样例输出 Copy

7006652
#include <bits/stdc++.h>

using namespace std;
int aa(long long m,long long n,long long s){
    if(!m||!n){
        return 0;
    }
    if(s==1){
        return m*n;
    }
    long long a,b,c,d;
    long long k=s/2;
    long long j;
    j=pow(10,k);
    a=m/j;
    b=m%j;
    c=n/j;
    d=n%j;
    long long ac=aa(a,c,k);
    long long bd=aa(b,d,k);
    long long abcd=aa(a-b,d-c,k)+ac+bd;
    return ac*pow(10,s)+abcd*pow(10,k)+bd;
}
int bb(long long  s){
    return s>0?1:-1;
}
int main()
{
    long long a,b;
    while(cin>>a>>b){
        int s=bb(a)*bb(b);
        a=abs(a);
        b=abs(b);
        long long k=a;
        int i=0;
        while(k){
            i++;
            k/=10;
        }
        long long r=s*aa(a,b,i);
        cout<<r<<endl;
    }
    return 0;
}

问题 E: 矩阵乘法

题目描述

设M1和M2是两个n×n的矩阵,使用分治法计算M1×M2 的乘积。

输入

一个整数n表示矩阵的维数,接下来n行为第一个矩阵,再下面n行为第二个矩阵。

 

输出

矩阵的乘积(两个数字之间空一格,数字右对齐)。

样例输入 Copy

2
1 1
2 2
3 3
4 4

样例输出 Copy

 7  7
14 14

 

 

//有关这个题目,按照课程进度和老师的要求,其实应该用strassen矩阵乘法的;很惭愧这个题我
//搜了题解,但是太复杂了,超出了我此时的能力范围,所以我决定暴力。。。
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
    while(cin>>n){
        int a[n][n],b[n][n],c[n][n];
        int i,j;
        for(i=0;i<n;++i){
            for(j=0;j<n;++j){
                cin>>a[i][j];
            }
        }
        for(i=0;i<n;++i){
            for(j=0;j<n;++j){
                cin>>b[i][j];
            }
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                c[i][j] = 0;
                for(int k = 0; k < n; k++){
                    c[i][j] += a[i][k] * b[k][j];
                }
                printf("%2d",c[i][j]);
                if(j!=n-1)
                    cout<<" ";
            }
            cout<<endl;
        }
	}
	return 0;
}

 问题 F: Matrix multiplication

题目描述

Given two matrices A and B of size n×n, find the product of them.

bobo hates big integers. So you are only asked to find the result modulo 3.

输入

The input consists of several tests. For each tests:

The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).

输出

For each tests:

Print n lines. Each of them contain n integers -- the matrix A×B in similar format.

样例输入 Copy

1
0
1
2
0 1
2 3
4 5
6 7

样例输出 Copy

0
0 1
2 1
//输出的矩阵取模就好了
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
    while(cin>>n){
        int a[n][n],b[n][n],c[n][n];
        int i,j;
        for(i=0;i<n;++i){
            for(j=0;j<n;++j){
                cin>>a[i][j];
            }
        }
        for(i=0;i<n;++i){
            for(j=0;j<n;++j){
                cin>>b[i][j];
            }
        }
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                c[i][j] = 0;
                for(int k = 0; k < n; k++){
                    c[i][j] += a[i][k] * b[k][j];
                }
                printf("%d",c[i][j]%3);
                if(j!=n-1)
                    cout<<" ";
            }
            cout<<endl;
        }
	}
	return 0;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值