简单有趣#14:阵容【难度:2级】:
答案1:
namespace myjinxin
{
public class Kata
{
public int LineUp(string commands
)
{
int res
= 0, sameDir
= 1;
foreach (char cmd
in commands
)
res
+= sameDir
^= cmd
!= 'A' ? 1 : 0;
return res
;
}
}
}
答案2:
namespace myjinxin
{
using System
;
public class Kata
{
public int LineUp(string cmd
){
int g1
= 0, g2
= 0,stacko
=0;
for (int i
= 0; i
< cmd
.Length
; i
++)
{
if (cmd
[i
]=='L')
{
g1
+= 1;
g2
+= 3;
}
if (cmd
[i
] == 'R')
{
g1
+= 3;
g2
+= 1;
}
if (cmd
[i
] == 'A')
{
g1
+= 2;
g2
+= 2;
}
if (g1
> 4)
g1
= g1
- 4;
if (g2
> 4)
g2
= g2
- 4;
if (g1
== g2
)
stacko
+= 1;
}
return stacko
;
}
}
}
答案3:
namespace myjinxin
{
using System
.Collections
.Generic
;
public class Kata {
private readonly Dictionary
<char, char> _adic
= new Dictionary<char, char>{{'N', 'S'}, {'S', 'N'}, {'W', 'E'}, {'E', 'W'}};
private readonly Dictionary
<char, char> _northDic
= new Dictionary<char, char> { {'L', 'W'}, {'R', 'E'} };
private readonly Dictionary
<char, char> _westDic
= new Dictionary<char, char> { {'L', 'S'}, {'R', 'N'}, };
private readonly Dictionary
<char, char> _eastDic
= new Dictionary<char, char> { {'L', 'N'}, {'R', 'S'}, };
private readonly Dictionary
<char, char> _southDic
= new Dictionary<char, char> { {'L', 'E'}, {'R', 'W'}, };
public int LineUp(string cmd
) {
var nd
= new Dictionary<char, Dictionary
<char, char>> {{'N', _northDic
}, {'W', _westDic
}, {'E', _eastDic
}, {'S', _southDic
}};
var dd
= new Dictionary<char, Dictionary
<char, char>> { { 'N', _southDic
}, { 'W', _eastDic
}, { 'E', _westDic
}, { 'S', _northDic
} };
var normal
= 'N';
var dumb
= 'N';
var count
= 0;
foreach ( var com
in cmd
) {
if ( com
== 'A' ) {
normal
= _adic
[ normal
];
dumb
= _adic
[ dumb
];
} else {
normal
= nd
[normal
][com
];
dumb
= dd
[dumb
][com
];
}
if ( normal
== dumb
) {
count
+= 1;
}
}
return count
;
}
}
}
答案4:
namespace myjinxin
{
using System
;
public class Kata
{
public int LineUp(string cmd
){
int[] positionsArray
= { 0, 0 };
int count
= 0;
for (int i
= 0; i
< cmd
.Length
; i
++)
{
switch (cmd
[i
])
{
case 'L':
{
positionsArray
[0] = (positionsArray
[0] + 1) % 4;
positionsArray
[1] = (positionsArray
[1] + 3) % 4;
break;
}
case 'R':
{
positionsArray
[0] = (positionsArray
[0] + 3) % 4;
positionsArray
[1] = (positionsArray
[1] + 1) % 4;
break;
}
case 'A':
{
positionsArray
[0] = (positionsArray
[0] + 2) % 4;
positionsArray
[1] = (positionsArray
[1] + 2) % 4;
break;
}
}
if (positionsArray
[0] == positionsArray
[1]) count
++;
}
return count
;
}
}
}
答案5:
namespace myjinxin
{
using System
;
using System
.Linq
;
using System
.Collections
.Generic
;
public class Kata
{
public int LineUp(string cmd
)
{
var lefty
= ApplyMoves(cmd
, c
=> c
== 'R' ? 1 : c
== 'L' ? -1 : 0);
var righty
= ApplyMoves(cmd
, c
=> c
== 'R' ? -1 : c
== 'L' ? 1 : 0);
return lefty
.Zip(righty
, (right
, left
) => left
== right
).Count(t
=> t
);
}
private IEnumerable
<int> ApplyMoves(IEnumerable
<char> moves
, Func
<char, int> func
)
{
var state
= 0;
foreach (var move
in moves
)
{
state
= (state
+ func(move
) + 4) % 4;
yield return state
;
}
}
}
}
答案6:
namespace myjinxin
{
using System
;
using System
.Linq
;
public class Kata
{
public int LineUp(string cmd
)
{
int a
= 0, b
= 0, count
= 0;
foreach (var turns
in cmd
.Select(c
=> c
== 'R' ? 1 : c
== 'A' ? 2 : -1))
{
a
= (a
- turns
+ 4) % 4;
b
= (b
+ turns
+ 4) % 4;
if (a
== b
)
count
++;
}
return count
;
}
}
}
答案7:
namespace myjinxin
{
using System
;
public class Kata
{
public int LineUp(string cmd
)
{
int res
= 0;
char check
= 'F';
for(int i
= 0; i
< cmd
.Length
; i
++)
{
if(cmd
[i
] == 'A')
{
if (check
== 'L') check
= 'R';
else if (check
== 'R') check
= 'L';
else if (check
== 'F') { check
= 'B'; res
++; }
else { check
= 'F'; res
++; }
}
else if(cmd
[i
] == 'R')
{
if (check
== 'L') { check
= 'F'; res
++; }
else if (check
== 'R') { check
= 'B'; res
++; }
else if (check
== 'F') check
= 'R';
else check
= 'L';
}
else
{
if (check
== 'L') { check
= 'B'; res
++; }
else if (check
== 'R') { check
= 'F'; res
++; }
else if (check
== 'F') check
= 'L';
else check
= 'R';
}
}
return res
;
}
}
}
答案8:
namespace myjinxin
{
using System
;
public class Kata
{
public int LineUp(string cmd
)
{
int counter
= 0;
bool oneSide
= true;
if (!string.IsNullOrEmpty(cmd
))
{
if (cmd
[0] == 'L' || cmd
[0] == 'R')
{
oneSide
= false;
}
else
{
counter
++;
}
for (int i
= 1; i
< cmd
.Length
; i
++)
{
if ((cmd
[i
] == 'L' || cmd
[i
] == 'R') &
;&
; oneSide
== false)
{
counter
++;
oneSide
= true;
}
else if (oneSide
&
;&
; (cmd
[i
] == 'L' || cmd
[i
] == 'R'))
{
oneSide
= false;
}
else if (oneSide
&
;&
; cmd
[i
] == 'A')
{
counter
++;
oneSide
= true;
}
else
{
oneSide
= false;
}
}
}
return counter
;
}
}
}
答案9:
using System
.Collections
.Generic
;
namespace myjinxin
{
using System
;
public class Kata
{
public int LineUp(string cmd
)
{
int sameDirectionCount
= 0;
Stack
<char> teacherCommands
= new Stack<char>();
foreach (char letter
in cmd
)
{
if (letter
== 'L' || letter
== 'R')
{
if (teacherCommands
.Count
> 0 &
;&
; (teacherCommands
.Peek() == 'L' || teacherCommands
.Peek() == 'R'))
{
sameDirectionCount
++;
teacherCommands
.Pop();
}
else
{
teacherCommands
.Push(letter
);
}
}
else
{
if (teacherCommands
.Count
== 0)
{
sameDirectionCount
++;
}
}
}
return sameDirectionCount
;
}
}
}
答案10:
namespace myjinxin
{
using System
;
using System
.Linq
;
public class Kata
{
public int LineUp(string cmd
){
if (!cmd
.Any())
return 0;
int count
= 0;
bool pair
= true;
foreach (var command
in cmd
.ToUpper())
{
if (command
== 'L' || command
== 'R')
{
if (!pair
)
count
++;
pair
= !pair
;
}
else
{
if (pair
)
count
++;
}
}
return count
;
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-496999.html