题解:P16406[Algo Beat Contest 004 B] Buying Lotteries

· · 题解

Solution

两步判断即可:

  1. 先检查整张彩票。遍历所有格子的第一个元素,如果存在任意一个 L ,那么直接获得所有格子的奖金总和。
  2. 如果没有 L ,那么就遍历所有格子,只有第一个元素等于中奖编码 X 的格子,才累加它的奖金。

建议使用字符串读取,对比号码时会方便很多。奖金的数量很大,要使用 long long 。

AC Code

#include <iostream>
#include <string>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    long long x;
    cin>>n>>m>>x;
    string t1[2005][2005];
    long long t2[2005][2005];
    bool f=0;
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            cin>>t1[i][j];
            if(t1[i][j]=="L") f=1;
        }
    }
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j) cin>>t2[i][j];
    }
    long long a=0;
    if(f){
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j) a+=t2[i][j];
        }
    }
    else{
        for(int i=0;i<n;++i){
            for(int j=0;j<m;++j){
                if(stoll(t1[i][j])==x)a+=t2[i][j];
            }
        }
    }
    cout<<a<<endl;
    return 0;
}

::::info[温馨提示] 未成年人严禁购买彩票、参与博彩类活动,远离侥幸心理。 ::::