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

go评论66阅读模式

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 -&gt; 第一本书中的页数
ln1 -&gt; 第一本书每页的行数
pn2 -&gt; 第二本书中的页数
ln2 -&gt; 第二本书每页的行数
rs -&gt; 读取速度，每秒行数
ws -&gt; 写入速度，每秒行数
t -&gt; 要处理结果的时间，以秒为单位
``````

``````打印当前活动（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
{
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;
}
}
}
}
}
``````

``````
<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
{
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

```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) {
} else {
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) {
} else {
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);
}
``````

• 本文由 发表于 2020年10月4日 15:57:42
• 转载请务必保留本文链接：https://go.coder-hub.com/64192333.html
• for-loop
• if-statement
• java
• while-loop

go 64

go 61

go 140

go 65