跳到正文

题解

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

Kai29约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年5月2日
收录
2026年5月2日
更新
2026年11月17日
篇幅
约 2 分钟
题目
P16406

版本

  1. v1当前

Solution

两步判断即可:

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

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

AC Code

C++
#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;
}
温馨提示

未成年人严禁购买彩票、参与博彩类活动,远离侥幸心理。

原文
存档评论6 条抓取于