藍橋杯-加法變乘法(java)

來源:互聯網
上載者:User

標籤:demo   image   origin   round   lang   答案   ima   min   整數   

藍橋杯第六屆省賽題目-加法變乘法(java)

題目:

我們都知道:1+2+3+ ... + 49 = 1225

現在要求你把其中兩個不相鄰的加號變成乘號,使得結果為2015

比如:

1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015

就是符合要求的答案。

請你尋找另外一個可能的答案,並把位置靠前的那個乘號左邊的數字提交(對於樣本,就是提交10)。

注意:需要你提交的是一個整數,不要填寫任何多餘的內容。

 

我的思路:如案例:1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015

我們這樣拆分 10*11 = 10+10*10     27*28 = 27+27*27

轉化為:1+2+3+...+9+10+10*10+ 12+...+26+27+ 27*27+29+...+49 = 2015

兩邊同時加上11+28

1+2+3+...+9+10+10*10+11+ 12+...+26+27+ 27*27+28+29+...+49 = 2015+11+28

轉化為:  1225+ 10*10+27*27 = 2015+11+28= 2017+10+27

根據這個思路:使用兩層迴圈i,j分別代表乘號的位置得到判斷條件

                  1225+ i*i +  j*j  ==  2017+i+j

 

public class Demo6 {    public static void main(String[] args) {        for (int i = 1; i < 49; i++) {for (int j = i + 2; j < 48; j++) {                if (1225 + i * i + j * j == 2017 + i + j) {                    System.out.println(i);                }            }        }    }}

 

 

執行結果:10  16

答案:16

 

 

藍橋杯-加法變乘法(java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.