有人可以解释这段代码的LOGIC吗? [英] Can someone explain the LOGIC of this code ?

查看:95
本文介绍了有人可以解释这段代码的LOGIC吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>

Scanner sc = new Scanner(System.in);



long sum = 0;



int N = sc.nextInt();



for(int i = 0; i< N; i ++){



final long x = sc.nextLong(); //读取输入



String str = Long.toString((long)Math.pow(1<< 1,x));



str = str.length()> 2? str.substring(str.length() - 2):str;



sum + = Integer.parseInt(str);

< br $>
}



我想写一个等价的python程序

任何人都可以帮我理解逻辑这个Java代码?




输入

4 8 6 7 4

输出

64



例2



输入

3

1 2 3



输出

14



我尝试过:



n = int(input())

nums = list (map(int,input()。split(''))

而n> 2:

#LOGIC

Scanner sc = new Scanner(System.in);

long sum = 0;

int N = sc.nextInt();

for (int i = 0; i < N; i++) {

final long x = sc.nextLong(); // read input

String str = Long.toString((long) Math.pow(1 << 1, x));

str = str.length() > 2 ? str.substring(str.length() - 2) : str;

sum += Integer.parseInt(str);

}

I want to write a equivalent python program
Can anyone help me understand the logic of this Java Code ?


Input
4 8 6 7 4
Output
64

Example 2

Input
3
1 2 3

Output
14

What I have tried:

n = int(input())
nums = list(map(int,input().split(' '))
while n>2:
#LOGIC

推荐答案

所以你找到了一些代码来完成你的作业所要求的,你想要它解释给你,这样你就可以把它作为你自己的作品提交吗?



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



而不是花时间试图找到一些东西你可以交出来,仔细阅读问题,然后编写自己的代码。你将学到更多的负担!

亲自尝试一下,你可能会发现它并不像你想象的那么难!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
So you have found some code that does what your homework asks, and you want it explained to you so you can submit it as your own work?

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.

Instead of spending your time trying to find something that you can hand in, try reading the question carefully, and writing your own code. You will learn a whole load more that way!
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!


你应该和在https://www.codeproject.com/Questions/1255912/How-do-I-solve-this-我无法解决 [ ^ ]。我相信你们两个人之间可以解决这个问题。
You should get together with your colleague who posted the question at https://www.codeproject.com/Questions/1255912/How-do-I-solve-this-I-am-unable-to-solve[^]. I am sure between the two of you you can work it out.


引用:

有人可以解释一下这段代码的LOGIC?

Can someone explain the LOGIC of this code ?



使用调试器并在执行代码时观察代码,你会看到它的作用。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器上运行你的代码一步一步,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有调试器中的魔法,它不知道你的cpde应该做什么,它没有找到bug,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码正在做,你的任务是与它应该做的事情进行比较。


use a debugger and watch the code as it execute, you will see by yourself what it does.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于有人可以解释这段代码的LOGIC吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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