Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:真不知道我哪里错了 结果一样的In Reply To:真不知道我哪里错了 结果一样的 Posted by:CrossChao at 2009-11-28 22:01:50 #include <iostream> #include <string> using namespace std; void caculate( string string1, int cinum ); int main() { string string1; int cinum; while( cin >> string1 >> cinum ) { caculate( string1, cinum ); } return 0; } void caculate( string string1, int cinum ) { int numTmp[500], rs[500], ping[500][500],dot(0),z(0),z2(0),pingHMax(0),jz(0);//dot 记录被乘数小数点后面的位数 int i(0),j(0); //判断string1中的数字是否有小数点 numTmp[0] = string1.length();//默认情况下认为不含有小数点 for( i = 0; i < string1.length(); ++ i ) { if( (int)string1[i] == 46 ) { numTmp[0] = string1.length() - 1;//numTmp数组第0位记录长度,-1表示除去小数点 dot = string1.length() - 1 - i; break; } } //结束判断string1中的数字是否有小数点 for( i = 0, j = 0; j < numTmp[0] ; ++ i, ++j )//将string1中的内容压入numTmp数组中,0位为长度 { if( (int)string1[i] == 46 ) { --j; continue; } numTmp[j + 1] = string1[i] - 48; //cout << "string[" << i <<"]" << string1[i] << endl; //cout << j <<" " << string1.length()<< endl; //cout << numTmp[j+1] << endl; } //TEST BEGIN /** for( i = 0; i <= numTmp[0] ; ++ i ) // test numTmp[500] { cout << i << " : " << numTmp[i] << endl; } cout << "test for [int]dot" << dot << endl; //test dot /**/ //TEST END //开始清除numTmp数组末尾的无效0 if( dot != 0 ) { for( i = numTmp[0]; i > 0; -- i ) { if( numTmp[i] == 0 ) { --numTmp[0]; -- dot; } else { break;//第一次遇到不是0的数字则跳出清除循环 } } } //清除完毕 /**开始将numTmp[500]内容复制到rs[500]数组中,为第一次运算做准备**/ for( i = 0; i < numTmp[0] + 1; ++ i ) { rs[i] = numTmp[i]; } /**复制完毕**/ //test begin /** for( i = 0; i < rs[0]; ++ i ) { cout << rs[i] << endl; } **/ //test end for( int ci = 1; ci < cinum; ++ ci ) { //test begin /** for( i = 0; i < rs[0]; ++ i ) { cout << rs[i + 1]; } cout << endl; /**/ //test end /**开始计算,利用过渡矩阵ping[500][500]**/ for( i = numTmp[0]; i > 0; -- i )//外层循环numTmp[0]次 { int k = 0;//k用来每次暂时存储运算矩阵ping[][]的每一行的位数 //int jz = 0;//运算进制标志 //int z = 0;//用于运算矩阵的位标志,在前面声明过 //test begin //cout << "z: " << z << endl; //test end for( j = rs[0], z2 = 0; j > 0; -- j, ++ z2 )//内层 { if( z > 0 && z2 == 0 )//结构化运算矩阵中的内容,是最后合并计算简便好算 { for( int q = z; q > 0; -- q ) { ping[z][z2+1] = 0; ++ z2; ++ k; } } ping[z][z2 + 1] = numTmp[i] * rs[j] + jz; //test begin //cout << numTmp[i] << "*" << rs[j] << endl; //cout << "ping[][] : z = " << z << " " << ping[z][z2 + 1] << endl; //system("pause"); //test end jz = 0;//恢复jz标志 ++ k;//迭进位数计数 if( ping[z][z2 + 1] > 9 )//判断是否进位 { jz = ping[z][z2+1] / 10;//进位标记 ping[z][z2 + 1] %= 10; //test begin //cout << "jz:" << jz << endl; //cout << "ping[z][z+1]= " << ping[z][z2+1] << endl; //test end } } //ping[z][0] = k;//存储位数 //检查是否还遗留有进位 while( jz > 0 )//如果还需要进制 { ping[z][++ k] = jz % 10; jz /= 10; //test begin //cout << "jz" << jz << endl; //cout << "ping[][]: " << ping[z][k] << endl; //cout << "行位数:" << k << endl; //test end } ping[z][0] = k;//存储位数 k = jz = 0;//恢复 ++ z; } /**计算结束**/ //test begin - 测试运算矩阵第一次平方的内部值 注意 运算矩阵的行数值是 numtmp[0] //cout << numTmp[0] << endl; //cout << "ping[][]" << ping[3][0] << endl; /** for( i = 0; i < numTmp[0]; ++i ) { for( j = 0; j <= ping[i][0]; ++j ) { cout << ping[i][j] <<" "; } cout << endl; } /**/ //test end //开始找出运算矩阵中长度值最大行,存储于pingHMax中 for( i = 0; i < numTmp[0]; ++ i ) { if( ping[i][0] > pingHMax ) { pingHMax = ping[i][0]; } } //运算矩阵中长度值最大行超找完毕 //test begin //cout << "pingHMax: " << pingHMax << endl; //test end jz = 0;//回复jz初始状态 /**开始合并运算矩阵ping,将结果存入rs中,运算矩阵斜线相加**/ for( i = 0; i < pingHMax; ++ i ) { rs[pingHMax - i] = 0;//先对结果此位数组值置零 for( j = 0; j < numTmp[0]; ++ j ) { if( i >= ping[j][0] ) { continue; } rs[pingHMax - i] += ping[j][i + 1]; //test begin //cout << "ping[][]:" << ping[j][i + 1] << " "; //test end } //test begin //cout << "test: " << rs[pingHMax - i] << endl; //test end rs[pingHMax - i] += jz;//加上进位的数 jz = 0;//进位后恢复jz标志 if( rs[pingHMax - i] > 9 )//检测是否需要进制 { jz = rs[pingHMax - i] / 10 ; rs[pingHMax - i] %= 10; } } rs[0] = pingHMax;//改变rs中记录长度的数值 z = 0;//恢复z,为下次用 //至此第一次平方完毕 //test begin //cout << "ci" << ci << endl; /** system("pause"); for( i = 0; i < rs[0]; ++ i ) { cout << rs[i + 1]; } cout << endl; **/ //test end } dot *= cinum;//计算小数点位数 //输出前开始对结果最后的连续0进行清除,注意是小数点后面的0,不是小数点前面的0 /** for( i = rs[0]; true; -- i ) { if( rs[i] == 0) { -- rs[0]; } else { break; } } //清除完毕 **/ //开始输出最后的结果 if( rs[1] == 0 && rs[0] != 1) {} else if( rs[0] == 1 ) { cout << rs[1]; } else { cout << rs[1]; } for( i = 1; i < rs[0]; ++ i ) { //test begin //cout << "dot: " << dot << "rs[0]: " << rs[0] << endl; //test end if( i == rs[0] - dot )//判断是否输出小数点 { if( i != rs[0] )//不打印小数点如果结果是整数 { cout <<'.'; } } cout << rs[i + 1]; } cout << endl; //输出最后的结果完毕 } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator