牛客oj 习题6.10 A+B for Matrixs(矩阵相加)&&习题6.11递推数列

练这题主要是为了练习参数初始化表定义法。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cctype>
#include <cmath>
#include <climits>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;

struct Matrix{
	int maritx[10][10];
	int row;
	int col;
	Matrix(int r, int c) : row(r), col(c) {}//注意这里没有封号!(参数初始化表定义法) 
};

Matrix Plus(Matrix x, Matrix y){
	Matrix z(x.row, x.col);
	for(int i = 0; i < x.row; i++){
		for(int j = 0; j < x.col; j++){
			z.maritx[i][j] = x.maritx[i][j] + y.maritx[i][j];
		}
	}
	return z;
}

int main(){
 //   freopen("in.txt", "r", stdin);
    int M, N;
    while(~scanf("%d", &M)){
    	if(M == 0) break;
    	scanf("%d", &N);
    	Matrix x(M, N);
    	Matrix y(M, N);
    	for(int i = 0; i < x.row; i++){
    		for(int j = 0; j < x.col; j++){
    			scanf("%d", &x.maritx[i][j]);
    		}
    	}
    	for(int i = 0; i < y.row; i++){
    		for(int j = 0; j < y.col; j++){
    			scanf("%d", &y.maritx[i][j]);
    		}
    	}
    	Matrix z = Plus(x, y);
    	int ans = 0;
    	for(int i = 0; i < z.row; i++){
    		bool flag = true;
    		for(int j = 0; j < z.col; j++){
    			if(z.maritx[i][j] != 0) flag = false;
    		}
    		if(flag) ans++;
    	}
    	for(int i = 0; i < z.col; i++){
    		bool flag = true;
    		for(int j = 0; j < z.row; j++){
    			if(z.maritx[j][i] != 0) flag = false;
    		}
    		if(flag) ans++;
    	}
    	printf("%d\n", ans);
    }
    return 0;
}

送分题,不过这题让求的ak并非第k个数,而是第k+1个数,稍微纠结了下。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cctype>
#include <cmath>
#include <climits>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;

int main(){
   // freopen("in.txt", "r", stdin);
    int a0, a1, p, q, k;
    vector<int> a;
    while(~scanf("%d %d %d %d %d", &a0, &a1, &p, &q, &k)){
    	a.push_back((a0%10000));
    	a.push_back((a1%10000));
    	if(k == 1){
    		printf("%d", a[0]);
    		continue;
    	}
    	if(k == 2){
    		printf("%d", a[1]);
    		continue;
    	}
    	for(int i = 2; i <= k; i++){
    		int tmp = (p*a[i-1] + q*a[i-2])%10000;
    		a.push_back(tmp);
    	}
    	printf("%d\n", a[k]);
    	a.clear();
    }
    return 0;
}
发布了411 篇原创文章 · 获赞 72 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Flynn_curry/article/details/104748953