#include<stdlib.h>
#include <time.h>
main()
{
int face // 储存每次色子的点数
int roll //投掷色子的次数(循环变量)
int frequency[7] = {0} //记录色子每个点数出现的次数
srand(time (NULL)) //用系统时间来初始化系统随机数的种子值
//用循环产生100次随机数,并记录每个点数出现的次数
for (roll=1 roll<=100 roll++)
{
face = rand()%6 + 1
frequency[face]++
}
printf("%4s%17s\n", "Face", "Frequency")
//输出每个点数出现的次数
for (face=1 face<=6 face++)
{
printf("%4d%17d\n", face, frequency[face])
}
system("pause")
}
static void Main(string[] args){
// RollDice()
//TestEquation()
DoubleCircle()
Console.ReadLine()
}
以上为主程序调用的方法,我分别将你的题目写在上面的方法里.
题目一:
private static void RollDice(){
Dice dice = new Dice()
Console.WriteLine("初始化的值{0}", dice.GetFaceValue())
dice.Roll()
Console.WriteLine("滚动骰子后的值{0}", dice.GetFaceValue())
dice = new Dice(6)
Console.WriteLine("初始化的值{0}", dice.GetFaceValue())
dice.Roll()
Console.WriteLine("滚动骰子后的值{0}", dice.GetFaceValue())
}Dice类如下: public class Dice
{
private int faceValue
private Random random = new Random()
public Dice()
{
}
public Dice(int p)
{
this.faceValue = p
}
public int GetFaceValue()
{
return faceValue
}
public void Roll()
{
//这个可以注释,为了使随机数延时生成不同的的数字
System.Threading.Thread.Sleep(10)
faceValue = random.Next(1, 6)
}
}
题目2:
private static void TestEquation(){
string strInput = string.Empty
double a = 0, b = 0, c = 0
while (!TestInputNum(strInput, ref a, ref b, ref c))
{
Console.WriteLine("请输入三个数字 a b c,保证 a != 0, b * b – 4 * a * c >0,用空格分开:")
strInput = Console.ReadLine()
}
Equation qu = new Equation(a, b, c)
Console.WriteLine("第一个根:{0}", qu.GetFirstRoot())
Console.WriteLine("第二个根:{0}", qu.GetSecondRoot())
}
TestInputNum方法:
private static bool TestInputNum(string strInput, ref double a, ref double b, ref double c){
if (!string.IsNullOrEmpty(strInput) &&strInput.Split(' ').Length == 3)
{
string[] strs = strInput.Split(' ')
//条件
if (double.TryParse(strs[0], out a) &&
double.TryParse(strs[1], out b) &&
double.TryParse(strs[2], out c))
{
if (a != 0 &&b * b - 4 * a * c >0)
{
return true
}
else
Console.WriteLine("无根")
}
else
Console.WriteLine("输入不是数字")
}
return false
}
Equation:
public class Equation{
private double a
private double b
private double c
private double del
public Equation()
{
}
public Equation(double a, double b, double c)
{
this.a = a
this.b = b
this.c = c
this.del = b * b - 4 * a * c
}
public double GetFirstRoot()
{
return (-b + Math.Sqrt(del)) / (2 * a)
}
public double GetSecondRoot()
{
return (-b - Math.Sqrt(del)) / (2 * a)
}
}
题目三(这个问题有漏洞,需求和你截图的输出结果不一样,我是按照你需求来写的):
private static void DoubleCircle(){
int x = 3
Circle c = new Circle()
c.Radius = 5
Helper h = new Helper()
x = h.DoubleMe(x)
c.Radius = h.DoubleMe(c)
Console.WriteLine("Out doubleMe:x={0}", x)
Console.WriteLine("Out doubleMe:半径={0}", c.Radius)
}
Circle类:
public class Circle{
public int Radius { getset}
}
Helper类(自己再加工下):
public class Helper{
/// <summary>
/// 是把参数x(值类型)的值增加一倍
/// </summary>
/// <param name="x"></param>
public int DoubleMe(int x)
{
return x * 2
}
/// <summary>
/// 把参数c(引用类型)所代表的圆对象的半径增加一倍
/// </summary>
/// <param name="c"></param>
public int DoubleMe(Circle c)
{
return DoubleMe(c.Radius)
}
}
参考下面的代码.
play 可能有问题,主要是没说清楚在保留牌的时候, 输入Ace 或者 "Ace Ace" 有什么区别,到底是输入一次 Ace 保留手上所有的 Ace 还是只保留一个,这个没说清楚。看例子,这两种用法都有,我按照输入了几个就保留几个来做的。
simulate 没问题,和图片中的结果完全一样
必须用 python 3
import randomimport collections
_dice_type = ['Ace', 'King', 'Queen', 'Jack', '10', '9']
_hand_mapping = collections.OrderedDict([
('5kind', 'Five of a kind'),
('4kind', 'Four of a kind'),
('full', 'Full house'),
('straight', 'Straight'),
('3kind', 'Three of a kind'),
('2pair', 'Two pair'),
('1pair', 'One pair'),
('bust', 'Bust'),
])
def _check_hand(dices):
counter = collections.Counter(dices)
if len(counter) == 1:
return '5kind'
sorted5 = counter.most_common(5)
if sorted5[0][1] == 4:
return '4kind'
if sorted5[0][1] == 3:
if sorted5[1][1] == 2:
return 'full'
else:
return '3kind'
if sorted5[0][1] == 2:
if sorted5[1][1] == 2:
return '2pair'
else:
return '1pair'
if len(counter) == 5:
dtype = sorted5[0][0]
for x in sorted5:
if dtype != x[0]:
break
dtype += 1
else:
return 'straight'
return 'bust'
def play():
dices = []
retry = 0
while True:
remain = 5 - len(dices)
if remain <= 0:
break
dices.extend([random.randint(0,5) for x in range(remain)])
print("The roll is: {}".format(
" ".join([_dice_type[d] for d in sorted(dices)])
))
print("It is a {}".format(_hand_mapping[_check_hand(dices)]))
if retry > 1:
break
prompt = "Which dice do you want to keep for the {} roll? ".format(
"second" if retry == 0 else "third"
)
while True:
answer = input(prompt).lower()
if answer == 'all':
break
answer = [x.capitalize() for x in answer.split()]
if set(answer).issubset(set(_dice_type)):
break
print("That is not possible, try again!")
retry += 1
if answer == 'all':
print("Ok, done")
break
tmp = dices
dices = []
for x in tmp:
if _dice_type[x] in answer:
dices.append(x)
answer.remove(_dice_type[x])
def simulate(n, debug=False):
result = dict.fromkeys(_hand_mapping.keys(), 0)
for _ in range(n):
dices = [random.randint(0,5) for x in range(5)]
if debug:
print("DEBUG:", " ".join([_dice_type[d] for d in sorted(dices)]))
result[_check_hand(dices)] += 1
for k, v in _hand_mapping.items():
cnt = result[k]
print("{:<16s}: {:.2f}%".format(v, 100*cnt/n))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)