首页 > 其他分享 >P1480 A/B Problem

P1480 A/B Problem

时间:2024-04-13 16:02:45浏览次数:25  
标签:node 10 P1480 int long len str Problem

P1480 A/B Problem

题目描述

输入两个整数 \(a,b\),输出它们的商。

输入格式

两行,第一行是被除数,第二行是除数。

输出格式

一行,商的整数部分。

样例

输入

10
2

输出

5

提示

\(0\le a\le 10^{5000}\),\(1\le b\le 10^9\)。


思路

通过题目数据范围可以发现是高精度除以单精度的题目。

假设被除数存储在整型数组 a 中,定义整型变量 x,x初始化为 0,每次执行“\(x=x \times 10+a \lbrack i \rbrack\)”并除以除数取整后,赋值到商的数组中,\(x\) 被余数更新。最后删去前导零。另外需要注意,除法运算是从高位到低位进行的,在字符串转为整型数组的过程中正序存储即可。处理前导零的时候也需要正向处理。


代码

#include <bits/stdc++.h>

using namespace std;

char str[50010];
long long a[50010], p, c[50010], len, r;

void read()
{
	scanf("%s%d", str, &p);
	len = strlen(str);
	for (int i = 0; i < len; i ++ )
		a[i + 1] = str[i] - '0';
}

void div()
{
	long long x = 0, fl = 0;
	for (int i = 1; i <= len; i ++ )
	{
		x = x * 10 + a[i];
		if (x / p)
			fl = 1;
		if (!fl)
			continue;
		c[++ r] = x / p;
		x %= p;
	}
}

void print()
{
	if (r == 0)
	{
		printf("0");
		return;
	}
	for (int i = 1; i <= r; i ++ )
		printf("%d", c[i]);
}

int main()
{
	read();
	div();
	print();
	return 0;
}

代码二:重载运算符

需要注意的是:由于 \(b\) 的最大值为 \(10^9\),因此在模拟竖式除法执行“\(x=x \times 10+a \lbrack i \rbrack\)”的过程中,\(x\) 的最大值为“\((10^9-1) \times 10+9\)”,超出整型 \(\operatorname{int}\) 范围,所以 \(x\) 定义为 \(\operatorname{long long}\) 类型。

#include <bits/stdc++.h>

using namespace std;

char str[5010];

struct node
{
	int len, s[5010];
	node()
	{
		len = 0;
		memset(s, 0, sizeof(s));
	}
};

node operator / (const node &a, int b)
{
	node c;
	c.len = a.len;
	long long x = 0; // 注意 x 的范围
	for (int i = a.len; i >= 1; i -- )
	{
		x = x * 10 + a.s[i];
		c.s[i] = x / b;
		x %= b;
	}
	while (!c.s[c.len])
		c.len --;
	return c;
}

node read()
{
	node c;
	scanf("%s", str);
	if (str[0] == '0')
	{
		printf("0");
		exit(0);
	}
	int len = strlen(str);
	for (int i = 1; i <= len; i ++ )
		c.s[i] = str[len - i] - '0';
	c.len = len;
	return c;
}

void print(const node &a)
{
	for (int i = a.len; i >= 1; i -- )
		printf("%d", a.s[i]);
}

int main()
{
	node a, c;
	a = read();
	int p;
	scanf("%d", &p);
	c = a / p;
	print(c);
	return 0;
}

标签:node,10,P1480,int,long,len,str,Problem
From: https://www.cnblogs.com/IronMan-PZX/p/18132975

相关文章

  • P1303 A*B Problem
    P1303A*BProblem题目给出两个非负整数,求它们的乘积。输入输入共两行,每行一个非负整数。输出输出一个非负整数表示乘积。样例输入12输出2提示每个非负整数不超过\(10^{2000}\)。思路根据题意,乘数的数据最大范围是\(10^{2000}\),需要使用高精度乘高精度的算......
  • CF1618G Trader Problem 题解
    CF1618GTraderProblem题解题目链接:CF|洛谷提供一个在线做法。分析1我们不妨把\(a\)和\(b\)合并为一个序列,称合并后的序列为\(c\),并将其不降序排序。把玩样例后不难发现:对于一个物品序列\(c_1,c_2,\cdots,c_l\),满足\(\foralli<l,c_{i+1}-c_i\lek\)(即任意......
  • 52 Things: Number 11: What are the DLP, CDH and DDH problems?
    52Things:Number11:WhataretheDLP,CDHandDDHproblems?52件事:数字11:DLP、CDH和DDH问题是什么? Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography':asetofquestion......
  • 52 Things: Number 10: What is the difference between the RSA and strong-RSA prob
    52Things:Number10:WhatisthedifferencebetweentheRSAandstrong-RSAproblem?52件事:数字10:RSA和强RSA问题有什么区别?Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography......
  • CF1748E Yet Another Array Counting Problem の Solution
    Link有些人还是啥都不会。看到题目应该能想到这是笛卡尔树的性质,因为每一对\((l,r)\)都满足最左端最大值位置相同,所以说明在笛卡尔树上,每一对点的lca相同,说明\(a\)和\(b\)序列的笛卡尔树相同。我们以下标为键,\(a_i\)为值建立大根笛卡尔树,现在题目就转换成在这个树上填......
  • P1865 A % B Problem
    原题链接题解1.筛一筛就行code#include<bits/stdc++.h>usingnamespacestd;intheshu[1000006]={0},f[1000007]={0};intmain(){intn,m;cin>>n>>m;for(longlongi=2;i<=m;i++){if(!heshu[i])for(longlongk=i*i;k<=m;......
  • Randomness Is All You Need: Semantic Traversal of Problem-Solution Spaces with L
    本文是LLM系列文章,针对《RandomnessIsAllYouNeed:SemanticTraversalofProblem-SolutionSpaceswithLargeLanguageModels》的翻译。随机性就是你所需要的:具有大型语言模型的问题解决空间的语义遍历摘要1引言2相关工作3模型4算法5评估6实现7结论摘......
  • CF30D King's Problem? 题解
    CF30D题意有\(n+1\)个点,其中的\(n\)个点在数轴上。求以点\(k\)为起点走过所有点的最短距离,允许重复。思路有两种情况:\(k\)在数轴上(如图1)。\(k\)在第\(n+1\)个点上(如图2)。图1:图2:像第一种情况:一定存在数轴上某点\(k\),使得人先走遍\(1\simk\),回来,再走遍......
  • CF1934B Yet Another Coin Problem 题解
    CF1934BYetAnotherCoinProblem题解题意目前有\(5\)种硬币,面值分别为\(1,3,6,10,15\)。给你一个数字\(n\),求出可以凑出\(n\)的最少的硬币的数量。思路这道题,大多数的人大概会想到动态规划的方法。但是,我们应该有敢于创新的精神。于是我就想到了一个简单的数学方法......
  • 【2021.6.26 NOI模拟】Problem B. 简单题 another solution
    ProblemDescriptionInput从文件b.in中读入数据。一个正整数n。Output输出到文件b.out中。一个整数表示答案。SampleDataInput#1Copy5Output#1Copy31Input#2Copy50Output#2Copy2885DataConstraint首先,我们从小到大枚举\(n\),假设当前枚举......