牛客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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章