【有奖问答】写程序解香蕉苹果梨问题

in #cn5 years ago (edited)

(image source: Pixabay)

看到一道有趣的问题:假设法简化题目,然后调整求解
题目是:香蕉一斤6元,苹果一斤3元,梨一斤5元,妈妈一共买了10斤水果,花了52元,其中苹果和梨一样多,请问各买了水果多少斤?

作为能偷懒就偷懒的草台班子,决定写个小程序解这道题。

解题的思路是,让设i为未知斤香蕉,设j为未知斤苹果,设k为未知斤梨。
i,j,k从0-9搭配,直到i斤香蕉,j斤苹果和k斤梨加起来的价格等于52,并且j等于k(苹果和梨一样多),并且i+j+k等于10(妈妈一共买了10斤水果)

这是程序的代码:

        int banana = 6;
        int apple = 3;
        int pear = 5;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j <10; j++)
                for (int k = 0; k < 10; k++) {
                    int cost = banana * i + apple * j + pear * k;
                    if (cost == 52 && j == k && i+j+k==10) {
                        System.out.println("Banana: " + i);
                        System.out.println("Apple: " + j);
                        System.out.println("Pear: " + k);
                    }
                }
        }

答案:
Banana: 6
Apple: 2
Pear: 2

是不是很简单的把这道题给解了~

最后出道题给大家
题目是:香蕉一斤6元,苹果一斤3元,梨一斤5元,妈妈一共花了252元,其中苹果数量是香蕉的2倍,梨的数量是苹果的3倍,请问各买了水果多少斤?
谁最快解出将获得1 SBI奖励~
提示:把上面的代码修改一下就可以迅速解答~
上面的代码我已经放到在线编译器里:https://rextester.com/KZMF76887, 你需要修改几个参数就可以解这题(想运行程序点击”Run it“ 或者按F8)


Posted from my blog with SteemPress : http://ericet.vornix.blog/2019/01/23/%e3%80%90%e6%9c%89%e5%a5%96%e9%97%ae%e7%ad%94%e3%80%91%e5%86%99%e7%a8%8b%e5%ba%8f%e8%a7%a3%e9%a6%99%e8%95%89%e8%8b%b9%e6%9e%9c%e6%a2%a8%e9%97%ae%e9%a2%98/


Sort:  




This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

3个loops,complexity太高拉。不如用线性代数
1 1 1 x 10
6 3 5 y = 52
0 1 -1 z 0
不太好打,x香蕉,y苹果,z梨。大家理解了吗?

Posted using Partiko Android

是的,但是解这样的问题complexity不在考虑中。
线性代数已经还给老师了😂

Posted using Partiko iOS

@zy-sb给您叫了一份外卖!

@also.einstein 亦亦 迎着台风 开着轮船给您送来
精装版带榛果 大颗粒 不加价

吃饱了吗?跟我猜拳吧! 石头,剪刀,布~

如果您对我的服务满意,请不要吝啬您的点赞~
@onepagex

剪刀

Posted using Partiko Android


It’s a tie! 平局!想不到你也出鸡爪!

石头

Posted using Partiko Android


You lose! 你输了!愿赌服输,请给我点赞~

😂😂那是你们攻城狮的偷懒工具,对我们来说这不是偷懒,是给自己挖坑……

Posted using Partiko Android

特别同意!!!
我按照自己的理解改了他那个代码,贴过来,可是哪里会出答案嘛😂

Posted using Partiko Android

😂😂咱们连在哪里码也不知道……

Posted using Partiko Android

Thank you for using CryptoSicko!

Well done and thank you for joining and supporting my subscription based upvoting bot that provides you with daily upvotes in exchange for your spare STEEM POWER.
Delegation links for CryptoSicko
100SP 250SP500SP

村长挖了个坑,我算出来跟北北一样,但是我的常识以为要整数斤,所以一直在想错在哪里?
@teamcn-shop
给村长上个榴莲

Posted using Partiko Android

        int banana = 6;
        int apple = 3;
        int pear = 5;
        for (int i = 0;  i++) {
            for (int j = 0; j++)
                for (int k = 0; ; k++) {
                    int cost = banana * i + apple * j + pear * k;
                    if (cost ==252 && j == 2i&& k== 3i) {
                        System.out.println("Banana: " + i);
                        System.out.println("Apple: " + j);
                        System.out.println("Pear: " + k);
                    }
                }
        }

答案呢?😂

Posted using Partiko iOS

这个代码对吗?应该怎么出答案?要贴到什么软件里吗?

Posted using Partiko Android

代码没错,就是语法有点问题。对于没有编程知识的人已经很棒了!
厉害北北~

Posted using Partiko iOS

感觉这里有领域常识陷阱
就是,你所以为的常识,在领域外的没有知识背景的人看来,特别费解😂

其实是以另外一种方式来解题,靠机械的方式

Posted using Partiko iOS

明白
就是说,我可能能按照你的模板改一下"公式"
但是一点儿也不了解编程
所以你说的特别简单的出现答案
在我看来比较费解
坐等答案,好期待

Posted using Partiko Android

的确,对于有一点编程知识的人,会很简单。
下回我会弄个在线编辑器,只需要修改几个参数就可以解出来

Posted using Partiko iOS

我在提示那里加了在线编译器,把我上面的代码放进去了。你只需修改几个参数就可以解了。按Run it运行程序。

Thank you so much for participating the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!

一点都不简单。完全认同@windowglass所讲。根本就是欺负我们不懂些程序。😵😵😵

Posted using Partiko Android

哈哈哈,下回放个在线编辑器,只需修改几个参数就可以解

Posted using Partiko iOS

解:设香蕉=x,苹果=2x,梨=3x
价格=6x+32x+53x=252
解方程发现x无整数解😂

我才发现我题目出错了。。。
梨是苹果的3倍。
香蕉=x,苹果=2x,梨=6x

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.033
BTC 61959.02
ETH 3004.67
USDT 1.00
SBD 3.59