网站流量提升方案,wordpress免费字体,网站 数据库 关系,怎么在百度上注册店铺小数第n位
题目描述
我们知道#xff0c;整数做除法时#xff0c;有时得到有限小数#xff0c;有时得到无限循环小数。如果我们把有限小数的末尾加上无限多个0#xff0c;那么有限小数和无限小数就都有了统一的形式。 本题的任务是:在上面的约定下#xff0c;求整数除法…小数第n位
题目描述
我们知道整数做除法时有时得到有限小数有时得到无限循环小数。如果我们把有限小数的末尾加上无限多个0那么有限小数和无限小数就都有了统一的形式。 本题的任务是:在上面的约定下求整数除法小数点后的第 n 位开始的 3 个数字。
输入描述
输入一行三个整数:a,b,n用空格分开。a是被除数b是除数n 是所求的小数后位置(0a, b,n 109) 比如a1,b8,则a/b1/80.125。如果n1输出为125n2输出为250n3输出为500
输出描述
输出一行 3 位数字表示a 除以 b小数后第 n 位开始的 3 位数字。
输入输出样例
示例
输入1 8 1输出125一般解法然并卵 1将a/b的结果保存为字符串 2把字符串中小数点的位置找出来然后向后移动n位 3对字符串切片把indexn : indexn3的子字符串截取出来 4如果子字符串的长度不够末尾用0补足
import os
import sys# 请在此输入您的代码
a, b, n map(int, input().split())
# 先进行除法运算得到小数形式字符串表示注意Python中整数除法会得到整数结果这里要转为浮点数除法
result str(a / b)
# print(result)# 找到小数点的位置
dot_index result.find(.)# 如果n大于总长度包含小数点则不符合要求
if dot_index n len(result) and n 1000000000:# 提取从第n位开始的3位数字sub_result result[dot_index n: dot_index n 3]# 如果不足3位数字在末尾补0if len(sub_result) 3:sub_result 0 * (3 - len(sub_result))
print(sub_result)输入输出结果略。
然后就是只通过了一个测试用例。心中挺纳闷的。 后来看了其他作者分享的内容才知道自己想法错了。 https://blog.csdn.net/red_red_red/article/details/89843256 https://blog.csdn.net/A_ACM/article/details/88304399 换个思路这道题的解法是这样 1获得a,b,n并转换为整数。 2目标值即为a ÷ b× 10n2 % 1000 a× 10n2 % b × 1000/ b 3然后用快速求幂 感谢胡歌爱亦菲 感谢qdu_zhaiH虽然还是一头雾水没看怎么看懂。
代码实现 Python 实现
# 快速幂函数用于计算a的b次方对mod取模的结果
def q_pow(a, b, mod):res 1while b:if b 1:res (res * a) % moda (a * a) % modb 1return res# 使用map函数将输入的字符串转换为整数并分别赋值给a1, b1, n
a1, b1, n map(int, input().split())# 计算取模的数值
mod b1 * 1000
# 调用快速幂函数计算结果
res q_pow(10, n 2, mod)
# 进行临时计算
tem (a1 % mod * res % mod) % mod
# 按照格式化要求输出结果确保输出三位宽度不足三位前面补0
print(f{tem // b1:03d})JAVA 实现
import java.util.Scanner;public class Main {// 快速幂函数用于计算a的b次方对mod取模的结果static long q_pow(long a, long b, long mod) {long res 1;while (b 0) {if ((b 1) 1) {res (res * a) % mod;}a (a * a) % mod;b 1;}return res;}public static void main(String[] args) {Scanner scanner new Scanner(System.in);// 获取用户输入的三个整数分别赋值给a1, b1, nlong a1 scanner.nextLong();long b1 scanner.nextLong();long n scanner.nextLong();// 计算取模的数值long mod b1 * 1000;// 调用快速幂函数计算结果long res q_pow(10, n 2, mod);// 进行临时计算long tem (a1 % mod * res % mod) % mod;// 按照格式化要求输出结果确保输出三位宽度不足三位前面补0System.out.printf(%03d\n, tem / b1);scanner.close();}
}C实现
#include iostream
using namespace std;// 快速幂函数用于计算a的b次方对mod取模的结果
long long q_pow(long long a, long long b, long long mod) {long long res 1;while (b 0) {if (b 1) {res (res * a) % mod;}a (a * a) % mod;b 1;// 右移操作相当于b / 2;}return res;
}int main() {long long a1, b1, n;// 获取用户输入的三个整数cin a1;cin b1;cin n;long long mod b1 * 1000;// 调用快速幂函数计算结果long long res q_pow(10, n 2, mod);long long tem (a1 % mod * res % mod) % mod;// 按照格式化要求输出结果确保输出三位宽度不足三位前面补0printf(%03lld\n, tem / b1);return 0;
}C 实现
#include stdio.h// 快速幂函数用于计算a的b次方对mod取模的结果
long long q_pow(long long a, long long b, long long mod) {long long res 1;while (b 0) {if (b 1) {res (res * a) % mod;}a (a * a) % mod;b 1;// 右移操作相当于b / 2;}return res;
}int main() {long long a1, b1, n;// 获取用户输入的三个整数scanf(%lld, a1);scanf(%lld, b1);scanf(%lld, n);long long mod b1 * 1000;// 调用快速幂函数计算结果long long res q_pow(10, n 2, mod);long long tem (a1 % mod * res % mod) % mod;// 按照格式化要求输出结果确保输出三位宽度不足三位前面补0printf(%03lld\n, tem / b1);return 0;
}