URAL - 1153 Supercomputer 大数开方

发布时间:2020-10-21 整理:编程之家
编程之家收集整理的这篇文章主要介绍了URAL - 1153 Supercomputer 大数开方编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随编程之家小编过来看看吧!

        题意:给定m,m = n * (n+1) / 2,计算n值。

      思路:n = SQRT(m*2)

     注意m很大,需要自己实现大数开方。我用的是自己写的大数模板:大数模板

AC代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int,int> 
typedef long long LL;
const int maxn = 1e4 + 5;

struct BigInteger {
	vector<int>s;  //12345--54321
	
	void DealZero() { //处理前导0 
		for(int i = s.size() - 1; i > 0; --i){
			if(s[i] == 0) s.pop_back();
			else break;
		}
	}
	
 	BigInteger operator = (long long num) { // 赋值运算符
 		s.clear();
 		vector<int>tmp;
       	do{
       		s.push_back(num % 10);
       		num /= 10;
		}while(num);
		return *this;
    }
    
	BigInteger operator = (const string& str) { // 赋值运算符
    	s.clear();
   		for(int i = str.size() - 1; i >= 0; --i) s.push_back(str[i] - '0');
   		this->DealZero();
   		return *this;
    }
    BigInteger operator = (const char *a) {
    	int n = strlen(a);
	}
    
    BigInteger operator + (const BigInteger& b) const {
    	BigInteger c;
    	c.s.clear();
    	int len1 = s.size(),len2 = b.s.size(); 
    	for(int i = 0,g = 0; g > 0 || i < len1 || i < len2; ++i) {
    		int x = g;
    		if(i < len1) x += s[i];
    		if(i < len2) x += b.s[i];
    		c.s.push_back(x % 10);
    		g = x / 10;
		}
    	return c;
   }
   
   //大数减小数 
   BigInteger operator - (const BigInteger& b) const {
    	BigInteger c;
    	c.s.clear();
    	int len1 = s.size(),g = 0; i < len1 || i < len2; ++i) {
    		int x = g;
    		if(i < len1) x += s[i];
    		g = 0;
    		if(i < len2) x -= b.s[i];	
    		if(x < 0) {
    			g = -1; //借位 
    			x += 10;
			} 
    		c.s.push_back(x);
		}
		c.DealZero();
    	return c;
   }
   
   BigInteger operator * (const BigInteger& b) const {
   		BigInteger c,tmp;
    	c.s.clear();
    	int len1 = s.size(),len2 = b.s.size();
   		for(int i = 0; i < len1; ++i) {
   			tmp.s.clear();tmp;
   			int num = i;
			while(num--) tmp.s.push_back(0);
			int g = 0;
			for(int j = 0; j < len2; ++j) {
				int x = s[i] * b.s[j] + g;
				tmp.s.push_back(x % 10);
				g = x / 10;
			}
			if(g > 0) tmp.s.push_back(g);
			c = c + tmp;	
		}
		c.DealZero();
		return c;
   }
   
   //单精度除法 
   BigInteger operator / (const int b) const { 
   		BigInteger c,tmp;
   		c.s.clear();
   		int len = s.size();
   		int div = 0;
   		for(int i = len - 1; i >= 0; --i) {
   			div = div * 10 + s[i];
   			while(div < b && i > 0) {
   				div = div * 10 + s[--i];
			}
			tmp.s.push_back(div / b);
			div %= b;
		}
		for(int i = tmp.s.size() - 1; i >= 0; --i) c.s.push_back(tmp.s[i]);
		c.DealZero();
		return c;
   } 
   
   bool operator < (const BigInteger& b) const {
   	   	int len1 = s.size(),len2 = b.s.size();
   	   	if(len1 != len2) return len1 < len2;
   	   	for(int i = len1 - 1; i >= 0; --i) {
   	   		if(s[i] != b.s[i]) return s[i] < b.s[i];	
		}
		return false; //相等 
   }
   
   bool operator <= (const BigInteger& b) const {
   		return !(b < *this);
   }
   string ToStr() {
   		string ans;
   		ans.clear();
   		for(int i = s.size()-1; i >= 0; --i) 
   			ans.push_back(s[i] + '0');
   		return ans;
   }
   
   //大数开方 
/**大数开方用法说明:
   字符串必须从第二个位置开始输入,且s[0] = '0' 
   scanf("%s",s+1); 
*/ 
   BigInteger SQRT(char *s) {
   		string p = "";
		s[0]='0';
    	if(strlen(s)%2 == 1)
        	work(p,2,s+1,0);
    	else
        	work(p,s,0);
   		BigInteger c;
   		c.s.clear();
   		c = p;
   		return c;
   }
   
   
//开方准备  
//------------------------------------
	int l;
	int work(string &p,int o,char *O,int I){
	    char c,*D=O ;
	    if(o>0)
	    {
	        for(l=0;D[l];D[l++]-=10)
	        {
	            D[l++]-=120;
	            D[l]-=110;
	            while(!work(p,O,l))
	                D[l]+=20;
	            p += (char)((D[l]+1032)/20);
	            
	        }
	    }
	    else
	    {
	        c=o+(D[I]+82)%10-(I>l/2)*(D[I-l+I]+72)/10-9;
	        D[I]+=I<0 ? 0 : !(o=work(p,c/10,I-1))*((c+999)%10-(D[I]+92)%10);
	    }
	    return o;
	}
//-----------------------------------------
};

ostream& operator << (ostream &out,const BigInteger& x) {
	for(int i = x.s.size() - 1; i >= 0; --i)
		out << x.s[i];
	return out;
}

istream& operator >> (istream &in,BigInteger& x) {
  string s;
  if(!(in >> s)) return in;
  x = s;
  return in;
}

int main() {
	BigInteger a,tmp;
	tmp = 2;
	string str;
	char s[maxn];
	while(cin >> str) {
		a = str;
		a = tmp * a;
		int cur = 1;
		for(int i = a.s.size()-1; i >= 0; --i) {
			s[cur++] = a.s[i] + '0';
		}
		cout << a.SQRT(s) << "\n";
	} 
	return 0;
}
如有不当之处欢迎指出!

总结

以上是编程之家为你收集整理的URAL - 1153 Supercomputer 大数开方全部内容,希望文章能够帮你解决URAL - 1153 Supercomputer 大数开方所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478
编程之家官方公众号

微信公众号搜索 “ 程序精选 ” ,选择关注!

微信公众号搜索 “ 程序精选 ”
精选程序员所需精品干货内容!