//高精度加法
//只能是两个正数相加
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
string x,y;
string add(string s1,string s2)
{
string ans;
int l1 = s1.size(),l2 = s2.size();
if( l1 < l2 )
{
for(int i = 1;i <= l2 - l1; i++)
s1 = "0" + s1;
}
else
{
for(int i = 1;i <= l1 - l2; i++)
s2 = "0" + s2;
}
int l = s1.size();
int cf = 0,tmp;
for(int i = l - 1;i >= 0; i--)
{
tmp = s1[i]-'0' + s2[i]-'0' +cf;
cf = tmp/10;
tmp %= 10;
ans = char(tmp + '0') + ans;
}
if( cf != 0 )
ans = char(cf + '0') + ans;
return ans;
}
int main()
{
cin >> x >> y;
cout << add(x,y);
return 0;
}
/*
翻转字符串 reverse(x.begin(),x.end());
*/
//高精度减法
//只能是两个正数相减,而且要大减小
string sub(string s1,string s2)//高精度减法
{
string ans;
int tmp = s1.length()-s2.length();
int cf = 0;
// 处理进退位
for(int i = s2.length()-1;i >= 0;i--)
{
if( s1[ tmp+i ] < s2[i]+cf )
{
ans = char(s1[ tmp+i ]-s2[i]-cf+'0'+10)+ans;
cf = 1;
}
else
{
ans = char(s1[tmp+i]-s2[i]-cf+'0')+ans;
cf = 0;
}
}
for(int i = tmp-1;i >= 0;i--)
{
if( s1[i]-cf >= '0' )
{
ans = char(s1[i]-cf)+ans;
cf = 0;
}
else
{
ans = char(s1[i]-cf+10)+ans;
cf = 1;
}
}
ans.erase(0,ans.find_first_not_of('0'));
//去除结果中多余的前导0
return ans;
}
//高精度乘法
//只能是两个正数相乘
string mul(string s1,string s2)
{
string ans;
int len1=s1.length();
int len2=s2.length();
if( s1 == "0" || s2 == "0" )
return "0";
string tempstr;
for(int i = len2-1;i >= 0;i--)
{
tempstr = "";
int temp = s2[i]-'0';
int t = 0;
int cf = 0;
if( temp != 0 )
{
for(int j = 1;j <= len2-1-i;j++)
tempstr += "0";
for(int j = len1-1;j >= 0;j--)
{
t = (temp*(s1[j]-'0')+cf)%10;
cf = (temp*(s1[j]-'0')+cf)/10;
tempstr = char(t+'0')+tempstr;
}
if( cf != 0 )
tempstr = char(cf+'0')+tempstr;
}
ans = add(ans,tempstr);
}
ans.erase(0,ans.find_first_not_of('0'));
return ans;
}
#include<iostream>
#include<cstring>
using namespace std;
char a1[50001],b1[50001];
int a[50001],b[50001],i,x,len,j,c[50001];
int main ()
{
cin >>a1 >>b1;//读入两个数
a[0]=strlen(a1);b[0]=strlen(b1);//计算长度
for (i=1;i<=a[0];++i)a[i]=a1[a[0]-i]-'0';//将字符串转换成数字
for (i=1;i<=b[0];++i)b[i]=b1[b[0]-i]-'0';
for (i=1;i<=a[0];++i)for (j=1;j<=b[0];++j)c[i+j-1]+=a[i]*b[j];//按乘法
len=a[0]+b[0]; //原理进行高精乘
for (i=1;i<len;++i)if (c[i]>9){c[i+1]+=c[i]/10;c[i]%=10;}//进位
while (c[len]==0&&len>1)len--;//判断位数
for (i=len;i>=1;--i)cout <<c[i];//输出
return 0;
}
//高精度除法
//两个正数相除,商为quotient,余数为residue
//需要高精度减法和乘法
//compare比较函数:相等返回0,大于返回1,小于返回-1
int compare(string str1,string str2)
{
if( str1.length() > str2.length() )
return 1;
else if( str1.length() < str2.length() )
return -1;
else
return str1.compare(str2);
}
void div(string str1,string str2,string "ient,string &residue)
{
quotient = residue = "";//清空
if( str2 == "0" )//判断除数是否为0
{
quotient = residue = "ERROR";
return;
}
if( str1 == "0" )//判断被除数是否为0
{
quotient = residue = "0";
return;
}
int res = compare(str1,str2);
if( res < 0 )
{
quotient = "0";
residue = str1;
return;
}
else if( res == 0 )
{
quotient = "1";
residue = "0";
return;
}
else
{
int len1 = str1.length();
int len2 = str2.length();
string tempstr;
tempstr.append(str1,0,len2-1);
for(int i = len2-1;i < len1;i++)
{
tempstr = tempstr+str1[i];
tempstr.erase(0,tempstr.find_first_not_of('0'));
if(tempstr.empty())
tempstr = "0";
for(char ch = '9';ch >= '0';ch--)
{
string str,tmp;//试商
str = str+ch;
tmp = mul(str2,str);
if( compare(tmp,tempstr) <= 0 )
{//试商成功
quotient = quotient+ch;
tempstr = sub(tempstr,tmp);
break;
}
}
}
residue = tempstr;
}
quotient.erase(0,quotient.find_first_not_of('0'));
if( quotient.empty() )
quotient="0";
}
感谢奆佬User_Unauthorized赞助的封装模板
高精度模板
#include<bits/stdc++.h>
using namespace std;
class DividedByZeroException {};
class Int {
private:
vector<char> digits;
bool sign;
void trim();
public:
Int(int);
Int(string&) ;
Int();
Int(const Int&);
Int operator=(const Int& op2);
Int abs() const;
Int pow(int a);
friend Int operator+=(Int&, const Int&);
friend Int operator-=(Int&, const Int&);
friend Int operator*=(Int&, const Int&);
friend Int operator/=(Int&, const Int&) throw(DividedByZeroException);
friend Int operator%=(Int&, const Int&) throw(DividedByZeroException);
friend Int operator+(const Int&, const Int&);
friend Int operator-(const Int&, const Int&);
friend Int operator*(const Int&, const Int&);
friend Int operator/(const Int&, const Int&) throw(DividedByZeroException);
friend Int operator%(const Int&, const Int&) throw(DividedByZeroException);
friend Int operator-(const Int&);
friend Int operator++(Int&);
friend Int operator++(Int&, int);
friend Int operator--(Int&);
friend Int operator--(Int&, int);
friend bool operator>(const Int&, const Int&);
friend bool operator<(const Int&, const Int&);
friend bool operator==(const Int&, const Int&);
friend bool operator!=(const Int&, const Int&);
friend bool operator>=(const Int&, const Int&);
friend bool operator<=(const Int&, const Int&);
friend ostream& operator<<(ostream&, const Int&);
friend istream& operator>>(istream&, Int&);
public:
static const Int ZERO;
static const Int ONE;
static const Int TEN;
};
const Int Int::ZERO = Int(0);
const Int Int::ONE = Int(1);
const Int Int::TEN = Int(10);
Int::Int() {
sign = true;
}
Int::Int(int val) {
if (val >= 0) {
sign = true;
}
else {
sign = false;
val *= (-1);
}
do {
digits.push_back((char)(val % 10));
val /= 10;
} while (val != 0);
}
Int::Int(string& def) {
sign = true;
for (string::reverse_iterator iter = def.rbegin() ; iter < def.rend(); iter++) {
char ch = (*iter);
if (iter == def.rend() - 1) {
if (ch == '+') {
break;
}
if (ch == '-') {
sign = false;
break;
}
}
digits.push_back((char)((*iter) - '0'));
}
trim();
}
void Int::trim() {
vector<char>::reverse_iterator iter = digits.rbegin();
while (!digits.empty() && (*iter) == 0) {
digits.pop_back();
iter = digits.rbegin();
}
if (digits.size() == 0) {
sign = true;
digits.push_back(0);
}
}
Int::Int(const Int& op2) {
sign = op2.sign;
digits = op2.digits;
}
Int Int::operator=(const Int& op2) {
digits = op2.digits;
sign = op2.sign;
return (*this);
}
Int Int::abs() const {
if (sign) {
return *this;
}
else {
return -(*this);
}
}
Int Int::pow(int a) {
Int res(1);
for (int i = 0; i < a; i++) {
res *= (*this);
}
return res;
}
Int operator+=(Int& op1, const Int& op2) {
if (op1.sign == op2.sign) {
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
char to_add = 0;
while (iter1 != op1.digits.end() && iter2 != op2.digits.end()) {
(*iter1) = (*iter1) + (*iter2) + to_add;
to_add = ((*iter1) > 9);
(*iter1) = (*iter1) % 10;
iter1++;
iter2++;
}
while (iter1 != op1.digits.end()) {
(*iter1) = (*iter1) + to_add;
to_add = ((*iter1) > 9);
(*iter1) %= 10;
iter1++;
}
while (iter2 != op2.digits.end()) {
char val = (*iter2) + to_add;
to_add = (val > 9) ;
val %= 10;
op1.digits.push_back(val);
iter2++;
}
if (to_add != 0) {
op1.digits.push_back(to_add);
}
return op1;
}
else {
if (op1.sign) {
return op1 -= (-op2);
}
else {
return op1 = op2 - (-op1);
}
}
}
Int operator-=(Int& op1, const Int& op2) {
if (op1.sign == op2.sign) {
if (op1.sign) {
if (op1 < op2) {
return op1 = -(op2 - op1);
}
}
else {
if (-op1 > -op2) {
return op1 = -((-op1) - (-op2));
}
else {
return op1 = (-op2) - (-op1);
}
}
vector<char>::iterator iter1;
vector<char>::const_iterator iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
char to_substract = 0;
while (iter1 != op1.digits.end() && iter2 != op2.digits.end()) {
(*iter1) = (*iter1) - (*iter2) - to_substract;
to_substract = 0;
if ((*iter1) < 0) {
to_substract = 1;
(*iter1) += 10;
}
iter1++;
iter2++;
}
while (iter1 != op1.digits.end()) {
(*iter1) = (*iter1) - to_substract;
to_substract = 0;
if ((*iter1) < 0) {
to_substract = 1;
(*iter1) += 10;
}
else {
break;
}
iter1++;
}
op1.trim();
return op1;
}
else {
if (op1 > Int::ZERO) {
return op1 += (-op2);
}
else {
return op1 = -(op2 + (-op1));
}
}
}
Int operator*=(Int& op1, const Int& op2) {
Int result(0);
if (op1 == Int::ZERO || op2 == Int::ZERO) {
result = Int::ZERO;
}
else {
vector<char>::const_iterator iter2 = op2.digits.begin();
while (iter2 != op2.digits.end()) {
if (*iter2 != 0) {
deque<char> temp(op1.digits.begin(), op1.digits.end());
char to_add = 0;
deque<char>::iterator iter1 = temp.begin();
while (iter1 != temp.end()) {
(*iter1) *= (*iter2);
(*iter1) += to_add;
to_add = (*iter1) / 10;
(*iter1) %= 10;
iter1++;
}
if (to_add != 0) {
temp.push_back(to_add);
}
int num_of_zeros = iter2 - op2.digits.begin();
while (num_of_zeros--) {
temp.push_front(0);
}
Int temp2;
temp2.digits.insert(temp2.digits.end(), temp.begin(), temp.end());
temp2.trim();
result = result + temp2;
}
iter2++;
}
result.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
}
op1 = result;
return op1;
}
Int operator/=(Int& op1, const Int& op2) throw(DividedByZeroException) {
if (op2 == Int::ZERO) {
throw DividedByZeroException();
}
Int t1 = op1.abs(), t2 = op2.abs();
if (t1 < t2) {
op1 = Int::ZERO;
return op1;
}
deque<char> temp;
vector<char>::reverse_iterator iter = t1.digits.rbegin();
Int temp2(0);
while (iter != t1.digits.rend()) {
temp2 = temp2 * Int::TEN + Int((int)(*iter));
char s = 0;
while (temp2 >= t2) {
temp2 = temp2 - t2;
s = s + 1;
}
temp.push_front(s);
iter++;
}
op1.digits.clear();
op1.digits.insert(op1.digits.end(), temp.begin(), temp.end());
op1.trim();
op1.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
return op1;
}
Int operator%=(Int& op1, const Int& op2) throw(DividedByZeroException) {
return op1 -= ((op1 / op2) * op2);
}
Int operator+(const Int& op1, const Int& op2) {
Int temp(op1);
temp += op2;
return temp;
}
Int operator-(const Int& op1, const Int& op2) {
Int temp(op1);
temp -= op2;
return temp;
}
Int operator*(const Int& op1, const Int& op2) {
Int temp(op1);
temp *= op2;
return temp;
}
Int operator/(const Int& op1, const Int& op2) throw(DividedByZeroException) {
Int temp(op1);
temp /= op2;
return temp;
}
Int operator%(const Int& op1, const Int& op2) throw(DividedByZeroException) {
Int temp(op1);
temp %= op2;
return temp;
}
Int operator-(const Int& op) {
Int temp = Int(op);
temp.sign = !temp.sign;
return temp;
}
Int operator++(Int& op) {
op += Int::ONE;
return op;
}
Int operator++(Int& op, int x) {
Int temp(op);
++op;
return temp;
}
Int operator--(Int& op) {
op -= Int::ONE;
return op;
}
Int operator--(Int& op, int x) {
Int temp(op);
--op;
return temp;
}
bool operator<(const Int& op1, const Int& op2) {
if (op1.sign != op2.sign) {
return !op1.sign;
}
else {
if (op1.digits.size() != op2.digits.size())
return (op1.sign && op1.digits.size() < op2.digits.size())
|| (!op1.sign && op1.digits.size() > op2.digits.size());
vector<char>::const_reverse_iterator iter1, iter2;
iter1 = op1.digits.rbegin();
iter2 = op2.digits.rbegin();
while (iter1 != op1.digits.rend()) {
if (op1.sign && *iter1 < *iter2) {
return true;
}
if (op1.sign && *iter1 > *iter2) {
return false;
}
if (!op1.sign && *iter1 > *iter2) {
return true;
}
if (!op1.sign && *iter1 < *iter2) {
return false;
}
iter1++;
iter2++;
}
return false;
}
}
bool operator==(const Int& op1, const Int& op2) {
if (op1.sign != op2.sign || op1.digits.size() != op2.digits.size()) {
return false;
}
vector<char>::const_iterator iter1, iter2;
iter1 = op1.digits.begin();
iter2 = op2.digits.begin();
while (iter1 != op1.digits.end()) {
if (*iter1 != *iter2) {
return false;
}
iter1++;
iter2++;
}
return true;
}
bool operator!=(const Int& op1, const Int& op2) {
return !(op1 == op2);
}
bool operator>=(const Int& op1, const Int& op2) {
return (op1 > op2) || (op1 == op2);
}
bool operator<=(const Int& op1, const Int& op2) {
return (op1 < op2) || (op1 == op2);
}
bool operator>(const Int& op1, const Int& op2) {
return !(op1 <= op2);
}
ostream& operator<<(ostream& stream, const Int& val) {
if (!val.sign) {
stream << "-";
}
for (vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++) {
stream << (char)((*iter) + '0');
}
return stream;
}
istream& operator>>(istream& stream, Int& val) {
string str;
stream >> str;
val = Int(str);
return stream;
}
Int a,b;
int main()
{
cin >> a >> b;
cout << a+b << endl << a-b<<endl<<a*b<<endl<<a/b<<endl<<a%b;
}
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
目录一.加解密算法数字签名对称加密DES(DataEncryptionStandard)3DES(TripleDES)AES(AdvancedEncryptionStandard)RSA加密法DSA(DigitalSignatureAlgorithm)ECC(EllipticCurvesCryptography)非对称加密签名与加密过程非对称加密的应用对称加密与非对称加密的结合二.数字证书图解一.加解密算法加密简单而言就是通过一种算法将明文信息转换成密文信息,信息的的接收方能够通过密钥对密文信息进行解密获得明文信息的过程。根据加解密的密钥是否相同,算法可以分为对称加密、非对称加密、对称加密和非
我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h
📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP
我正在尝试使用正则表达式验证美元金额:^[0-9]+\.[0-9]{2}$这工作正常,但每当用户提交表单并且美元金额以0(零)结尾时,ruby(或rails?)将0砍掉。所以500.00变成500.0,因此正则表达式验证失败。有没有办法让ruby/rails保持用户输入的格式,而不管尾随零? 最佳答案 我假设您的美元金额是小数类型。因此,用户在字段中输入的任何值在保存到数据库之前都会从字符串转换为适当的类型。验证适用于已转换为数字类型的值,因此在您的情况下,正则表达式并不是真正合适的验证过滤器。不过,您有几种可能性可以解决这个问
一般来说,我是Middleman和ruby的新手。我已经安装了Ruby我已经安装了Middleman和gem以使其运行。我需要使用slim而不是默认的模板系统。所以我安装了Slimgem。Slim的网站只说我需要'slim'才能让它工作。中间人网站说我只需要在config.rb文件中添加模板引擎,但是没有给出例子...对于没有ruby背景的人来说,这没有帮助。我在git上找了几个config.rb,它们都有:require'slim'和#Setslim-langoutputstyleSlim::Engine.set_default_options:pretty=>true#Se
1.问题描述使用Python的turtle(海龟绘图)模块提供的函数绘制直线。2.问题分析一幅复杂的图形通常都可以由点、直线、三角形、矩形、平行四边形、圆、椭圆和圆弧等基本图形组成。其中的三角形、矩形、平行四边形又可以由直线组成,而直线又是由两个点确定的。我们使用Python的turtle模块所提供的函数来绘制直线。在使用之前我们先介绍一下turtle模块的相关知识点。turtle模块提供面向对象和面向过程两种形式的海龟绘图基本组件。面向对象的接口类如下:1)TurtleScreen类:定义图形窗口作为绘图海龟的运动场。它的构造器需要一个tkinter.Canvas或ScrolledCanva
Ruby到底是怎么做到的?Jörg或其他人是否知道幕后发生的事情?不幸的是,我不太了解C,所以bignum.c对我帮助不大。我只是有点好奇有人可以解释(用简单的英语)它使用的任何神奇算法背后的理论。irb(main):001:0>999**99936806348825922326789470084006052186583833823203735320465595962143702560930047223153010387361450517521869134525758989639113039318944796977164583238219236607653663113200177617
我有一个偏爱:如何将像o.office这样的值插入到属性中?value="#{o.office}"无效。 最佳答案 'type='text'/>或者你可以使用表单助手 关于ruby-on-rails-如何将变量值插入ERB模板中的HTML标签?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6172174/