Replies: 26 comments 8 replies
|
CptSoap (程式) 我的解答public class Question1
{
private int currentStatus;
public bool CanWin(int status, int[] monsters)
{
currentStatus = status;
for (var i = 0; i < monsters.Length; i++)
{
if (currentStatus >= monsters[i])
{
currentStatus += monsters[i];
}
else
{
return false;
}
}
return true;
}
} |
2 replies
|
Suncacao(程式) 我的答案public class Question1
{
public bool CanWin(int status, int[] monsters)
{
//check input
if(monsters.Length <1 ||monsters.Length >1000){
Debug.LogError("Sorry,monsters data wrong!check pls.");
return false;
}
if(status <0 ||status >10000000){
Debug.LogError("Sorry,your status can't over 10M!check pls.");
return false;
}
//Okay! Game Start
int index = 0;
while(index<monsters.Length){
if(status < monsters[index]){
return false;
}else{
status += monsters[index];
}
index +=1;
}
return true;
}
} |
0 replies
2 replies
|
Snoweve (程式) 我的答案<<反饋>> |
1 reply
|
吉米(不知道要學什麼但是喜歡創作的新人) 答案在這 public class Question1
{
public enum Function
{
CheckLength,
CheckValue
}
private int minValue = 0;
private int MaxValue = 10000000;
private int MonsterMinLength = 1;
private int MonsterMaxLength = 1000;
public bool CheckValue(Function Function, int Value)
{
switch (Function)
{
case Function.CheckLength:
return Value >= MonsterMinLength && Value <= MonsterMaxLength;
case Function.CheckValue:
return Value >= minValue && Value <= MaxValue;
default:
return false;
}
}
public bool CanWin(int status, int[] monsters)
{
if(!CheckValue(Function.CheckValue, status) || !CheckValue(Function.CheckLength, monsters.Length)) return false;
foreach (var item in monsters)
{
if(!CheckValue(Function.CheckValue, item)) return false;
if(status >= item)
{
status += item;
}
else
{
return false;
}
}
return true;
}
}感謝老師!! |
0 replies
|
riverRobot (新人/企劃/程式) 我的答案public class Question1
{
public bool CanWin(int status, int[] monsters)
{
for(int i = 0; i < monsters.Length; i++)
{
if(status >= monsters[i])
{
status += monsters[i] ;
}
else
{
return false;
}
}
return true;
}
} |
0 replies
|
TWEdward(新人/程式) 我的答案public bool CanWin(int status, int[] monsters)
{
//return false;
int A;
for (int i = 0; i < monsters.Length - 1; i++)
{
for (int j = i + 1; j < monsters.Length; j++)
{
if (monsters[i] > monsters[j])
{
A = monsters[i];
monsters[i] = monsters[j];
monsters[j] = A;
}
}
}
for (int i = 0; i < monsters.Length; i++)
{
if (status < monsters[i])
{
return false;
}
else
{
status = status + monsters[i];
}
}
return true;
}我的學習心得感想我第一題終於有寫出通過單元測試的程式碼出來了^_^,但是第二題屬性火、水、風,我目前還沒寫出可以通過單元測試的程式碼出來QQ |
1 reply
|
肉鬆(程式 / 音樂) 我的答案 int status = 9;
int[] monsters = new int[] { 7, 13, 25, 51, 100 };
public bool CanWin(int status, int[] monsters)
{
for (int i = 0; i < monsters.Length; i++)
{
if (status >= monsters[i])
{
status += monsters[i];
}
else
return false;
}
return true;
}謝謝勞贖 |
0 replies
|
pruss(程式) 我的答案 public class Question1
{
bool CheckOutOfValue(int value, double minValue, double maxValue) => minValue <= value && value <= maxValue;
public bool CanWin(int status, int[] monsters)
{
var maxValue = Math.Pow(10, 7);
var maxLength = 1000;
if (!CheckOutOfValue(status, 0, maxValue))
{
throw new ArgumentException($"{nameof(status)} value out of bounds");
}
if (monsters.Any(monster => !CheckOutOfValue(monster, 0, maxValue)))
{
throw new ArgumentException($"{nameof(monsters)} value out of bounds");
}
if (!monsters.Any() || monsters.Length > maxLength)
{
throw new ArgumentException($"{nameof(monsters)} length out of bounds");
}
foreach (var monster in monsters)
{
if (status < monster)
{
return false;
}
status += monster;
}
return true;
}
}單元測試結果 |
0 replies
|
歐雷(程式) 答案 public class Question1
{
int Status = 0;
int[] Monsters;
void Start()
{
var a = Math.Pow(10, 7);
Status = UnityEngine.Random.Range(0, (int)a);
var MonsterLangth = (int)UnityEngine.Random.Range(1, 1000);
for(int i = 0; i< MonsterLangth; i++)
{
Monsters[i] = (int)UnityEngine.Random.Range(1, (int)a);
}
CanWin(Status, Monsters);
}
public bool CanWin(int status, int[] monsters)
{
for (int i = 0; i < monsters.Length; i++)
{
if (status >= monsters[i]) status += monsters[i];
else return false;
}
return true;
}
}我的答案不知道要幾次才會成功呢 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment





























Uh oh!
There was an error while loading. Please reload this page.
Introduction
聖哉是一位轉生到異世界的勇者,他獲得了轉生特典鑑識眼和急速成長,能看出敵我的能力值並且擊敗敵人時能獲得敵人完整的能力值,只要不弱於敵人,勇者一定能贏。在前去討伐魔王的路上有一堆怪物,聖哉目前可以看到自己的能力值status和前方怪物的能力值monsters。寫出一函式判斷聖哉能不能成功過關贏得勝利?
Answer
解答
這次的題目要求只需要用迴圈逐一判斷陣列內的元素,並且在跑一次判斷後,如果有勝利,就在把元素加總到status上就可以囉!
做法其實跟sum差不多。
All reactions