|
|
发表于 2023-1-26 15:31:39
|
显示全部楼层
应该是不需要高精度的
版本一(任意输入方式):- #include <iostream>
- #include <string>
- int main(){
- std::string a;
- uint64_t b;
- uint64_t result = 0;
- std::cin >> a >> b;
- for(const auto c: a){
- result = ((result << 3) + (result << 1) + (c - '0')) % b;
- }
- std::cout << result;
- return 0;
- }
复制代码
版本二(需要将输入重定向到文件):- #include <unistd.h>
- #include <sys/stat.h>
- int main(){
- enum{ BufferSize = 1024 };
- struct stat status;
- unsigned char buffer[BufferSize];
- ssize_t effective_length;
- if(fstat(STDIN_FILENO, &status) == -1){
- return 1;
- }
- off_t seek_offset = status.st_size > 11 ? -11 : -status.st_size;
- if(lseek(STDIN_FILENO, seek_offset, SEEK_END) == -1){
- return 1;
- }
- effective_length = read(STDIN_FILENO, buffer, BufferSize);
- unsigned long b = 0;
- ssize_t i = 0;
- while(buffer[i++] != ' ');
- while(i < effective_length && buffer[i] >= '0' && buffer[i] <= '9'){
- b = (b << 3) + (b << 1) + (buffer[i++] - '0');
- }
- lseek(STDIN_FILENO, 0, SEEK_SET);
- unsigned long result = 0;
- effective_length = i = 0;
- while(1){
- effective_length = read(STDIN_FILENO, buffer, BufferSize);
- for(i = 0; i < effective_length; i++){
- if(buffer[i] == ' ') goto end;
- result = ((result << 3) + (result << 1) + (buffer[i] - '0')) % b;
- }
- }
- end:
- i = 12;
- effective_length = 0;
- if(result == 0){
- effective_length = 1;
- buffer[i--] = '0';
- }
- while(result){
- effective_length++;
- buffer[i--] = (result % 10) + '0';
- result /= 10;
- }
- write(STDOUT_FILENO, buffer + i + 1, effective_length);
- return 0;
- }
复制代码 |
评分
-
查看全部评分
|