晚上睡不着,无聊着码了码代码 - -
这个题还比较简单。。。一个多小时完工吧。。。
C++语言:
高亮代码由发芽网提供
01
#include
02
#include
03
04
using
namespace
std;
05
06
int
ball_compare(
char
color,string
ball_color){
07
int
jsq=
0,i=
0;
08
// char temp[15];
09
// strncpy(temp,ball_color.c_str(),ball_color.length());
10
char*
temp
=
new
char(ball_color.length()+
1);
11
sprintf(temp
,
"%s"
,
ball_color.c_str());
12
if
(color==
'F'){
13
if
(temp[ball_color.size()-
1]==
'B')
return
'B';
14
if
(temp[ball_color.size()-
1]==
'L')
return
'L';
15
}
16
else
if(color==
'Y'){
17
for(i=
0;i<(ball_color.size());i++){
18
if
(temp[i]==
'Y')
jsq++;
19
}
20
return
jsq;
21
}
22
else
if(color==
'R'){
23
for(i=
0;i<(ball_color.size());i++){
24
if
(temp[i]==
'R')
jsq++;
25
}
26
return
jsq;
27
}
28 }
29
30
int
winner(
int
ball_num,string
ball_color){
31
if
(ball_num<<span style="color: rgb(0, 153, 153);">8){
32
if
(ball_compare(
'F',ball_color)==
'B')
33
cout<<
"Yellow"<<endl;
34
else
35
cout<<
"Red"<<endl;
36
}
37
else{
38
if
(ball_compare(
'F',ball_color)==
'B'){
39
if
(ball_compare(
'R',ball_color)==
7)
40
cout<<
"Red"<<endl;
41
else
42
cout<<
"Yellow"<<endl;
43
}
44
if
(ball_compare(
'F',ball_color)==
'L'){
45
if
(ball_compare(
'Y',ball_color)==
7)
46
cout<<
"Yellow"<<endl;
47
else
48
cout<<
"Red"<<endl;
49
}
50
}
51
return
0;
52 }
53
54
int
main(){
55
int
ball_num[
100];
56
string
ball_color[
100];
57
int
i=
0;
58
int
n;
59
while(
1){
60
cin>>ball_num[i];
61
if
(ball_num[i]==
0)
break;
62
cin>>ball_color[i];
63
i++;
64
}
65
n=i;
66
for
(i=
0;i
67
winner(ball_num[i],ball_color[i]);
68
}
69
return
0;
70 }
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
其中将字符串转为字符数组的时候百度了一下,本来是想用
strncpy函数的,但是提交OJ的时候说
‘strncpy’ was not declared in this scope,本地编译都通过了,略不解