如何在指定的时间间隔后,根据速度找出当前页面的页数和行数?

huangapple go评论84阅读模式
英文:

how to find number of pages and number of lines on the current page respect to speed after a specified interval of time?

问题

一个学生创建了一个软件,该软件将从一本书中读取内容,并写入另一本书中。这两本书的尺寸可能不同。
软件首先从书中完全读取,然后处理格式以便写入另一本书中。
您的任务是在指定的时间间隔后,识别软件是在读取还是在写入。对于每个活动,需要捕获已发生的读取和写入活动,以页数和当前页上的行数表示。

**输入**
```plaintext
pn1 -> 第一本书中的页数
ln1 -> 第一本书每页的行数
pn2 -> 第二本书中的页数
ln2 -> 第二本书每页的行数
rs -> 读取速度,每秒行数
ws -> 写入速度,每秒行数
t -> 要处理结果的时间,以秒为单位

输出

打印当前活动(READ 或 WRITE)、页码和行号

示例

输入
100
10
500
6
8
4
145

输出
WRITE 13 2

代码:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int pn1 = sc.nextInt();
        int ln1 = sc.nextInt();
        int pn2 = sc.nextInt();
        int ln2 = sc.nextInt();
        int rs = sc.nextInt();
        int ws = sc.nextInt();
        int t = sc.nextInt();
        int i,j,s1,s2,s3=0;
        s1 = pn1 * ln1;
        s2 = s1/rs;
        if(s2<t)
        {
            System.out.print("WRITE ");
            for(i=1;i<=pn2;i++)
            {
                for(j=1;j<=ln2;j=j+ws)
                {
                    if(s2==t)
                    {
                        break;
                    }
                    s2++;
                }
                if(j<=ln2)
                {
                    System.out.print(i+ " " +j);
                    break;
                }
            }
        }
        else
        {
            System.out.print("READ ");
            for(i=1;i<=pn1;i++)
            {
                for(j=1;j<=ln1;j=j+rs)
                {
                    if(s3==t)
                    {
                        break;
                    }
                    s3++;
                }
                if(j<=ln1)
                {
                    System.out.print(i+ " " +j);
                    break;
                }
            }
        }
    }
}

我的输出:WRITE 11 1

有人可以帮忙找出我哪里出错了吗?因为在我的 if else 语句中,j 的值没有增加,它将始终打印 1,因为在 for 循环中它将被初始化为 1。
提前谢谢您的帮助。


<details>
<summary>英文:</summary>
A student has created a software which will read from one book and write into another book . Both books may have different dimensions. 
Software first reads from the book fully then process the format to write into another book. 
Yours task is to identify after a specified interval of time , if software is reading or writing. for each of these activities how much read and write activity has happened needs to be captured in terms of page and number of lines on the current page.
**Input** 

pn1 -> number of pages in first book
ln1 -> number of lines per page in first book
pn2 -> number of pages in second book
ln2 -> number of lines per pages in second book
rs -> reading speed in lines/seconds
ws -> writing speed in lines/seconds
t -> time in seconds at which the result is to be processed


**Output**

print current activity (READ or WRITE),page number and line number

**Example**

Input
100
10
500
6
8
4
145

Output
WRITE 13 2

**Code :**

import java.util.*;

public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int pn1 = sc.nextInt();
int ln1 = sc.nextInt();
int pn2 = sc.nextInt();
int ln2 = sc.nextInt();
int rs = sc.nextInt();
int ws = sc.nextInt();
int t = sc.nextInt();
int i,j,s1,s2,s3=0;
s1 = pn1 * ln1;
s2 = s1/rs;
if(s2<t)
{
System.out.print("WRITE ");
for(i=1;i<=pn2;i++)
{
for(j=1;j<=ln2;j=j+ws)
{
if(s2==t)
{
break;
}
s2++;
}
if(j<=ln2)
{
System.out.print(i+ " " +j);
break;
}
}
}
else
{
System.out.print("READ ");
for(i=1;i<=pn1;i++)
{
for(j=1;j<=ln1;j=j+rs)
{
if(s3==t)
{
break;
}
s3++;
}
if(j<=ln1)
{
System.out.print(i+ " " +j);
break;
}
}
}
}
}

**My output : WRITE 11 1**
Can anyone please help where I m going wrong , Because in my if else statement j value is not incrementing it will always print 1 as it will be initialized as 1 in for loop . 
Thank you in advanced . 
</details>
# 答案1
**得分**: 1
循环嵌套中的计算是错误的,因为它们没有考虑到页面在一秒钟内未完全处理的情况。
对于输入数据,写入过程如下:&lt;br/&gt;
第1秒:总行数 4:第1页,第4行
第2秒:总行数 8:第2页,第2行
第3秒:总行数12:第2页,第6行
等等。
因此,最好使用时间参数进行单个循环,并使用模运算修改页面/行信息:
```java
if(s2&lt;t) {
for (int tt = s2 + 1; tt &lt;= t; tt++) {
j += ws;
if (j &gt;= ln2) {
i++;
j = j % ln2;
}
}
if (j == 0) { // 如果在一秒钟内完全写入,纠正页面编号
i--;
j = ln2;
}
System.out.print(&quot;WRITE &quot; + i + &quot; &quot; + j);
} else { // 以类似方式修复读取
for (int tt = 0; tt &lt; t; tt++) {
j += rs;
if (j &gt;= ln1) {
i++;
j = j % ln1;
}
}
if (j == 0) { // 如果在一秒钟内完全读取,纠正页面编号
i--;
j = ln1;
}
System.out.print(&quot;READ &quot; + i + &quot; &quot; + j);
}

此任务也可以通过模运算和一个单独的函数来解决,以定义当前页面和行数:

// 重用在主函数中定义的所有变量
int totalReadLines = pn1 * ln1;
int timeToRead = (int) Math.ceil(totalReadLines / rs);
if (t &lt;= timeToRead) {
    printState(&quot;READ&quot;, totalReadLines, rs, t, ln1);
} else {
    t -= timeToRead;
    int totalWriteLines = pn2 * ln2;
    int timeToWrite = (int) Math.ceil(totalWriteLines / ws);
    if (t &lt;= timeToWrite) {
        printState(&quot;WRITE&quot;, totalWriteLines, ws, t, ln2);
    } else {
        System.out.println(&quot;DONE&quot;);
    }
}
private static void printState(String state, int totalLines, int speed, int time, int linesPerPage) {
    int linesProcessed = time * speed;
    int pagesProcessed = linesProcessed / linesPerPage;
        
    int remLines = linesProcessed % linesPerPage;
        
    int currentPage;
    int currentLine;
        
    if (remLines == 0) {
        currentPage = pagesProcessed;
        currentLine = linesPerPage;
    } else {
        currentPage = pagesProcessed + 1;
        currentLine = remLines;
    }
    System.out.printf(&quot;%s %d %d%n&quot;, state, currentPage, currentLine);
}
英文:

The calculations in the nested loops are incorrect because they do not take into account the case when the page is not processed completely within a second.

For the input data, the writing process is as follows:<br/>
sec 1: total lines 4: page 1, line 4
sec 2: total lines 8: page 2, line 2
sec 3: total lines 12: page 2, line 6
etc.

Thus, it's better to have a single loop by the time parameter and modify the page/line information using modulo operation:

if(s2&lt;t) {
    for (int tt = s2 + 1; tt &lt;= t; tt++) {
        j += ws;
        if (j &gt;= ln2) {
            i++;
            j = j % ln2;
        }
    }
    if (j == 0) { // correct page number if it&#39;s been completely written within a second
        i--;
        j = ln2;
    }
    System.out.print(&quot;WRITE &quot; + i + &quot; &quot; + j);
} else { // fix reading in similar way
    for (int tt = 0; tt &lt; t; tt++) {
        j += rs;
        if (j &gt;= ln1) {
            i++;
            j = j % ln1;
        }
    }
    if (j == 0) { // correct page number if it&#39;s been completely read within a second
        i--;
        j = ln1;
    }
    System.out.print(&quot;READ &quot; + i + &quot; &quot; + j);
}

This task could also be resolved without loops using modulo arithmetics and a separate function to define current page and line:

// reusing all the variables defined in main 
int totalReadLines = pn1 * ln1;
int timeToRead = (int) Math.ceil(totalReadLines / rs);
if (t &lt;= timeToRead) {
    printState(&quot;READ&quot;, totalReadLines, rs, t, ln1);
} else {
    t -= timeToRead;
    int totalWriteLines = pn2 * ln2;
    int timeToWrite = (int) Math.ceil(totalWriteLines / ws);
    if (t &lt;= timeToWrite) {
        printState(&quot;WRITE&quot;, totalWriteLines, ws, t, ln2);
    } else {
        System.out.println(&quot;DONE&quot;);
    }
}
private static void printState(String state, int totalLines, int speed, int time, int linesPerPage) {
    int linesProcessed = time * speed;
    int pagesProcessed = linesProcessed / linesPerPage;
        
    int remLines = linesProcessed % linesPerPage;
        
    int currentPage;
    int currentLine;
        
    if (remLines == 0) {
        currentPage = pagesProcessed;
        currentLine = linesPerPage;
    } else {
        currentPage = pagesProcessed + 1;
        currentLine = remLines;
    }
    System.out.printf(&quot;%s %d %d%n&quot;, state, currentPage, currentLine);
}

huangapple
  • 本文由 发表于 2020年10月4日 15:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64192333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定