题目99:文件中哪一个 基/指数 对具有最大的数值?
Largest exponentialComparing two numbers written in index form likeis not difficult, as any calculator would confirm that .
However, confirming thatwould be much more difficult, as both numbers contain over three million digits.
Using base_exp.txt (right click and 'Save Link/Target As...'), a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value.
NOTE: The first two lines in the file represent the numbers in the example given above.
题目:
比较两个写成上标形式的数并不困难,比如,因为任何计算机都会告诉你。
但是,要确实就要困难很多,因为这两个数都包含超过三百万位数。
包含一千行内容,每一行都包含一个基和一个指数,找出哪一行具有最大的值。
注意:前两行的数字就是上面给出的例子。
这题只要学过对数的同学就很容易解决了,用对数比较轻松+愉快!{:5_109:}
709
代码中间的原始数据不贴全了,自己补上即可。
data = '''519432,525806
632382,518061
78864,613712
466580,530130
。。。。。。
。。。。。。
。。。。。。
325361,545187
172115,573985
13846,725685'''
import math
data = str(data)
data = data.replace(chr(10),',').split(',')
data =
maxi, maxn = 0, 0
for i in range(1000):
num = data*math.log(data)
if num > maxn:
maxn, maxi = num, i
print maxi+1
本帖最后由 永恒的蓝色梦想 于 2020-6-1 13:26 编辑
感谢楼上提供的思路{:10_256:}
fstream 慢死了{:10_306:} #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main() {
unsigned short line, maxline;
unsigned int a, b;
double n, maxn = 0.0L;
FILE* file = fopen("p099_base_exp.txt", "r");
for (line = 1; line <= 1000; line++) {
fscanf(file, "%d,%d", &a, &b);
n = log(a) * b;
if (n > maxn) {
maxn = n;
maxline = line;
}
}
fclose(file);
printf("%d", maxline);
return 0;
} 本帖最后由 永恒的蓝色梦想 于 2021-1-8 20:08 编辑
from math import log
maxn = 0.0
maxline = 0
for line, string in enumerate(open("p099_base_exp.txt"), 1):
a, b = string.split(',', 1)
n = log(int(a)) * int(b)
if n > maxn:
maxn = n
maxline = line
print(maxline) 709
Process returned 0 (0x0) execution time : 0.024 s
Press any key to continue.
取对数,没啥好说的
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const int M = 1000;
int main(){
freopen("i.in","r",stdin);
double mx = 0;
int ans;
for (int r = 1;r <= M;r++){
int a,b;
scanf("%d,%d",&a,&b);
double t = b*log(a);
if (t > mx) {mx = t;ans = r;}
}
cout << ans << endl;
return 0;
}
页:
[1]