关于C++里的简单却要命的数学问题
关于大数以及引入log10后的简化问题
这题看到的时候菜菜子的第一想法是大数求n!然后计算字符串长度
不会真有人这么做吧?
后来看到别人的代码才发现自己许久未用的log10函数在此真的超级好用:
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
int m;
cin >> m;
double ans=0;
for (int i = 1; i <= m; i++)
ans += log10(i);
cout <<int (ans)+1 << endl;
}
}
将大数乘法通过log10转换成小数加法,舒坦啊~~
求一个数n的位数->联想到(int)log10(n)+1
hdu1066:你以为是大数问题结果???!!!
这题考的是数学规律啊哈哈~~(哭了哭了)~~
关于其实不是大数,但是意外因为0忘记考虑从而一直20分的悲惨故事
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
char putout[100];
char table[50] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
int much(char a) {
if (a <= '9' && a >= '0')return a - '0';
else return a - 'A' + 10;
}
long long get_num(string a,long long k) {//将字符串转化为十进制数
long long ans=0;
for (int i =0; i<a.length();i++) {
ans = ans * k + much(a[i]);
}
return ans;
}
int main() {
long long t, num=0, nnum,k=10;
string org,s_num,s_nnum;
int flag = 0;
cin >> t;
while (t--) {
cin >> org;
if (org == "CLEAR") {
num = 0;
s_num = "";
flag = 0;
}
else if (org == "NUM") {
if (flag == 0) {
cin >> s_num;
num = get_num(s_num, k);
}
else {
cin >> s_nnum;
nnum = get_num(s_nnum,k);
switch (flag) {
case 1:num += nnum; break;
case 2:num -= nnum; break;
case 3:num *= nnum; break;
case 4:num /= nnum; break;
case 5:num %= nnum; break;
default:break;
}
}
}
else if (org == "CHANGE") {
cin >> k;
}
else if (org == "EQUAL") {
//输出答案(进制转换)
//结果因为忘记考虑结果为零的情况一直错啊哈哈哈
if (num == 0) {
cout << "0" << endl; continue;
}
long long i = 0;
long long cc = num;
//cout << "cc=" << cc << endl;
while (cc) {
putout[i] = table[cc % k];
cc /= k;
i++;
}
i--;
while (i>=0) {
cout << putout[i];
i--;
}
cout << endl;
}
else {
if (org == "ADD")flag = 1;
else if (org == "SUB")flag = 2;
else if (org == "MUL")flag = 3;
else if (org == "DIV")flag = 4;
else if (org == "MOD")flag = 5;
}
}
}