鱼C论坛

 找回密码
 立即注册
查看: 3843|回复: 13

题目39:如果p是直角三角形{a,b,c}的周长,1000以下的p中哪一个具有最多的解?

[复制链接]
发表于 2015-5-2 11:24:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Integer right triangles

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

题目:

如果 p 是一个直角三角形的周长,三角形的三边长 {a, b, c} 都是整数。对于 p = 120 一共有三组解:

{20,48,52}, {24,45,51}, {30,40,50}

对于 1000 以下的 p 中,哪一个能够产生最多的解?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-30 13:16:45 | 显示全部楼层
周长:12
3,4,5
周长:24
6,8,10
周长:30
5,12,13
周长:36
9,12,15
周长:40
8,15,17
周长:48
12,16,20
周长:56
7,24,25
周长:60
10,24,26
15,20,25
周长:70
20,21,29
周长:72
18,24,30
周长:80
16,30,34
周长:84
12,35,37
21,28,35
周长:90
9,40,41
15,36,39
周长:96
24,32,40
周长:108
27,36,45
周长:112
14,48,50
周长:120
20,48,52
24,45,51
30,40,50
周长:126
28,45,53
周长:132
11,60,61
33,44,55
周长:140
40,42,58
周长:144
16,63,65
36,48,60
周长:150
25,60,65
周长:154
33,56,65
周长:156
39,52,65
周长:160
32,60,68
周长:168
21,72,75
24,70,74
42,56,70
周长:176
48,55,73
周长:180
18,80,82
30,72,78
45,60,75
周长:182
13,84,85
周长:192
48,64,80
周长:198
36,77,85
周长:200
40,75,85
周长:204
51,68,85
周长:208
39,80,89
周长:210
35,84,91
60,63,87
周长:216
54,72,90
周长:220
20,99,101
周长:224
28,96,100
周长:228
57,76,95
周长:234
65,72,97
周长:240
15,112,113
40,96,104
48,90,102
60,80,100
周长:252
36,105,111
56,90,106
63,84,105
周长:260
60,91,109
周长:264
22,120,122
66,88,110
周长:270
27,120,123
45,108,117
周长:276
69,92,115
周长:280
35,120,125
56,105,119
80,84,116
周长:286
44,117,125
周长:288
32,126,130
72,96,120
周长:300
50,120,130
75,100,125
1000下最多组解的有4
为周长=240周长:240
15,112,113
40,96,104
48,90,102
60,80,100
请按任意键继续.


代码写得好辣鸡
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. using namespace std;
  6. //输出p(周长)的解组
  7. int maxx = 0,maxN;

  8. bool isin(vector<string> &result,string str)
  9. {
  10.         for (int n =0;n<result.size();n++)
  11.         {
  12.                 if(result[n] == str)
  13.                         return true;
  14.         }
  15.         return false;
  16. }
  17. void calc(int p)
  18. {

  19.         int n = 0;
  20.         int j = 0;
  21.         vector<string> result;
  22.        
  23.                 for (int i=1;i<p;i++)
  24.                 {
  25.                         for (int b=i;b<p;b++)
  26.                         {
  27.                                 int c = p-i-b;
  28.                                 if(c<=0)
  29.                                         continue;
  30.                                         int bian[3]={i,b,c};
  31.                                         sort(bian,bian+3); //斜边是bian[2]

  32.                                         if(i+b+c == p)
  33.                                         {

  34.                                        
  35.                                                 //直角三角形
  36.                                                 if(bian[0]*bian[0] + bian[1]*bian[1] == bian[2]*bian[2])
  37.                                                 {
  38.                                                         //
  39.                                                         string temp;
  40.                                                         char buf[20];
  41.                                                         itoa(bian[0],buf,10);
  42.                                                         temp = buf;
  43.                                                         temp+=',';
  44.                                                         itoa(bian[1],buf,10);
  45.                                                         temp += buf;
  46.                                                         temp+=',';
  47.                                                         itoa(bian[2],buf,10);
  48.                                                         temp += buf;
  49.                                                        

  50.                                                         if(!isin(result,temp))
  51.                                                         result.push_back(temp);
  52.                                                 }

  53.                                         }

  54.                                 }
  55.                        
  56.                 }
  57.                
  58.                 if(result.size())
  59.                         std::cout<<"周长:"<<p<<endl;
  60.                 for (int i = 0; i < result.size(); i++)
  61.                 {
  62.                         std::cout<<result[i]<<endl;
  63.                 }
  64.                

  65.                 if(result.size()>maxx)
  66.                 {
  67.                         maxN = p;
  68.                         maxx = result.size();
  69.                 }
  70. }
  71. int main(void)
  72. {
  73.        
  74.         for (int i = 2;i<=300;i++)
  75.         {
  76.                 calc(i);
  77.         }
  78.         std::cout<<"1000下最多组解的有"<<maxx<<endl;
  79.         std::cout<<"为周长="<<maxN;
  80.         calc(maxN);
  81.         return 0;
  82. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-6 22:24:54 | 显示全部楼层
  1. import math
  2. def Jie(x):
  3.       list1 = []
  4.       for i in range(1,x):
  5.             temp = math.sqrt(x**2 - i**2)
  6.             if len(str(temp)) == 3 or len(str(temp))==4 or len(str(temp))== 5:
  7.                   total = temp + x + i
  8.                   list1.append([total,temp,x,i])
  9.       return list1
  10. list1 = []
  11. for i in range(1000):
  12.       total  = Jie(i)
  13.       for each in total:
  14.             if each!=[]and each[0]<1000:
  15.                   list1.append(each)
  16. list2=[]                 
  17. for each in list1:
  18.       each.sort()
  19.       if each not in list2:
  20.             list2.append(each)
  21. list3 = []
  22. for each in list2:
  23.       list3.append(each[-1])
  24. list4 = []
  25. for each in list3:
  26.       if each not in list4:
  27.             list4.append(each)
  28. temp = {}.fromkeys([i for i in list4],0)
  29. for each in list3:
  30.       if each in temp:
  31.             temp[each] += 1
  32. first = 0
  33. for each in temp.values():
  34.       if first < each:
  35.             first = each
  36. for each in temp:
  37.       if temp[each] == first:
  38.             print(each)
复制代码

写的好纠结,最多的解有8组,周长是840
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-22 00:06:28 From FishC Mobile | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-2 18:40 编辑

8组不重复解,周长840.

  1. lst=[]
  2. for a in range(1,250):
  3.   for b in range(a,500-a):
  4.     for c in range(b,500):
  5.       if a*a+b*b==c*c:
  6.         p=a+b+c
  7.         lst.append([p,a,b,c])
  8. cal=[]
  9. master = {}
  10. for each in lst:
  11.   if each[0] not in cal:
  12.     cal.append(each[0])
  13.     master[each[0]]= 1
  14.   else:
  15.     master[each[0]]+=1
  16. print (master)
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-14 15:16:20 | 显示全部楼层
  1. # encoding:utf-8
  2. # 寻找周长1000以下,能够成直角三角形最多的数
  3. from time import time
  4. def euler039():
  5.     maxcount = 0
  6.     result = []
  7.     for p in range(12, 1001):
  8.         count = 0
  9.         tmp = []
  10.         for i in range(int(p / (1 + 2 ** 0.5)) - 1, int(p / 2) + 1):
  11.             for j in range(int(i / (2 ** 0.5)) - 1, i):
  12.                 if i ** 2 == j ** 2 + (p - i - j) ** 2:
  13.                     count += 1
  14.                     tmp.append((p, i, j, (p - i - j)))
  15.                     break
  16.         if count > maxcount:
  17.             result = tmp
  18.             maxcount = count
  19.     print(maxcount, result)
  20. if __name__ == '__main__':
  21.     start = time()
  22.     euler039()
  23.     print('cost %.6f sec' % (time() - start))
复制代码


8 [(840, 348, 252, 240), (840, 350, 280, 210), (840, 357, 315, 168), (840, 364, 336, 140), (840, 370, 350, 120), (840, 375, 360, 105), (840, 394, 390, 56), (840, 401, 399, 40)]
cost 5.641026 sec
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-15 00:43:36 | 显示全部楼层
此代码使用matlab编程
Problem39所用时间为: 2.1683秒
Problem39的答案为: 840
  1. %% Problem39.m
  2. % 最后编辑时间:17-06-14 22:34
  3. % 如果p是直角三角形{a,b,c}的周长,1000以下的p中哪一个具有最多的解?
  4. % Problem39所用时间为: 2.1683秒
  5. % Problem39的答案为: 840
  6. function Output = Problem39()
  7. tic

  8. Store = [];
  9. for a = 1:250
  10.     for b = a+1:500 - a  %根据楼上老大的解法,这点不懂
  11.         for c = b+1:500
  12.             if c^2 == a^2 + b^2
  13.                 Store = [Store,a+b+c];
  14.             end
  15.         end
  16.     end
  17. end

  18. Sta = tabulate(Store);  %找到所有解的比例

  19. Value = Sta(:,1);
  20. Ration = Sta(:,3);

  21. Set = Ration == max(Ration);

  22. Output = Value(Set);

  23. toc
  24. disp('此代码使用matlab编程')
  25. disp(['Problem39所用时间为: ',num2str(toc),'秒'])
  26. disp(['Problem39的答案为: ',num2str(Output)])
  27. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-30 10:59:23 | 显示全部楼层
用的matlab
结果是:
>> Untitled

ans =

   840

时间已过 0.029216 秒。
>>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-5 23:57:24 | 显示全部楼层
najin 发表于 2017-9-30 10:59
用的matlab
结果是:
>> Untitled

额,为啥这么快。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-14 12:22:17 | 显示全部楼层
最多解为:8
用时:7.80005 秒

  1. import time


  2. def cal_result():
  3.     result = []
  4.     for p in range(12, 1000):
  5.         count = 0
  6.         for x in range(int(p//3), p):
  7.             for y in range(x-1, 0, -1):
  8.                 if x + y >= p:
  9.                     break
  10.                 else:
  11.                     z = p - x - y
  12.                     if z <= y:
  13.                         if x**2 == y**2 + z**2:
  14.                             count += 1
  15.                     else:
  16.                         break
  17.         if count != 0:
  18.             result.append((p, count))
  19.     return result

  20. result = cal_result()
  21. max_result = max(each[1] for each in result)
  22. print("最多解为:{}\n用时:{} 秒".format(max_result, time.process_time()))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-12 21:22:39 | 显示全部楼层
840

Process returned 0 (0x0)   execution time : 0.034 s
Press any key to continue.

                               
登录/注册后可看大图

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;

  4. int sol(int p,int x){
  5.   int res = 0;
  6.   for (int i = 2;i <= sqrt(x);i++){
  7.     if (x % i == 0){
  8.       if (x / i < p && i < p) res++;
  9.     }
  10.   }
  11.   return res;
  12. }

  13. int main(){
  14.   int ans;
  15.   int max_sol = 0;
  16.   for (int p = 10;p <= 1000;p+=2){
  17.     int t = sol(p,p*p/2);
  18.     if (t > max_sol) {max_sol = t;  ans = p;}
  19.   }
  20.   cout << ans << endl;
  21.   return 0;
  22. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-18 21:25:58 | 显示全部楼层
  1. #include <stdio.h>

  2. main()
  3. {
  4.         int i, j, k, a, b, c, max = 0, count = 0, num;
  5.        
  6.         for (i = 120; i < 1000; i++)
  7.         {
  8.                 j = i / 3;//a不可能超过i的三分之一
  9.                 for (a = 10; a <= j; a++)
  10.                 {
  11.                         k = i / 2;//b不可能超过i的二分之一
  12.                         for (b = a; b <= k; b++)
  13.                         {
  14.                                 c = i - a - b;
  15.                                 if (a * a + b * b == c * c)
  16.                                 {
  17.                                         count++;
  18.                                 }
  19.                                 if (a * a + b * b > c * c)
  20.                                 {
  21.                                         break;
  22.                                 }
  23.                         }               
  24.                 }
  25.                 if (max < count)
  26.                 {
  27.                         max = count;
  28.                         num = i;       
  29.                 }
  30.                 count = 0;       
  31.         }
  32.         printf("%d %d\n", num, max);
  33.        
  34. }
复制代码



840 8组
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-3 21:00:21 | 显示全部楼层
  1. /*
  2. 应用本原直角三角形a=x^2 - y^2,b=2*x*y,c=x^2 + y^2的公式来枚举
  3. 答案:840
  4. 耗时:0.0000257秒
  5. */
  6. #include <iostream>
  7. #include <cmath>
  8. using namespace std;

  9. int nCount[1001];

  10. int gcd(int x, int y)
  11. {
  12.   if (y == 0)
  13.     return x;
  14.   int z = x % y;
  15.   while (z != 0)
  16.   {
  17.     x = y;
  18.     y = z;
  19.     z = x % y;
  20.   }
  21.   return y;
  22. }

  23. int main(void)
  24. {
  25.   for (int i = 6; i <= 500; ++i)//搜索边长为2*i的本原直角三角形
  26.   {
  27.     for (int x = 2; x < sqrt((double)i); ++x)//枚举大参数
  28.     {
  29.       if (i % x == 0)
  30.       {
  31.         int y = i / x;
  32.         y -= x;//求出小参数
  33.         //检查这两个参数是否可以构成本原直角三角形
  34.         if (x > y && gcd(x, y) == 1 && ((x - y) & 1) != 0)
  35.         {
  36.           int k = 2 * i;
  37.           int j = 1;
  38.           while (k * j <= 1000)
  39.           {
  40.             ++nCount[k * j];//计数边长为k*j的直角三角形的个数
  41.             ++j;
  42.           }
  43.         }
  44.       }
  45.     }
  46.   }
  47.   //求出可以构成最多直角三角形的边长
  48.   int nMaxCount = 0, nVal;
  49.   for (int i = 12; i <= 1000; i += 2)
  50.   {
  51.     if (nCount[i] > nMaxCount)
  52.     {
  53.       nMaxCount = nCount[i];
  54.       nVal = i;
  55.     }
  56.   }
  57.   cout << nVal << endl;
  58.   return 0;
  59. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-24 17:27:06 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()
  3. p_list = {}
  4. for a in range(1, 333):
  5.     for b in range(a, int(1000 - a / 2)):
  6.         for c in range(b, (1000 - a - b)):
  7.             if a + b < c:
  8.                 break
  9.             elif a ** 2 + b ** 2 == c ** 2:
  10.                 try:
  11.                     p_list[a + b + c] += 1
  12.                 except KeyError:
  13.                     p_list[a + b + c] = 1

  14. print(max(p_list, key=lambda x: p_list[x]))
  15. print("It costs %f s" % (t.perf_counter() - start))
复制代码



840
It costs 4.748411 s
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-20 11:39:30 | 显示全部楼层
  1. $ time ./main
  2. 8 840

  3. real        0m0.034s
  4. user        0m0.034s
  5. sys        0m0.000s
复制代码
  1. fn f(x: usize) -> usize {
  2.     let mut count = 0;
  3.     for i in 1..x/3 {
  4.         for j in i..x/2 {
  5.             if i*i + j*j == (x-i-j)*(x-i-j) {
  6.                 count += 1;
  7.             }
  8.         }
  9.     }
  10.     count
  11. }

  12. fn main () {
  13.     let mut max=0;
  14.     let mut max_i = 0;
  15.     for i in 1..1000 {
  16.         if f(i) > max {
  17.             max = f(i);
  18.             max_i = i;
  19.         }
  20.     }
  21.     println!("{max} {max_i}");
  22. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 12:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表