精华内容
下载资源
问答
  • Loops

    2021-03-30 20:44:37
    Loops There are two kind of loops in Java,forandwhile. For The for loop has three sections: for (int i = 0; i < 3; i++) {} First section runs once when we enter the loop. Second section is ...

    Loops


    There are two kind of loops in Java, for and while.


    For

    The for loop has three sections:

    for (int i = 0; i < 3; i++) {}
    

    First section runs once when we enter the loop.

    Second section is the gate keeper(门卫), if it returns true, we run the statements in the loop, if it returns false, we exit the loop. It runs right after the first section for the first time(第一次), then every time the loop is finished and the third section is run.

    The third section is the final statement that will run every time(每次) the loop runs.

    So in the case we have just seen, the loop will run 3 times. Here is the order of the commands:

    int i = 0;
    i < 3 // 0 < 3 = true
          // Inside of loop
    i++ // i is now 1
    i < 3 // 1 < 3 = true
          // Inside of loop
    i++ // i is now 2
    i < 3 // 2 < 3 = true
          // Inside of loop
    i++ // i is now 3
    i < 3 // 3 < 3 = false
          // Loop is done...
    

    We can omit(省略) the first and third section of the loop (although it will be weird 不寻常的), and the loop will still work:

    for (;i < 5;) {}
    

    For cases where we want to use a loop that look like that, we use a while loop


    While

    The syntax is very similar to the previous for we looked at:

    while (condition) {}
    

    The condition will run for the first time when entering and every time the loop is done. If it returns false, the loop will not run.

    If we want the loop to always run at least one(至少一次), we can use do-while

    do {
    
    } while(condition);
    

    Notice the ; in the end of the do-while.


    Foreach 遍历数组

    Another version of for, is the foreach. The keyword we use is still for, but when we want to iterate on the elements inside an array we can simply use it:

    int[] arr = {2, 0, 1, 3};
    for (int el : arr) {
        System.out.println(el);
    }
    

    This is a short version and equivalent to:

    int[] arr = {1, 9, 9, 5};
    for (int i = 0; i < arr.length; i++) {
        int el = arr[i];
        System.out.println(el);
    }
    

    Notice that if you want to use the index of the element inside the loop, you have to use the longer version and can't use foreach.


    break and continue

    These two keywords help us control the loop from within it. break will cause the loop to stop and will go immediately to the next statement after the loop:

    int i;
    for (i = 0; i < 5; i++) {
        if (i >= 2) {
            break;
        }
        System.out.println("Yuhu");
    }
    System.out.println(i);
    // Output:
    // Yuhu
    // Yuhu
    // 2
    

    continue will stop the current iteration and will move to the next one. Notice that inside a for loop, it will still run the third section.

    int i;
    for (i = 0; i < 5; i++) {
        if (i >= 3) {
            break;
        }
        System.out.println("Yuhu");
        if (i >= 1) {
            continue;
        }
        System.out.println("Tata");
    }
    System.out.println(i);
    // Output
    // Yuhu
    // Tata
    // Yuhu
    // Yuhu
    // 3
    

    Exercise

    Loop through and print out all even numbers(偶数), each in a separate line, from the numbers list in the same order they are received. Don't print any numbers that come after 237 in the sequence.(该序列237后面的数字就不用打印了)

    public class Main {
        public static void main(String[] args) {
            int[] numbers = {
                951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
                615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
                386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
                399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
                815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
                958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
                743, 527};
            
            for (int i = 0; i < numbers.length; i++) {
                int el = numbers[i];
    
                if (el == 237) {
                    break;
                }
    
                // using the division remainder operator %
                if (el % 2 == 0) {
                    System.out.println(el);
                }
    
            }
        }
    }

     

     

     

    展开全文
  • LOOPS

    2016-08-23 17:58:00
    LOOPS 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3853 DP 设dp[i][j]为(i,j)到终点期望的使用魔力值,mp[i][j][k]为(i,j)到三个方向的概率。 那么,dp[i][j]=(2+dp[i][j+1])*mp[i][j][1...

    LOOPS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3853

    DP

    设dp[i][j]为(i,j)到终点期望的使用魔力值,mp[i][j][k]为(i,j)到三个方向的概率。

    那么,dp[i][j]=(2+dp[i][j+1])*mp[i][j][1]+(2+dp[i+1][j])*mp[i][j][2]+(2+dp[i][j])*mp[i][j][0],

    移向得,dp[i][j]*(1-mp[i][j][0])=(2+dp[i][j+1])*mp[i][j][1]+(2+dp[i+1][j])*mp[i][j][2]+2*mp[i][j][0],

    化简得,dp[i][j]=(2+dp[i][j+1]*mp[i][j][1]+dp[i+1][j]*mp[i][j][2])/(1-mp[i][j][0]).

    注意当mp[i][j][0]==1时,此时将陷入LOOPS.

    代码如下:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #define EPS 1e-8
     5 #define N 1005
     6 #define INF 1000005
     7 using namespace std;
     8 int n,m;
     9 double mp[N][N][3];
    10 double dp[N][N];
    11 int main(void){
    12     while(~scanf("%d%d",&n,&m)){
    13         memset(dp,0,sizeof(dp));
    14         for(int i=0;i<n;++i)
    15         for(int j=0;j<m;++j)
    16         for(int k=0;k<3;++k)
    17             scanf("%lf",&mp[i][j][k]);
    18         for(int i=n-1;i>=0;--i)
    19         for(int j=m-1;j>=0;--j){
    20             if(i==n-1&&j==m-1)continue;
    21             if(fabs(1-mp[i][j][0])<EPS)dp[i][j]=INF;
    22             else dp[i][j]=(2+dp[i][j+1]*mp[i][j][1]+dp[i+1][j]*mp[i][j][2])/(1-mp[i][j][0]);
    23         }
    24         printf("%.3lf\n",dp[0][0]);
    25     }
    26 }

     

    转载于:https://www.cnblogs.com/barrier/p/5800401.html

    展开全文
  • loops

    2010-12-20 23:17:00
    #include /***********************************************************************... * Author : loops * Date : 2007.11.28 * Description: Remove sub string s2 from s1 without invoking any other funct

    展开全文
  • Loops-源码

    2021-03-09 10:15:55
    Loops
  • On Elementary Loops and Proper Loops for Disjunctive Logic Programs
  • HDU3853 LOOPS

    2017-10-30 11:13:28
    LOOPS

    LOOPS

    传送门1
    传送门2
    Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).

    Homura wants to help her friend Madoka save the world. But because of the plot of the Boss Incubator, she is trapped in a labyrinth called LOOPS.
    寻找唯一的出口
    The planform of the LOOPS is a rectangle of R*C grids. There is a portal in each grid except the exit grid. It costs Homura 2 magic power to use a portal once. The portal in a grid G(r, c) will send Homura to the grid below G (grid(r+1, c)), the grid on the right of G (grid(r, c+1)), or even G itself at respective probability (How evil the Boss Incubator is)!
    At the beginning Homura is in the top left corner of the LOOPS ((1, 1)), and the exit of the labyrinth is in the bottom right corner ((R, C)). Given the probability of transmissions of each portal, your task is help poor Homura calculate the EXPECT magic power she need to escape from the LOOPS.

    Input

    The first line contains two integers R and C (2 <= R, C <= 1000).

    The following R lines, each contains C*3 real numbers, at 2 decimal places. Every three numbers make a group. The first, second and third number of the cth group of line r represent the probability of transportation to grid (r, c), grid (r, c+1), grid (r+1, c) of the portal in grid (r, c) respectively. Two groups of numbers are separated by 4 spaces.

    It is ensured that the sum of three numbers in each group is 1, and the second numbers of the rightmost groups are 0 (as there are no grids on the right of them) while the third numbers of the downmost groups are 0 (as there are no grids below them).

    You may ignore the last three numbers of the input data. They are printed just for looking neat.

    The answer is ensured no greater than 1000000.

    Terminal at EOF

    Output

    A real number at 3 decimal places (round to), representing the expect magic power Homura need to escape from the LOOPS.

    Sample Input

    2 2
    0.00 0.50 0.50 0.50 0.00 0.50
    0.50 0.50 0.00 1.00 0.00 0.00

    Sample Output

    6.000


    题意

    有一个RC的迷宫,从(1,1)走到(R,C),每个格子有留在原地,向右走一格,向下走一格的概率,且每走一格要2能量,求最后需要的能量期望。

    分析

    psy(pstay),prt(pright),pdn(pdown)记录概率。
    当然正推和逆推都行。

    逆推

    逆推时是期望dp。
    定义dp[i][j]表示从(i,j)(r,c)的期望,则

    dp[i][j]=prt[i][j]dp[i][j+1]+pdn[i][j]dp[i+1][j]+21psy[i][j].
    当然psy[i][j]=1dp[i][j]=0.
    顺推

    比较难懂!!!dalao请无视
    顺推时是概率dp。
    定义dp[i][j]表示从(1,1)(i,j)的概率,则当psy[i][j]不为1时
    dp[i][j]/=(1psy[i][j]);
    dp[i+1][j]+=(dp[i][j]pdn[i][j]);
    dp[i][j+1]+=(dp[i][j]prt[i][j]);
    用ans记录期望:
    只要 psy[i][j]1ans+=dp[i][j]2
    (如果psy[i][j]==1则该点无法到终点或就是终点)

    CODE

    逆推
    #include<cstdio>
    #include<memory.h>
    #define N 1005
    #define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++)
    #define ROF(i,a,b) for(int i=(a),i##_END_=(b);i>=i##_END_;i--)
    using namespace std;
    double dp[N][N],psy[N][N],prt[N][N],pdn[N][N];
    
    int main() {
        int r,c;
        while(~scanf("%d%d",&r,&c)) {
            memset(dp,0,sizeof dp);
            FOR(i,1,r)FOR(j,1,c)
                scanf("%lf%lf%lf",&psy[i][j],&prt[i][j],&pdn[i][j]);
            ROF(i,r,1)ROF(j,c,1) {
                if(psy[i][j]==1)continue;
                dp[i][j]=(prt[i][j]*dp[i][j+1]+pdn[i][j]*dp[i+1][j]+2)/(1-psy[i][j]);//从它下面和右边转移上来
            }
            printf("%.3lf\n",dp[1][1]);
        }
        return 0;
    }
    顺推
    #include<cstdio>
    #include<memory.h>
    #define N 1005
    #define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++)
    #define ROF(i,a,b) for(int i=(a),i##_END_=(b);i>=i##_END_;i--)
    double dp[N][N],psy[N][N],prt[N][N],pdn[N][N];
    
    int main() {
        int r,c;
        while(~scanf("%d%d",&r,&c)) {
            memset(dp,0,sizeof dp);
            FOR(i,1,r)FOR(j,1,c)
                scanf("%lf%lf%lf",&psy[i][j],&prt[i][j],&pdn[i][j]);
            double ans=0;
            dp[1][1]=1;
            FOR(i,1,r)FOR(j,1,c) {
                if(psy[i][j]==1)continue;
                dp[i][j]/=(1-psy[i][j]);
                ans+=dp[i][j]*2;
                dp[i+1][j]+=(dp[i][j]*pdn[i][j]);
                dp[i][j+1]+=(dp[i][j]*prt[i][j]);
            }
            printf("%.3lf\n",ans);
        }
        return 0;
    }
    
    展开全文
  • Reducing loops

    2020-12-09 10:13:58
    <p>I was wondering if we could reduce the number of loops in the startup., eg currently it loops through all the points twice, once for <code>pointsAsObjects</code> and then again for <code>new geomIn...
  • For loops

    2020-12-26 17:30:04
    <code>for x in 1..10 {}</code> - loops through 1 (inclusive) to 10 (exclusive) (1, 2, 3, 4, 5, 6, 7, 8, 9) With a step: <code>for x in 1..2..10 {}</code> loops through 1, 3, 5, 7, 9</p><p>该提问来源于...
  • Add loops

    2020-11-28 10:07:51
    <div><p>This adds support for loops, as seen below. <p><img alt="image" src="https://img-blog.csdnimg.cn/img_convert/cc988f353118cd2381e59c941d3e6c15.png" /></p> <p>Multiple loops are permitted on an ...
  • 4.6 Loops

    2020-12-25 18:22:30
    <div><p>4.6 Loops に着手。 - [x] 自己レビュー項目の反映 - [x] string literal部を原文に戻す</p><p>该提问来源于开源项目:rust-lang-ja/the-rust-programming-language-ja</p></div>
  • While loops

    2021-04-09 08:41:36
    While loops while 循环 While loops are similar to for loops, but have less functionality(功能比较少). A while loop continues executing(继续执行) the while block as long as(只要) the condition ...
  • JavaScript Loops

    2019-07-27 18:41:03
    Different Kinds of Loops JavaScript supports different kinds of loops: for - loops through a block of code a number of times for/in - loops through the properties of an object while - loops thro...
  • Loops?

    2020-12-28 11:25:59
    <p>Are you planning on implementing loops? Like, running GET 20 times (without copy-pasting it 20 times in the script)? <p>Now that would be an awesome next step!</p><p>该提问来源于开源项目:f-...
  • Compile loops

    2020-12-31 13:20:38
    <div><ul>[x] compile loop in ...s not worth it in this case but the code is interesting enough and can be easily adapted to other loops. </p><p>该提问来源于开源项目:uwescience/myria</p></div>
  • Bounded loops

    2020-11-28 01:10:29
    <div><p>Adds a new search strategy called "bfs-bounded"... Should also help with BECToken and other samples that contain loops.</p><p>该提问来源于开源项目:ConsenSys/mythril</p></div>
  • For Loops

    2020-12-06 14:56:22
    <p>1.) For Loops in Blocks and Typescript not matching (stay consistent with either "i" or "index" variable name, use the same max value "4" or "5", and use "<&#...
  • fruit_loops-源码

    2021-03-16 13:03:01
    fruit_loops
  • control_loops-源码

    2021-03-12 22:01:53
    control_loops
  • Loops Flow

    2020-03-11 08:29:33
    for (let i = 0;... console.log(`in loops: ${i}`); } // 报错: sandbox.js:4 Uncaught ReferenceError: i is not defined // console.log(`out loops: ${i}`); for (var j = 0; j < 3; j++) ...
  • Loops, Switch

    2021-01-11 08:06:47
    <p>Support loops. <h1>Detail <ul><li>[x] Support <code>for</code> loops</li><li>[ ] Support <code>do ... while()</code> loops</li><li>[x] Support <code>while ... do()</code> loops</li><li>[ ] Support ...
  • Package dependency loops

    2021-01-11 08:33:58
    <div><p>With the pass system we now have in place we can trivially allow, or outlaw, package dependency loops. So which do we want to do? <p>Allowing loops makes ad-hoc development easier, but may ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 15,688
精华内容 6,275
关键字:

loops