使用java解决简单数学题

今天遇见别人给的几道题,尝试用java程序解决。

大家看看对不对。提出自己的意见。

 

 

 1 import org.junit.Test;
 2 
 3 public class test02math {
 4     @Test
 5     public void math1(){
 6         int money = 100;
 7         int cock_money = 5;
 8         int hen_money = 3;
 9         float chick_money = 1/3f;
10         int cock,hen,chick;
11         for (int j = 1; j <= money / cock_money; j++) {
12             cock = j;
13             for (int k = 1; k <= money / hen_money - j; k++) {
14                 hen = k;
15                 for (int l = 3; l <= money/chick_money/3 - k - j; l+=3) {
16                     chick = l;
17                     System.out.println("有"+cock+"只公鸡");
18                     System.out.println("有"+hen+"只母鸡");
19                     System.out.println("有"+chick+"只小鸡");
20                     if(cock*cock_money+hen*hen_money+chick*chick_money==money&&cock+hen+chick==100){
21                         System.out.println("此方案成功!");
22                         System.out.println(cock+"*"+cock_money+"+"+hen+"*"+hen_money+"+"+chick+"*"+chick_money+"="+money);
23                         System.out.println(cock*cock_money+"+"+hen*hen_money+"+"+chick*chick_money+"="+money);
24                         System.out.println("===================");
25                         System.exit(0);
26                     }else{
27                         System.out.println("此方案错误!");
28                         System.out.println("===================");
29                     }
30                 }
31             }
32         }
33     }
34 
35     @Test
36     public void math2(){
37         int day = 0;
38         int count = 0;
39         int a = 325;
40         int b = 325;
41         int c = 325;
42         while(day<365){
43             day++;
44             a += 150;
45             b += 120;
46             c += 90;
47             System.out.println("第"+day+"天甲共走了"+(a-325)+"里");
48             System.out.println("第"+day+"天乙共走了"+(b-325)+"里");
49             System.out.println("第"+day+"天丙共走了"+(c-325)+"里");
50             System.out.println("----------------------------");
51             if(a%325==0){
52                 count++;
53                 System.out.println("甲在第"+day+"天回到原点!");
54             }
55             if(b%325==0){
56                 count++;
57                 System.out.println("乙在第"+day+"天回到原点!");
58             }
59             if(c%325==0){
60                 count++;
61                 System.out.println("丙在第"+day+"天回到原点!");
62             }
63             if(count==3){
64                 System.out.println("三人在第"+day+"天同时回到原点!");
65                 break;
66             }else{
67                 count=0;
68             }
69         }
70     }
71     @Test
72     public void math03(){
73         for (float i = 2f; i < 10000; i++) {
74             if(i/2/20==1775/(i+14+20)){
75                 System.out.println("这座城的边长为"+(int)i+"步");
76             }
77         }
78     }
79 }

 

帮助大家拓展思维,能够使用java解决各种问题

 

转载于:https://www.cnblogs.com/YLTzxzy/p/11351461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值