m0_54132659 2021-03-10 11:20 采纳率: 83.3%
浏览 124
已结题

c# 编程题,请会的大佬直接留下代码,输入输出得一致

Here is a wheel with 10 numbers:

wheel

Suppose that the wheel begins in the position above. We are given an integer N in the range 0 ≤ N ≤ 100. We will spin the wheel three times. On each of the three spins, the wheel will rotate counterclockwise by a number of positions randomly chosen (uniformly) from the integers 0 .. N. For example, if N = 3, then on each spin there is

  • a 1/4 chance that it will not rotate
  • a 1/4 chance that it will rotate by 1 position
  • a 1/4 chance that it will rotate by 2 positions
  • a 1/4 chance that it will rotate by 3 positions

What is the exact probability that after the 3 spins the wheel will end up in the same position in which it started, i.e. pointing to number 1?

Write a program that can answer this question. The program should read the integer N on a single line of standard input, and write the answer as a fraction in reduced form.

Sample input:

3

Output:

1/64

Explanation: When N = 3, the wheel cannot possibly make a complete rotation in 3 spins. So it will end up at the starting position only if it never moves. On each spin, the probability that it won't move is 1/4, so the probability that it never moves is

    (1/4)(1/4)(1/4) = 1/64

Sample input #2:

9

Output:

1/10

Explanation:

When N = 9, the wheel will be in a (uniformly) random position after every spin. Therefore after 3 spins (or any number of spins, for that matter), the probability that it will be pointing to the number 1 is 1/10.

Sample input #3:

7

Output:

13/128

Hint:

Consider every possible sequence of 3 spins, and count how many of them end at the starting position. The probability of ending at the starting position is this count divided by the number of possible sequences.

  • 写回答

1条回答 默认 最新

  • ProfSnail 2021-03-10 12:55
    关注

     因为题目的n比较小,是一百以内的,O(n^3)也只有10^6,所以暴力解就行了,一般不会超时。

    using System;
    namespace HelloWorldApplication
    {
        class HelloWorld
        {
    		static int gcd(int a, int b){
    			if(b == 0)return a;
    			return gcd(b, a%b);
    		}
            static int Main(string[] args)
            {	
    			
    			int n = int.Parse(Console.ReadLine());
    			//int n = 3;
    			if(n == 0){
    				Console.WriteLine("1");
    				return 0;
    			}
    			int trueNum=0, allNum=0;
    			for(int i = 0; i <= n; i++){
    				for(int j = 0; j <= n; j++){
    					for(int k = 0; k <= n; k++){
    						int sum = i + j + k;
    						if(sum % 10 == 0){
    							trueNum++;
    						}
    						allNum++;
    					}
    				}
    			}
    			int ggcd = gcd(trueNum, allNum);
    			trueNum /= ggcd;
    			allNum /= ggcd;
    			Console.WriteLine(trueNum+"/"+allNum);
    			return 0;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵