我如何在java / C / C ++中解决这个问题?我的解决方案是显示TLE(延长时间限制)!我猜,算法必须以其他方式完全重新设计! [英] How do I solve this in java/ C/ C++? My solution is showing TLE (time limit extended)! algo has to be completely redesigned in some other way, I guess!

查看:92
本文介绍了我如何在java / C / C ++中解决这个问题?我的解决方案是显示TLE(延长时间限制)!我猜,算法必须以其他方式完全重新设计!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个整数区间[A,B]。对于此区间中的每个数,计算其最大奇数除数。输出这些除数的总和。



输入

第一行包含一个整数T,表示将遵循的测试用例数量。 />
每个测试用例包含一行,包含两个整数值A和B.



输出

输出应包含每个测试用例在不同行上的答案。

每个答案都包含一个整数值。





样品输入

3

1 3

1 4

3 9



样品输出

5

6

29



说明

1 + 1 + 3 = 5

1 + 1 + 3 + 1 = 6

3 + 1 + 5 + 3 + 7 + 1 + 9 = 29



我的尝试:



You are given an interval of integers [A, B].For each number in this interval compute its greatest odd divisor. Output the sum of these divisors.

Input
The first line contains an integer T representing the number of test cases that will follow
Each test case consists of one line containing two integer values A and B.

Output
The output should contain the answer for each test case on a different line.
Each answer consists of a single integer value.


Sample Input
3
1 3
1 4
3 9

Sample Output
5
6
29

Explanation
1+1+3=5
1 + 1 + 3 + 1 = 6
3 + 1 + 5 + 3 + 7 + 1 + 9 = 29

What I have tried:

import java.util.Scanner;
  
class Main {
      public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scn = new Scanner(System.in);
        int t = scn.nextInt();
        while (t-- > 0) {
            int l = scn.nextInt();
            int r = scn.nextInt();
            long sum = 0;
            for (int i = l; i <= r; i++) {
                sum += oddDivisor(i);
            }
            System.out.println(sum);
        }
    }
  
    public static long oddDivisor(int num) {
        long sum = 1;
        for (int i = 1; i * i <= num; i++) {
            if (i * i == num && i%2==1) {
                sum=i;
            } else {
                if (num % i == 0) {
                    if (i % 2 == 1) {
                        sum = i;
                    }
                    if ((num / i) % 2 == 1) {
                        sum = num / i;
                    }
                }
            }
        }
        return (((num%2)==0)?sum:num);
    }
}

推荐答案

您已在如何在java / C / C ++中解决这个问题?我的解决方案是显示TLE(延长时间限制)!我想,算法必须以其他方式完全重新设计! [ ^ ],并收到了一些有用的建议。请不要重新发布同样的问题。
You already posted this question at How do I solve this in java/ C/ C++? My solution is showing TLE (time limit extended)! algo has to be completely redesigned in some other way, I guess![^], and received some useful suggestions. Please do not repost the same question.


现在你开始接受micky了。

这是你的第二个功课问题,标题相同。 />


我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!
Now you are starting to take the micky.
This is your second homework question, with the same title.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


在所有关键代码路径中增强输出,即在if和return语句中。它将帮助您理解代码的工作。



奇数除数的总和为3:1 + 3 = 4

1和3的奇数除数之和:1 +(1 + 3)= 5



或者我错过了什么?



谷歌有一些更好的奇数除数。如果我没记错,奇数除数应该是一个素数。这意味着如果是素数,则进行递归调用。



Profi-tip编写一些测试函数来证明您的结果。与8,24或70一样。
Enhance the output in all critical code pathes, ie on the if and return statement. It will help you to understand the working of your code.

The sum of odd divisors of 3: 1 + 3 = 4
The sum of odd divisors of 1 and 3: 1 + (1 + 3) = 5

Or am I missing somewhat?

Google for some better odd divisor. If I remember right the odd divisor should be a prime integer. That means a recursive call if not a prime number.

Profi-tip write some testing functions to prove your results. Like with 8, 24 or 70.


这篇关于我如何在java / C / C ++中解决这个问题?我的解决方案是显示TLE(延长时间限制)!我猜,算法必须以其他方式完全重新设计!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆