啥也說,直接給代碼
//////////////////////////////////////////////////////////////// //作者:塗遠東 //時間:2010 01 23 //高精度的乘法,乘數的位數沒有限制,小數點後面的位數也沒有限制 //輸入參數:乘數 str1,str2 //輸出參數:str = str1*str2 //返回值類型:string& //////////////////////////////////////////////////////////////// string& large_mult(string str1,string str2,string& str/*輸出參數*/) { str = "0"; int pos1 = str1.find('.'); int pos2 = str2.find('.'); //小數點在字符串的位置 int offset = str1.length() + str2.length() - ((pos1 >0)? (pos1+1):0 ) - ((pos2 >0) ? (pos2+1):0 ); //去除字符串str1和str2中的小數點 if(pos1 != -1) str1.erase(pos1,1); if(pos2 != -1) str2.erase(pos2,1); unsigned char single = 0; //個位數 unsigned char tens = 0; //十位數 unsigned char ret = 0; //乘積 //計算規則,假設str1和str2字符串長度分別爲len1和len2 //乘積str的字符串長度len應該不大於len1+len2 //乘數str1[len1-i]和str2[len2-j]相乘只會乘積中str中str[len-i-j]和str[len-i-j-1]的值 int i = 0; for(string::iterator iter = str1.end()-1; iter >= str1.begin(); iter--,i++) { int index = 0; int offset = 0; int j=0; for(string::iterator iter2=str2.end()-1; iter2 >= str2.begin(); iter2--,j++) { offset = i+j +1; //字符‘0’的ASCII編碼值爲48 ret = (*iter2 -'0') * (*iter -'0'); //乘積 single = ret % 10; //乘積的個位數 tens = ret / 10; //乘積的十位數 //位數不夠左邊補0 index = str.length() - offset; while(index < 1) { str.insert(str.begin(), '0'); index = str.length() - offset; } assert(str.length() > offset); index = str.length() - offset; add(str,index,single); //個位數相加 add(str,index-1,tens); //十位數相加 } } if(str[0] == '0') { str.erase(str.begin()); } if(offset < str.length()) { string::iterator itr = str.end()-offset; str.insert(itr,'.'); } return str; } //////////////////////////////////////////////////////////////// //在大數str的index位處加於num 即 str+= num //////////////////////////////////////////////////////////////// inline bool add(string& str, int index, int num) { assert(index >=0); int p = str[index] + num - '0'; str[index] = p%10 + '0'; if(p >9) add(str,index-1,1); return true; }
測試代碼
#include "stdafx.h" #include <iostream> #include <string> #include <cassert> using namespace std; string& large_mult(string str1,string str2,string& str/*輸出參數*/); bool add(string& str, int index, int num); int main(int argc, char* argv[]) { printf("Please input two large number!/n"); string str1; string str2; std::cout<<"Please input the first large number:/nstr1="; std::cin>> str1; std::cout<<"Please input the second large number::/nstr2="; std::cin>>str2; string str; //str = str1 * str2 large_mult(str1,str2,str); std::cout<<"str=str1*str2/nstr="; std::cout<<str<<std::endl; return 0; }
一、 Bloom-Filter算法簡介。 Bloom-Filter,即布隆過濾器,1970年由Bloom中提出。它可以用於檢索一個元素是否在一個集合中,其優點是空間效率和查詢時間都遠遠超過其他算法,其不足在於Bloom- Filter存
這道題是我從網上找到的,據說是來自Exoweb,個人認爲這是一個很好的公司 “在國際象棋的棋盤上面有 NxN 個格。每個格里面有若干的米粒。一隻小豬站在 1x1 的格里,小豬每次只能向高位的列或行移動。小豬會喫掉所經過的格子裏面所有
1. 小學多位數乘法實現:http://blog.csdn.net/jiyanfeng1/article/details/8543846
1.首先,MR ao整理的ppt中的內容:spmv1.ppt ---》CSR,CSC,ELL,DIAGONAL~,Jagged diagonal format,Block compressed sparse row format~~ 2.
驗證多邊形是否爲凸多邊形 2108 Shape of HDU #include <stdio.h> #define _DEBUG 0 int crossMulti(int x0,int y0,int x1,int y1,int x
27. Remove Element 題目描述: Given an array nums and a value val, remove all instances of that value in-place and retu
58. Length of Last Word 題目描述: Given a string s consists of upper/lower-case alphabets and empty space characters '
從今天開始,辣雞劉學習一下搞搞算法,確定了leetcode網站,從最簡單的開始學習和思考。 入門算法, 1. Two Sum 描述: Given an array of integers, return indices of
Add Binary 題目描述; Given two binary strings, return their sum (also a binary string). The input strings are both no
對於準確率和召回率:一句話,準確率就是“找的對”,召回率就是“找的全”參考以下圖示: 區域A:能檢索到;想得到區域B:能檢索到;不想得到區域C:未檢索到;想得到區域D:未檢索到;不想得到注:準確率和
260. Single Number III Given an array of numbers nums, in which exactly two elements appear only once and all th
136. Single Number Given an array of integers, every element appears twice except for one. Find that single o
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, w
111. Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes
最長迴文字符串 題目描述 Catcher 是MCA國的情報員,他工作時發現敵國會用一些對稱的密碼進行通信,比如像這些ABBA,ABA,A,123321,但是他們有時會在開始或結束時加入一些無關的字符以防止別國破解。比如進行下列變化