PAT(A) - 1096. Consecutive Factors (20)

本文介绍了一种算法,用于找出一个正整数的所有因子中,最长的连续因子序列,并给出具体的实现代码。输入一个正整数N,输出最长连续因子的数量及最小的连续因子序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.

Input Specification:

Each input file contains one test case, which gives the integer N (1<N<231).

Output Specification:

For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format "factor[1]*factor[2]*...*factor[k]", where the factors are listed in increasing order, and 1 is NOT included.

Sample Input:
630
Sample Output:
3
5*6*7


思路分析: 一个分解因子的问题,要去找出最长的连续因子。我的做法是从2开始遍历(题目说1除外),依次判断当前数组能否被N整除,边找边记录连续因子的长度,如果整除不尽,找到第一个因子的位置,我的程序里是head = j - curLen + 1,j为当前整除不尽的因子,curLen为当前连续因子的长度,所以head就出来了。最后输出连续因子的长度。如果最后这个数只能被自身整除,那就输出它自己。


#include <cstdio>
#include <cmath>

int main() {
    int n;
    scanf( "%d", &n );

    int sqr = (int)sqrt( n );
    int maxLen = 0;
    int head = 0;

    for( int i = 2; i <= sqr; i++ ) {
        int curLen = 0;
        int temp = 1;
        int j = i;

        while( 1 ) {
            temp = temp * j;
            if( n % temp == 0 ) {
                curLen++;
                if( curLen > maxLen ) {
                    maxLen = curLen;
                    head = j - curLen + 1;
                }
            }
            else {
                break;
            }
            j++;
        }
    }

    if( head == 0 ) {
        printf( "1\n%d", n );
        return 0;
    }
    printf( "%d\n", maxLen );
    for( int i = head; i < maxLen + head; i++ ) {
        if( i == head ) printf( "%d", i );
        else printf( "*%d", i );
    }
    return 0;
}


ymtan@ecs-lj-openfoam:~/OpenFOAM/ymtan-v2412/run/250425/rigidBodyHull/background$ checkMesh -allGeometry -allTopology /*---------------------------------------------------------------------------*\ | ========= | | | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | | \\ / O peration | Version: 2412 | | \\ / A nd | Website: www.openfoam.com | | \\/ M anipulation | | \*---------------------------------------------------------------------------*/ Build : _8dbc61e11c-20241220 OPENFOAM=2412 version=v2412 Arch : "LSB;label=32;scalar=64" Exec : checkMesh -allGeometry -allTopology Date : May 28 2025 Time : 17:22:26 Host : ecs-lj-openfoam PID : 1115230 I/O : uncollated Case : /home/ymtan/OpenFOAM/ymtan-v2412/run/250425/rigidBodyHull/background nProcs : 1 trapFpe: Floating point exception trapping enabled (FOAM_SIGFPE). fileModificationChecking : Monitoring run-time modified files using timeStampMaster (fileModificationSkew 5, maxFileModificationPolls 20) allowSystemOperations : Allowing user-supplied system call operations // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Create time Create mesh for time = 0 Check mesh... Enabling all (cell, face, edge, point) topology checks. Enabling all geometry checks. Time = 0 Mesh stats points: 1171077 faces: 3255026 internal faces: 3144844 cells: 1045876 faces per cell: 6.119148 boundary patches: 13 point zones: 1 face zones: 0 cell zones: 0 Overall number of cells of each type: hexahedra: 970669 prisms: 12876 wedges: 7 pyramids: 0 tet wedges: 65 tetrahedra: 0 polyhedra: 62259 Breakdown of polyhedra by number of faces: faces number of cells 4 5946 5 4504 6 14031 7 110 8 139 9 26892 10 25 11 68 12 7666 13 3 14 35 15 2620 16 1 17 6 18 212 21 1 Checking topology... ****Problem with boundary patch 0 named oversetPatch of type overset. The patch should start on face no 3144844 and the patch specifies 3243735. Possibly consecutive patches have this same problem. Suppressing future warnings. ***Boundary definition is in error. Cell to face addressing OK. Point usage OK. Upper triangular ordering OK. Face vertices OK. Topological cell zip-up check OK. <<Number of duplicate (not baffle) faces found: 22. This might indicate a problem. <<Number of faces with non-consecutive shared points: 22. This might indicate a problem. <<Writing 66 faces with non-standard edge connectivity to set edgeFaces *Number of regions: 3 The mesh has multiple regions which are not connected by any face. <<Writing region information to "0/cellToRegion" <<Writing region 0 (fully disconnected) with 517350 cells to cellSet region0 <<Writing region 1 (fully disconnected) with 383501 cells to cellSet region1 <<Writing region 2 (fully disconnected) with 145025 cells to cellSet region2 Checking patch topology for multiply connected surfaces... Patch Faces Points Surface topology Bounding box oversetPatch 11291 11345 ok (closed singly connected) (-3.5137747 -0.11385833 0.015782884) (-3.3823898 0.11380627 0.24342155) overset-1 8440 8442 ok (closed singly connected) (-4.9998287 -1.1765634 -0.4005183) (5.0000125 1.1765635 2.6677242) outlet 3060 3172 ok (non-closed singly connected) (-7 -3 -1.8) (-7 3 5.28) inlet 3060 3172 ok (non-closed singly connected) (7 -3 -1.8) (7 3 5.28) ymin 7200 7381 ok (non-closed singly connected) (-7 -3 -1.8) (7 -3 5.28) ymax 7200 7381 ok (non-closed singly connected) (-7 3 -1.8) (7 3 5.28) zmin 6120 6292 ok (non-closed singly connected) (-7 -3 -1.8) (7 3 -1.8) atmosphere 6120 6292 ok (non-closed singly connected) (-7 -3 5.28) (7 3 5.28) HULL 29179 29931 ok (non-closed singly connected) (-3.600006 -0.60945788 -1.9512023e-06) (3.5892613 0.60945883 1.933472) HULL_DISK 87 105 ok (non-closed singly connected) (-3.3732875 -0.019542581 0.10979212) (-3.3724382 0.019934195 0.14859714) BLADES 26507 33135 ok (non-closed singly connected) (-3.4375999 -0.095075928 0.041281019) (-3.4059672 0.098815381 0.23087751) HUB_DISK 88 114 ok (non-closed singly connected) (-3.3985371 -0.020660421 0.10878564) (-3.3972407 0.020647007 0.15033154) HUB 1830 2674 ok (non-closed singly connected) (-3.4603339 -0.020660421 0.10878564) (-3.39743 0.020647007 0.15033154) ".*" 98891 106359 ok (closed singly connected) (-7 -3 -1.8) (7 3 5.28) Checking faceZone topology for multiply connected surfaces... No faceZones found. Checking basic cellZone addressing... No cellZones found. Checking basic pointZone addressing... PointZone PointsBoundingBox frozenPoints 0(1e+150 1e+150 1e+150) (-1e+150 -1e+150 -1e+150) Checking geometry... Overall domain bounding box (-7 -3 -1.8) (7 3 5.28) Mesh has 3 geometric (non-empty/wedge) directions (1 1 1) Mesh has 3 solution (non-empty) directions (1 1 1) Boundary openness (-2.1191257e-16 -1.1701675e-16 1.0552411e-15) OK. Max cell openness = 3.4499493e-16 OK. Max aspect ratio = 10.58398 OK. Minimum face area = 1.8622712e-09. Maximum face area = 0.015462575. Face area magnitudes OK. Min volume = 6.7187714e-11. Max volume = 0.0020033185. Total volume = 662.9577. Cell volumes OK. Mesh non-orthogonality Max: 69.978861 average: 5.2077846 Non-orthogonality check OK. Face pyramids OK. ***Max skewness = 18.358061, 36 highly skew faces detected which may impair the quality of the results <<Writing 36 skew faces to set skewFaces Coupled point location match (average 0) OK. ***Error in face tets: 2 faces with low quality or negative volume decomposition tets. <<Writing 2 faces with low quality or negative volume decomposition tets to set lowQualityTetFaces *Edges too small, min/max edge length = 3.0158475e-06 0.12979014, number too small: 18 <<Writing 18 points on short edges to set shortEdges *There are 1128 faces with concave angles between consecutive edges. Max concave angle = 79.784669 degrees. <<Writing 1128 faces with concave angles to set concaveFaces Face flatness (1 = flat, 0 = butterfly) : min = 0.35091858 average = 0.99944701 *There are 66 faces with ratio between projected and actual area < 0.8 Minimum ratio (minimum flatness, maximum warpage) = 0.35091858 <<Writing 66 warped faces to set warpedFaces Cell determinant (wellposedness) : minimum: 0.00098934483 average: 1.3639416 ***Cells with small determinant (< 0.001) found, number of cells: 1 <<Writing 1 under-determined cells to set underdeterminedCells ***Concave cells (using face planes) found, number of cells: 854 <<Writing 854 concave cells to set concaveCells Face interpolation weight : minimum: 0.065401151 average: 0.48202057 Face interpolation weight check OK. Face volume ratio : minimum: 0.011817813 average: 0.91971027 Face volume ratio check OK. Failed 4 mesh checks. Time = 0.1 Checking geometry... Overall domain bounding box (-7.0010041 -3.0000007 -1.8081815) (6.9989959 2.9999993 5.2718185) Mesh has 3 geometric (non-empty/wedge) directions (1 1 1) Mesh has 3 solution (non-empty) directions (1 1 1) Boundary openness (-1.4247891e-16 -4.8513891e-17 1.0347421e-15) OK. Max cell openness = 3.3602158e-16 OK. Max aspect ratio = 10.65007 OK. Minimum face area = 1.8622712e-09. Maximum face area = 0.015462575. Face area magnitudes OK. Min volume = 6.7187714e-11. Max volume = 0.0020033185. Total volume = 662.9577. Cell volumes OK. Mesh non-orthogonality Max: 69.978861 average: 5.2077846 Non-orthogonality check OK. Face pyramids OK. ***Max skewness = 18.358061, 36 highly skew faces detected which may impair the quality of the results <<Writing 36 skew faces to set skewFaces Coupled point location match (average 0) OK. ***Error in face tets: 2 faces with low quality or negative volume decomposition tets. <<Writing 2 faces with low quality or negative volume decomposition tets to set lowQualityTetFaces *Edges too small, min/max edge length = 3.0158475e-06 0.12979014, number too small: 18 <<Writing 18 points on short edges to set shortEdges *There are 1128 faces with concave angles between consecutive edges. Max concave angle = 79.784669 degrees. <<Writing 1128 faces with concave angles to set concaveFaces Face flatness (1 = flat, 0 = butterfly) : min = 0.35091858 average = 0.99944701 *There are 66 faces with ratio between projected and actual area < 0.8 Minimum ratio (minimum flatness, maximum warpage) = 0.35091858 <<Writing 66 warped faces to set warpedFaces Cell determinant (wellposedness) : minimum: 0.00098934483 average: 1.3639416 ***Cells with small determinant (< 0.001) found, number of cells: 1 <<Writing 1 under-determined cells to set underdeterminedCells ***Concave cells (using face planes) found, number of cells: 854 <<Writing 854 concave cells to set concaveCells Face interpolation weight : minimum: 0.065401151 average: 0.48202057 Face interpolation weight check OK. Face volume ratio : minimum: 0.011817813 average: 0.91971027 Face volume ratio check OK. Failed 4 mesh checks. Time = 0.2 Checking geometry... Overall domain bounding box (-7.004549 -3.0000049 -1.8351793) (6.995451 2.9999951 5.2448207) Mesh has 3 geometric (non-empty/wedge) directions (1 1 1) Mesh has 3 solution (non-empty) directions (1 1 1) Boundary openness (-1.3099804e-16 -9.8277796e-17 1.0607913e-15) OK. Max cell openness = 3.2277216e-16 OK. Max aspect ratio = 10.943125 OK. Minimum face area = 1.8622712e-09. Maximum face area = 0.015462575. Face area magnitudes OK. Min volume = 6.7187714e-11. Max volume = 0.0020033185. Total volume = 662.9577. Cell volumes OK. Mesh non-orthogonality Max: 69.978861 average: 5.2077846 Non-orthogonality check OK. Face pyramids OK. ***Max skewness = 18.358061, 36 highly skew faces detected which may impair the quality of the results <<Writing 36 skew faces to set skewFaces Coupled point location match (average 0) OK. ***Error in face tets: 2 faces with low quality or negative volume decomposition tets. <<Writing 2 faces with low quality or negative volume decomposition tets to set lowQualityTetFaces *Edges too small, min/max edge length = 3.0158475e-06 0.12979014, number too small: 18 <<Writing 18 points on short edges to set shortEdges *There are 1128 faces with concave angles between consecutive edges. Max concave angle = 79.784669 degrees. <<Writing 1128 faces with concave angles to set concaveFaces Face flatness (1 = flat, 0 = butterfly) : min = 0.35091858 average = 0.99944701 *There are 66 faces with ratio between projected and actual area < 0.8 Minimum ratio (minimum flatness, maximum warpage) = 0.35091858 <<Writing 66 warped faces to set warpedFaces Cell determinant (wellposedness) : minimum: 0.00098934483 average: 1.3639416 ***Cells with small determinant (< 0.001) found, number of cells: 1 <<Writing 1 under-determined cells to set underdeterminedCells ***Concave cells (using face planes) found, number of cells: 854 <<Writing 854 concave cells to set concaveCells Face interpolation weight : minimum: 0.065401151 average: 0.48202057 Face interpolation weight check OK. Face volume ratio : minimum: 0.011817813 average: 0.91971027 Face volume ratio check OK. Failed 4 mesh checks. Time = 0.3 Checking geometry... Overall domain bounding box (-7.0097797 -3.0000129 -1.8732831) (6.9902203 2.9999871 5.2067169) Mesh has 3 geometric (non-empty/wedge) directions (1 1 1) Mesh has 3 solution (non-empty) directions (1 1 1) Boundary openness (-1.7685801e-16 -1.5646894e-16 1.0557562e-15) OK. Max cell openness = 3.6004607e-16 OK. Max aspect ratio = 8.296384 OK. Minimum face area = 1.8622712e-09. Maximum face area = 0.015462575. Face area magnitudes OK. Min volume = 6.7187714e-11. Max volume = 0.0020033185. Total volume = 662.9577. Cell volumes OK. Mesh non-orthogonality Max: 69.978861 average: 5.2077846 Non-orthogonality check OK. Face pyramids OK. ***Max skewness = 18.358061, 36 highly skew faces detected which may impair the quality of the results <<Writing 36 skew faces to set skewFaces Coupled point location match (average 0) OK. ***Error in face tets: 2 faces with low quality or negative volume decomposition tets. <<Writing 2 faces with low quality or negative volume decomposition tets to set lowQualityTetFaces *Edges too small, min/max edge length = 3.0158475e-06 0.12979014, number too small: 18 <<Writing 18 points on short edges to set shortEdges *There are 1128 faces with concave angles between consecutive edges. Max concave angle = 79.784669 degrees. <<Writing 1128 faces with concave angles to set concaveFaces Face flatness (1 = flat, 0 = butterfly) : min = 0.35091858 average = 0.99944701 *There are 66 faces with ratio between projected and actual area < 0.8 Minimum ratio (minimum flatness, maximum warpage) = 0.35091858 <<Writing 66 warped faces to set warpedFaces Cell determinant (wellposedness) : minimum: 0.00098934483 average: 1.3639416 ***Cells with small determinant (< 0.001) found, number of cells: 1 <<Writing 1 under-determined cells to set underdeterminedCells ***Concave cells (using face planes) found, number of cells: 854 <<Writing 854 concave cells to set concaveCells Face interpolation weight : minimum: 0.065401151 average: 0.48202057 Face interpolation weight check OK. Face volume ratio : minimum: 0.011817813 average: 0.91971027 Face volume ratio check OK. Failed 4 mesh checks. End
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值