算法习题

这篇博客详细记录了从第一次到第七次的上机练习,涵盖了多项算法习题,包括曹冲称象、平方和问题、另一个Fibonacci数列、波兰表达式等。每个习题都有清晰的Input、Output示例以及Codeing实现,旨在提升读者的算法理解和应用能力。
摘要由CSDN通过智能技术生成

第一次上机练习

习题B:曹冲称象

这里写图片描述

Input:

The input contains several test cases. Each test cases consists of a number N N ( 0 < n 100 ) which indicates the number of stones. The next line consists N N integers, indicate each stone’s weight.
For you attention:each of stone’s weight will be no more than 100.

Ouput:

For each case, output the elephant’s weight.

Sample Input:

5
2 4 3 1 5
3
100 100 100

Sample Output:

15
300

Codeing:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int num = scanner.nextInt();
            int input[] = new int[num];
            int sum = 0;


            for (int i = 0; i < num; i++) {
                input[i] = scanner.nextInt();
                sum = sum + input[i];
            }
            System.out.println(sum);

        }
    }


}

习题C:平方和问题

description:

Some integers can be expressed by square sum of other integers.
For example:
25 = 1 2 + 2 2 + 2 2 + 4 2
25=32+42 25 = 3 2 + 4 2
25=52 25 = 5 2
So there are 3 ways to express 25 25 . The problem is how many ways the integer N N can be expressed?
In the problem a integer can be expressed by four integers square sum at most. Pay attention change order is same way. For example, the way 3 2 + 4 2 is same with 42+32 4 2 + 3 2 .

Input:

The first line is the number of cases, a integer T T ( 1 T 20000 ).
For each sample, there are only one line and the line is only one integer N(

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值