You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求5个质数和为500的组合数:代码结果与预期不符的排查

质数组合计数问题排查

需求说明:

  • 编写算法,找出所有不计顺序的5个质数组合,使其和等于指定目标值(当前目标为500)
  • 要求算法化实现,不能使用硬编码的“魔法常量”,且支持修改目标和数

遇到的问题:

  • 预期符合条件的组合有4088种,但当前C#代码仅生成3933种结果
  • 移除循环中的break终止条件后,结果数量反而更少,无法定位原因

以下是当前使用的C#代码:

using System;
using System.Collections.Generic;
using System.IO;

namespace součet_prv
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> vysledky= new List<string>();
            List<int> prvocisla= new List<int>();            
            prvocisla = prvocislo();
            vysledky = soucet(prvocisla);
            vypis(vysledky);
        }
        static List<int> prvocislo()
        {
            int x = 500;
            List<int> list = new List<int>();
            for (int i = 2; i <= x; i++)
            {
                list.Add(i);
            }
            for (int i = 2; i <= x; i++)
            {
                for (int y = i * 2; y <= x; y += i)
                {
                    list.Remove(y);
                }
            }
            return list;
        }
        static List<string> soucet(List<int>cisla)
        {
            List<string> list = new List<string>();
            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int e = 0;
            while (e < cisla.Count)
            {
                if (cisla[a] + cisla[b] + cisla[c] + cisla[d] + cisla[e] > 500)
                {
                    break;
                }
                while (d < cisla.Count)
                {
                    if (cisla[a] + cisla[b] + cisla[c] + cisla[d] + cisla[e] > 500)
                    {
                        break;
                    }
                    while (c < cisla.Count)
                    {
                        if (cisla[a] + cisla[b] + cisla[c] + cisla[d] + cisla[e] > 500)
                        {
                            break;
                        }
                        while (b<cisla.Count)
                        {
                            if (cisla[a] + cisla[b] + cisla[c] + cisla[d] + cisla[e] > 500)
                            {
                                break;
                            }
                            while (a < cisla.Count)
                            {

                                if (cisla[a] + cisla[b] + cisla[c] + cisla[d] + cisla[e] > 500)
                                {
                                    break;
                                }
                                if (cisla[a] + cisla[b] + cisla[c] + cisla[d] + cisla[e]==500)
                                {
                                    list.Add(cisla[a] + "+" + cisla[b] + "+" + cisla[c] + "+" + cisla[d] + "+" + cisla[e] + "= 500");                                   
                                }
                                a++;
                            }                   
                            b++;
                            a = b;
                        }                       
                        c++;
                        b = c;
                    }
                    d++;
                    c = d;
                }
                e++;
                d = e;
            }
            return list;
        }
        static void vypis(List<string> vysledky)
        {
            using(StreamWriter sw = new StreamWriter("vypis.txt"))
            {
                for (int i = 0; i < vysledky.Count; i++)
                {
                    sw.WriteLine(vysledky[i]);
                }
                sw.WriteLine(vysledky.Count);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Daniel Masek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 21:33:13