大整数乘法
2011年7月31日 16:37 | Comments(4) | Category:C++ | Tags:c++ 高精度
#include <iostream> #include <memory> using namespace std; int* multi(int* num1, int size1, int* num2, int size2) { int size = size1 + size2; int* ret = new int[size]; int i = 0; memset(ret, 0, sizeof(int)*size); //先把两数组分别相乘 for(i = 0; i < size2; ++i) { int k = i; for(int j = 0; j < size1; ++j) { ret[k++] += num2[i]*num1[j]; } } //进位 for(i = 0; i < size; ++i) { if(ret[i] >= 10) { //加上进位 ret[i+1] += ret[i] / 10; //只留个位 ret[i] %= 10; } } return ret; } int main(int argc, char *argv[]) { int num1[] = {1,2,3,4,5,6,7,8,9,1,1,1,1,1}; int num2[] = {1,1,1,2,2,2,3,3,3,4,4,4,5,5}; int* ret = multi(num1, 14, num2, 14); for(int i = 27; i >= 0; i--) { cout << ret[i]; } delete[] ret; return 0; }
计算11111987654321*55444333222111=616096746266157102781891631
中文汉字转拼音首字母
2010年8月05日 03:43 | Comments(1) | Category:C++ | Tags:c++ 转换 宽字符 拼音
/*ConvertToPY v0.1 author:star date:2010-02-25 */ #include <iostream> #include <string> using namespace std; static char convert(wchar_t n); static bool In(wchar_t start, wchar_t end, wchar_t code); int main(int argc, char* argv[]) { string sChinese = "取得拼音的首字母"; // 输入的字符串 char chr[3]; wchar_t wchr = 0; char* buff = new char[sChinese.length()/2]; memset(buff, 0, sizeof(char)*sChinese.length()/2+1); for (int i = 0, j = 0; i < (sChinese.length()/2); ++i) { memset(chr, 0, sizeof(chr)); chr[0] = sChinese[j++]; chr[1] = sChinese[j++]; chr[2] = '\0'; wchr = 0; wchr = (chr[0] & 0xff) << (1<<3); wchr |= (chr[1] & 0xff); buff[i] = convert(wchr); } cout << "结果 :[" << buff << "]" << endl; return 0; } char convert(wchar_t n) { if (In(0xB0A1,0xB0C4,n)) return 'A'; if (In(0XB0C5,0XB2C0,n)) return 'B'; if (In(0xB2C1,0xB4ED,n)) return 'C'; if (In(0xB4EE,0xB6E9,n)) return 'D'; if (In(0xB6EA,0xB7A1,n)) return 'E'; if (In(0xB7A2,0xB8c0,n)) return 'F'; if (In(0xB8C1,0xB9FD,n)) return 'G'; if (In(0xB9FE,0xBBF6,n)) return 'H'; if (In(0xBBF7,0xBFA5,n)) return 'J'; if (In(0xBFA6,0xC0AB,n)) return 'K'; if (In(0xC0AC,0xC2E7,n)) return 'L'; if (In(0xC2E8,0xC4C2,n)) return 'M'; if (In(0xC4C3,0xC5B5,n)) return 'N'; if (In(0xC5B6,0xC5BD,n)) return 'O'; if (In(0xC5BE,0xC6D9,n)) return 'P'; if (In(0xC6DA,0xC8BA,n)) return 'Q'; if (In(0xC8BB,0xC8F5,n)) return 'R'; if (In(0xC8F6,0xCBF0,n)) return 'S'; if (In(0xCBFA,0xCDD9,n)) return 'T'; if (In(0xCDDA,0xCEF3,n)) return 'W'; if (In(0xCEF4,0xD188,n)) return 'X'; if (In(0xD1B9,0xD4D0,n)) return 'Y'; if (In(0xD4D1,0xD7F9,n)) return 'Z'; return 0; } inline bool In(wchar_t start, wchar_t end, wchar_t code) { if (code >= start && code <= end) return true; return false; }