Leading and Trailing (LightOJ - 1282,求n^k 的前三位 和 后三位)

本文详细解析了LightOJ-1282题目的解决策略,通过科学计数法和快速幂算法求解大数的前三位和后三位,提供了完整的代码实现。

一.题目链接:

LightOJ-1282

二.题目大意:

给出 n 和 k,求 n^{k} 的前三位 和 后三位.

三.分析:

后三位用快速幂求即可.

前三位求法:

首先复习一下科学计数法: A \times 10^{ B}   ( 0 < A  < 10)

n^{k} 一定可以表示成 10^{x + y} 的形式         ( x 为整数,y 为小数 )

即 10^{x} == 10^{B} , 10^{y} == A

\therefore n^{k} 的前三位 == A \times 100

\because n^{k} == 10^{x + y}

对等式两边取对数得:klgn == x + y

又 x 为整数,y 为小数

\therefore y == fmod(klgn, 1)

\therefore A == pow(10, y)

\therefore n^{k} 的前三位为 pow(10, y + 2)

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-4
#define PI acos(-1.0)
#define ll long long int
using namespace std;

ll quick_power(ll n, ll k)
{
    ll sum = 1;
    while(k)
    {
        if(k & 1)
            sum = ((sum % 1000) * (n % 1000)) % 1000;
        n = ((n % 1000) * (n % 1000)) % 1000;
        k >>= 1;
    }
    return sum % 1000;
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int c = 1; c <= T; ++c)
    {
        ll n, k;
        scanf("%lld %lld", &n, &k);
        ll ans1 = pow(10, 2 + fmod(k * log10(n), 1));
        ll ans2 = quick_power(n, k);
        printf("Case %d: ", c);
        printf("%d ", ans1);
        if (ans2 < 100)///比如 n^k 为 1002,ans2 == 2,但要输出三位
            printf("0");
        if (ans2 < 10)
            printf("0");
        printf("%lld\n", ans2);
    }
    return 0;
}

 

### ijson.common.IncompleteJSONError 错误解决方案 `ijson.common.IncompleteJSONError` 是在使用 `ijson` 解析 JSON 文件时,当文件格式不正确或包含多余内容时抛出的错误。此错误通常表明 JSON 文件不符合标准格式,例如存在多余的字符、未闭合的对象或组等[^1]。 以下是解决该问题的几种方法: #### 1. 检查 JSON 文件格式 确保 JSON 文件是有效的。可以使用在线工具(如 JSONLint)或编程语言中的验证工具来检查文件格式是否正确。如果发现文件存在问题,则需要修正原始文件[^2]。 ```python import json def validate_json_file(file_path): try: with open(file_path, 'r', encoding='utf-8') as f: json.load(f) # 尝试加载整个文件以验证其格式 return True except json.JSONDecodeError as e: print(f"JSON 文件无效: {e}") return False ``` #### 2. 使用容错模式解析 JSON 某些情况下,JSON 文件可能包含非标准的格式(如多文档 JSON 或带有注释的内容)。可以通过预处理文件或使用其他库(如 `json5` 或 `demjson`)来支持更宽松的语法[^3]。 ```python import demjson def parse_with_demjson(file_path): with open(file_path, 'r', encoding='utf-8') as f: data = demjson.decode(f.read(), strict=False) # 启用宽松模式解析 return data ``` #### 3. 分块读取并修复损坏部分 如果 JSON 文件过大且包含多个独立对象,可以尝试分块读取文件,并捕获跳过损坏的部分。这种方法适用于日志文件或其他类似结构的据[^4]。 ```python import ijson def process_large_json(file_path): with open(file_path, 'rb') as f: try: objects = ijson.items(f, 'item') # 假设顶层是一个组 for obj in objects: print(obj) except ijson.common.IncompleteJSONError: print("遇到损坏的 JSON 据,跳过...") ``` #### 4. 调整 ijson 的解析器 `ijson` 提供了多种后端解析器(如 `yajl2_cffi`、`yajl2` `python`),某些后端可能对特类型的错误更加健壮。可以尝试切换到不同的后端以查看是否能解决问题[^5]。 ```python import ijson.backends.yajl2_cffi as ijson_backend def use_alternative_backend(file_path): with open(file_path, 'rb') as f: objects = ijson_backend.items(f, 'item') for obj in objects: print(obj) ``` #### 5. 捕获异常并记录错误置 通过捕获异常并记录错误发生的置,可以帮助问题所在。这在调试大型 JSON 文件时特别有用[^6]。 ```python def find_error_position(file_path): with open(file_path, 'rb') as f: parser = ijson.parse(f) try: for prefix, event, value in parser: pass except ijson.common.IncompleteJSONError as e: print(f"错误置: {prefix}, {event}, {value}") ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值