A. Brain's Photos ——Codeforces Round #368 (Div. 2)
A. Brain's Photos
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.
As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).
Brain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour!
As soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white.
Photo can be represented as a matrix sized n × m, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors:
'C' (cyan)
'M' (magenta)
'Y' (yellow)
'W' (white)
'G' (grey)
'B' (black)
The photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100) — the number of photo pixel matrix rows and columns respectively.
Then n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'.
Output
Print the "#Black&White" (without quotes), if the photo is black-and-white and "#Color" (without quotes), if it is colored, in the only line.
Examples
input
2 2 C M Y Y
output
#Color
input
3 2 W W W W B B
output
#Black&White
input
1 1 W
output
#Black&White
____________________
题意:一张照片用矩阵n * m 表示,里面有C M Y W G B 几种颜色,判断这张照片是否是黑白照(黑白照只有W G B 三种)
程序应该还是比较好写的,一开始无脑写了一个版本是:
#include<iostream>
using namespace std ;
string s[100];
int main()
{
int n , m ;
cin >> n >> m ;
int flag = 0 ;
for ( int i = 0 ; i < n ; i++ )
{
for ( int j = 0 ; j < m ; j++ )
{
cin>>s[i][j] ;
}
}
for ( int i = 0 ; i < n ; i++ )
{
for ( int j = 0 ; j < m ; j++ )
{
switch(s[i][j])
{
case'C':
case'M':
case'Y':
flag = 1 ; break ;
default : continue ;
}
}
}
if(flag == 1 )
cout << "#Color"<< endl ;
else cout <<"#Black&White" << endl ;
return 0 ;
}
这样的话,大概时间复杂度在1W左右,应该能A,还是runtime error (但是在本机评测的话RE的数据可以在4.6s跑完,不知道是TLE了还是又是代码写的还会导致segment fault) 。
我在这里认为既然本机4.6s能跑完,应该是TLE了。于是怀疑被无数ACMer所诟病的cin cout 的效率,于是用scanf printf改写:
//#include<iostream>
//using namespace std ;
#include<cstdio>
char s[110][110];
int main()
{
int n , m ;
//freopen("2.txt","r",stdin);
scanf("%d %d",&n,&m);
int flag = 0 ;
for ( int i = 0 ; i < n ; i++ )
{
for ( int j = 0 ; j < m ; j++ )
{
//cin>>s[i][j] ;
scanf("%s",&s[i][j]);
switch(s[i][j])
{
case'C':
case'M':
case'Y':
flag = 1 ; break ;
default : continue ;
}
}
}
if(flag == 1 )
//cout << "#Color"<< endl ;
printf("#Color\n");
else //cout <<"#Black&White" << endl ;
printf("#Black&White\n");
return 0 ;
}
成功AC。
但是这个还是有优化空间的,发现开了那么大数组其实根本没必要使用。
最后优化一下代码:
#include<cstdio>
int main()
{
int n , m ;
scanf("%d %d",&n,&m);
int flag = 0 ;
char t[2];
for ( int i = 0 ; i < n ; i++ )
{
for ( int j = 0 ; j < m ; j++ )
{
//cin>>s[i][j] ;
scanf("%s",&t);
switch(t[0])
{
case'C':
case'M':
case'Y':
flag = 1 ; break ;
default : continue ;
}
}
}
if(flag == 1 )
//cout << "#Color"<< endl ;
printf("#Color\n");
else //cout <<"#Black&White" << endl ;
printf("#Black&White\n");
return 0 ;
}
这里似乎还是有字符串的问题,一开始蠢了字符串开成了char t[1];还错了好几次,貌似我访问t[1]访问到了m的地址,于是t[0]存放字符,t[1]把m的值改变了导致非常奇怪的跑法。
ps:在这里之所以我总是不甘心的用%s 字符串处理,是为了处理空格。
最后 AC记录:
![]()
A. Brain's Photos ——Codeforces Round #368 (Div. 2)的更多相关文章
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos 水题
A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...
- Codeforces Round #368 (Div. 2)A B C 水 图 数学
A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #368 (Div. 2) A , B , C
A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #368 (Div. 2) A
Description Small, but very brave, mouse Brain was not accepted to summer school of young villains. ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
随机推荐
- javaMail邮件发送的简单实现
package com.test.mail; import java.util.Properties; import javax.mail.Message; import javax.mail.Ses ...
- 找出Java数组中不同的值
public void deltp(PrintWriter out) { try{ PageData pd = new PageData(); pd = this.getPageData(); Str ...
- php 中文转拼音首字母问题
<?php /* 中文汉字转拼音首字母的PHP简易实现方法. 要求: 只能是GB2312码表里面中文字符 转换得到字符串对应的拼音首字母大写. 用法: echo zh2py::conv('Chi ...
- php分类
<?php /* * PHP分页类 * @package Page * @Created 2013-03-27 * @Modify 2013-03-27 * @link http://www.6 ...
- Java代码之输出参数和(强制类型转换)
说明(因为Java中java Application的参数都是默认的字符型的数据,所以需要强制类型转换这一步骤) 设计思想: 向系统里输入若干个参数,计算出参数个数,利用for语句计算出参数的和.(程 ...
- pip更换软件镜像源
家里的网络访问某些国外网站.下载安装包的时候总是连接不上或者下载速度特别慢, pypi.python.org就是其中一个.所以,使用pip给Python安装软件时,经常出现错误.修改pip连接的软件库 ...
- python升级2.7.5
一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才有的,但是系统的自带的 ...
- Qt 5.7 > Qt Applications
本文翻译自Qt官方文档: http://doc.qt.io/qt-5/qmlapplications.html QML 应用 QML是声明式语言,它使得用户界面以及交互行为可以被"描述&qu ...
- java变量初始化
java全局变量会自动初始化,但局部变量不会自动初始化.当我们新建一个对象的时候,java会申请一个区域存放类的数据,而成员变量就是类的数据,也是放在这个内存区域中,jvm申请内存时初始化.而方法中变 ...
- ios隐藏软键盘
//判断是否为苹果 var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE')!= -1; // 元素失去焦点隐藏iphone ...