首页 > 其他分享 >PAT Basic 1096. 大美数

PAT Basic 1096. 大美数

时间:2023-04-15 14:15:25浏览次数:43  
标签:1096 正整数 int 大美数 因数 PAT

PAT Basic 1096. 大美数

1. 题目描述:

若正整数 \(N\) 可以整除它的 4 个不同正因数之和,则称这样的正整数为“大美数”。本题就要求你判断任一给定的正整数是否是“大美数”。

2. 输入格式:

输入在第一行中给出正整数 \(K\)(\(≤10\)),随后一行给出 \(K\) 个待检测的、不超过 \(10^4\) 的正整数。

3. 输出格式:

对每个需要检测的数字,如果它是大美数就在一行中输出 Yes,否则输出 No

4. 输入样例:

3
18 29 40

5. 输出样例:

Yes
No
Yes

6. 性能要求:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

一开始想着能找出大美数的规律,但是只能想到\(N\)为大美数时,4个不同正因数之和应为\(N,2N\)或\(3N\)。。。(因为4个不同正因数小于等于\(N\),其和一定小于\(4N\),不过这个条件好像没什么用),最后还是比较直接的思路:对于每个正整数\(N\)先找出其所有的正因数,再遍历这些正因数的所有组合情况依次判断,这里编写子函数judgeBigBeauty()进行逻辑判断,其中求解给定数组的所有组合情况的子函数combination()参考了:【递归+回溯】实现数组元素的组合、排列和全排列_数组元素组合方案_灰小猿的博客-CSDN博客 ,这里需要注意的一个细节是:因为C中数组作为参数时是以指针的形式传递,所以想获取数组大小的话需要额外再定义相关的参数。

AC后参考大佬的代码:1096 大美数 – PAT乙级真题_1096 大美数 柳诺_柳婼的博客-CSDN博客 ,一种比较简洁的思路,只能说智商被压制了。。。

My Code:

#include <stdio.h>

#define MAX_NUM 10000

int judgeBigBeauty(int num);
void combination(int *arr, int * ans, int k, int n, int arrLen, int ansLen, int *firstBlood, int num);

int main(void)
{
    int numCount = 0;
    int tempNum = 0;
    int i=0; // iterator
    
    scanf("%d", &numCount);
    for(i=0; i<numCount; ++i)
    {
        scanf("%d", &tempNum);
        if(judgeBigBeauty(tempNum))// judge num
        {
            printf("Yes\n");
        }
        else
        {
            printf("No\n");
        }
    }
    
    return 0;
}

int judgeBigBeauty(int num)
{
    int factor[MAX_NUM] = {0};
    int factorCount=0;
    int i=0; // iterator
    int flag=0; // flag of BigBeauty
    int tempCombination[4];
    
    for(i=1; i*i<num; ++i) // find all factors of num
    {
        if(num % i == 0)
        {
            factor[factorCount++] = i;
            factor[factorCount++] = num/i;
        }
    }
    if(i*i == num) // add sqrt(num) if it's a factor of num
    {
        factor[factorCount++] = i;
    }
    
    if(factorCount<4) return 0; // the number of factor of num is less than 4, return false directly
    
    combination(factor, tempCombination, 0, 4, factorCount, 4, &flag, num);
    
    return flag;
}

// refer to https://blog.csdn.net/weixin_44985880/article/details/113434593
// find all combination of an array
/*
    arr: original array
    ans: the result combination of array
    k: the start element index of select
    n: number of selection
    arrLen: length of arr
    ansLen: length of ans
    firstBlood: flag of end recurse
    num: the num to be judged
*/
void combination(int *arr, int * ans, int k, int n, int arrLen, int ansLen, int *firstBlood, int num)
{
    // int arrLen = sizeof(arr)/sizeof(int);
    // int ansLen = sizeof(ans)/sizeof(int);
    // printf("arrLen: %d, ansLen: %d\n", arrLen, ansLen);
    if(n==0)
    {
        int tempSum = 0;

        for(int i=0; i<ansLen; ++i)
        {
            //printf("%d ", ans[i]);
            tempSum += ans[i];
        }
        //printf("\n");
        if(tempSum%num == 0)
        {
            *firstBlood = 1;
        }
        return;
    }
    for(int i=k; i<= arrLen-n && !(*firstBlood); ++i)
    {
        ans[ansLen-n] = arr[i];
        combination(arr, ans, i+1, n-1, arrLen, ansLen, firstBlood, num);
    }
}


// // refer to https://blog.csdn.net/liuchuo/article/details/126209590
// #include <stdio.h>

// int main(void)
// {
//     int numCount = 0;
//     int tempNum = 0;
//     int i=0; // iterator
//     int flag = 0;
    
//     scanf("%d", &numCount);
//     for(i=0; i<numCount; ++i)
//     {
//         scanf("%d", &tempNum);
//         flag = 0;
//         for(int a=1; a<=tempNum; ++a)
//         {
//             if(flag) break;
//             if(tempNum%a != 0) continue;
//             for(int b=a+1; b<=tempNum; ++b)
//             {
//                 if(flag) break;
//                 if(tempNum%b != 0) continue;
//                 for(int c=b+1; c<=tempNum; ++c)
//                 {
//                     if(flag) break;
//                     if(tempNum%c != 0) continue;
//                     for(int d=c+1; d<=tempNum; ++d)
//                     {
//                         if(flag) break;
//                         if(tempNum%d != 0) continue;
//                         if((a+b+c+d) % tempNum == 0) flag = 1;
//                     }
//                 }
//             }
//         }
        
//         printf("%s\n", flag?"Yes":"No");
//     }
    
//     return 0;
// }

标签:1096,正整数,int,大美数,因数,PAT
From: https://www.cnblogs.com/tacticKing/p/17321012.html

相关文章

  • 二进制patch工具xdelta的使用方法
     Xdelta是一个二进制的diff工具[同时又兼具了patch功能],diff和patch是Unix世界里很有用的一对工具:我们通常将它们结合起来实现生成补丁,应用补丁的目的。如果要处理的不是文本文件,是二进制文件,我们可以使用一个专门用来处理二进制文件的工具–xdelta。      Xdelta......
  • TypeScript 报错:Type '({ filename: string; createTime: string; filePath: string;
    问题:因为TypeScript不支持直接给一个接口类型的变量赋一个未知的值。如consta:A={ name:'s'};你需要给这样的对象或数组值使用as指定一个类型。正确写法:consta:A={ name:'s'}asA;数组写法一样:consta:A[]=[ { name:'s' }]asA[];使用as将一......
  • UVA 12295 Optimal Symmetric Paths 最短路求方案数
    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23587题意:给一个n*n的矩阵,每个方格中有一个数字,从左上角走到右下角,且路径必须关于副对角线对称,求使路线上数字和最小的方案数思路:既然要关于副对角线对称,那么可以把关于副对角线对称的方格的值加到一起去,这样就......
  • pod中使用hostpath 持久化日志
    1、kubernetes日志持久化在Kubernetes中,可以使用各种不同的方式来持久化Pod中的日志。以下是一些常见的方法:使用HostPath:如上一个回答所示,可以使用HostPath来将Pod中的日志持久化到宿主机上。这种方法简单易用,但需要注意安全问题。使用本地存储卷:可以使用本地存储......
  • PAT Basic 1095. 解码PAT准考证
    PATBasic1095.解码PAT准考证1.题目描述:PAT准考证号由4部分组成:第1位是级别,即T代表顶级;A代表甲级;B代表乙级;第2~4位是考场编号,范围从101到999;第5~10位是考试日期,格式为年、月、日顺次各占2位;最后11~13位是考生编号,范围从000到999。现给定一系列......
  • re.findall()用法详解-返回string中所有与pattern相匹配的全部字串
    re.findall():函数返回包含所有匹配项的列表。返回string中所有与pattern相匹配的全部字串,返回形式为数组。  示例代码1:【打印所有的匹配项】   importre       s="Longlivethepeople'sRepublicofChina"   ret=re.findall('h',s)       ......
  • 3.2 Go语言从入门到精通:包管理工具之GOPATH
    当我们真正用Go去做项目,或者阅读Go项目(如,Go实现的开源项目)时,不可避免的会遇到包依赖问题,一些包管理方式总是很难区分、选择。Go的包管理与Java的Maven依赖管理不太一样,起初Go的包管理方式经常会被人吐槽,但随之Go版本的升级也出现了不同的包管理方式,以满足不同的需求。今天,我们......
  • Navicat常见错误怎么处理(Rsa Public Key not Find、Generate First a serial、No All
    一:下载一键提取软件提取码:rtce1.Navicat数据库管理工具:NavicatDBeaver数据库管理工具:可以代替Navicat2.NavicatKeygenPatch:激活工具二:安装激活1.安装Navicat:直接下一步即可安装NavicatKeygenPatch:安装好后即可打开使用2.断网、关闭杀毒软件和本地防火墙3.......
  • Node.js文件路径:Path模块
    path模块是nodejs的内置模块,便于我们去获取、操作文件路径记录一些注意事项:文件的绝对位置cjsconsole.log(__filename)mjsmjs中,不能使用__filename和__dirnameconsole.log(import.meta.url)文件所处的目录cjsconsole.log(__dirname)mjsimport{dirname}from"path......
  • Codeforces Round #303 (Div. 2) E. Paths and Trees (最短路+变形最小生成树)
    题目地址:E.PathsandTrees模拟了一场CF,这场实在太水了。。边玩边做的。。最后半分钟交了一发E题。。不幸AK绝杀失败。。。。首先的思路肯定是先求最短路,把可能为最短路的边挑出来,然后第二步我本来写的是直接用无向图的最小生成树,于是绝杀失败。。。后来才发现这样是不行的。......