If We Were a Child Again - UVa 10494 高精度

本文介绍了一个涉及大数除法和取模操作的问题,并提供了一段使用Java BigInteger类实现的高效解决方案。

Problem C
If We Were a Child Again

Input: standard input
Output: standard output

Time Limit: 7 seconds

 

“Oooooooooooooooh!

If I could do the easy mathematics like my school days!!

I can guarantee, that I’d not make any mistake this time!!”

Says a smart university student!!

But his teacher even smarter – “Ok! I’d assign you such projects in your software lab. Don’t be so sad.”

“Really!!” - the students feels happy. And he feels so happy that he cannot see the smile in his teacher’s face.

 

 

The Problem

 

The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.

 

But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.

 

Input

Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).

 

 
Output

A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.

 

 
 
Sample Input

110 / 100

99 % 10

2147483647 / 2147483647

2147483646 % 2147483647

 

 

 

 

 
 
Sample Output

1

9

1

2147483646


题意:计算大除法和取模。

思路:JAVA高精度。

JAVA AC代码如下:

import java.math.BigInteger;
import java.util.Scanner;

public class Main
{ public static void main(String [] args)
  { Scanner scan=new Scanner(System.in);
    String s;
    BigInteger a,b,c;
    while(scan.hasNextBigInteger())
    { a=scan.nextBigInteger();
      s=scan.next();
      b=scan.nextBigInteger();
      if(s.charAt(0)=='/')
    	  c=a.divide(b);
      else
    	  c=a.mod(b);
      System.out.println(c);
    }
  }
}




“fewer bytes are actually available right now”这种情况出现的原因及对应的解决方案如下: #### 接近文件末尾 - **原因**:当接近文件末尾时,剩余可读取的数据量少于请求读取的字节数,所以`read()`函数返回的字节数会少于请求的数量。这类似于在文件操作中,随着读取操作的进行,文件指针不断向后移动,当接近文件末尾时,自然没有足够的数据可供读取了,就像用word文档敲完内容后,再往后读就没有更多内容了 [^1]。 - **解决方案**:可以通过检查`read()`函数的返回值,当返回值小于请求读取的字节数时,说明可能接近文件末尾,之后可以停止读取操作。以下是示例代码: ```c #include <fcntl.h> #include <unistd.h> #include <stdio.h> #define BUF_SIZE 1024 int main() { char buffer[BUF_SIZE]; ssize_t bytesRead; int fd = open("example.txt", O_RDONLY); if (fd == -1) { perror("Error opening file"); return 1; } while ((bytesRead = read(fd, buffer, BUF_SIZE - 1)) > 0) { buffer[bytesRead] = '\0'; // 添加字符串结束符 printf("%s", buffer); if (bytesRead < BUF_SIZE - 1) { break; // 可能接近文件末尾,停止读取 } } if (bytesRead == -1) { perror("Error reading file"); return 1; } close(fd); return 0; } ``` #### 从管道或终端读取 - **原因**:管道是一种半双工的通信方式,它有固定的缓冲区大小。当管道中的数据量少于请求读取的字节数时,`read()`函数会返回实际可用的字节数。对于终端来说,用户输入的数据是实时的,可能在请求读取时,用户输入的数据较少,导致没有足够的数据可供读取 [^2]。 - **解决方案**:可以采用循环读取的方式,不断尝试从管道或终端读取数据,直到满足需求或者遇到错误。示例代码如下: ```python import os pipe_r, pipe_w = os.pipe() os.write(pipe_w, b"Some data in the pipe") buffer_size = 1024 total_read = 0 data = b"" while total_read < buffer_size: chunk = os.read(pipe_r, buffer_size - total_read) if not chunk: break data += chunk total_read += len(chunk) print(data) ``` #### `read()`被信号中断 - **原因**:在执行`read()`函数时,如果进程接收到一个信号,`read()`函数可能会被中断,此时它会返回已经读取的字节数,这个数量可能少于请求读取的字节数 [^2]。 - **解决方案**:可以在捕获到信号后,重新调用`read()`函数继续读取数据。在C语言中可以结合信号处理函数来实现,示例代码如下: ```c #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <signal.h> #include <errno.h> #define BUF_SIZE 1024 volatile sig_atomic_t interrupted = 0; void sig_handler(int signo) { interrupted = 1; } int main() { int fd = open("example.txt", O_RDONLY); if (fd == -1) { perror("Error opening file"); return 1; } signal(SIGINT, sig_handler); char buffer[BUF_SIZE]; ssize_t bytesRead = 0; ssize_t totalRead = 0; while (totalRead < BUF_SIZE) { if (interrupted) { interrupted = 0; continue; // 重新尝试读取 } bytesRead = read(fd, buffer + totalRead, BUF_SIZE - totalRead); if (bytesRead == -1) { if (errno == EINTR) { continue; // 被信号中断,重新读取 } perror("Error reading file"); close(fd); return 1; } else if (bytesRead == 0) { break; // 到达文件末尾 } totalRead += bytesRead; } buffer[totalRead] = '\0'; printf("%s", buffer); close(fd); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值