题解 | #大整数的因子#
大整数的因子
https://www.nowcoder.com/practice/3d6cee12fbf54ea99bb165cbaba5823d
//除法的原理,KY26和KY30都用到了,如果上述两题会,这道题还是比较简单的 #include "stdio.h" #include "string" using namespace std; char num[31];string str; string divsionJudge(){ str = num;int k = 2;//为除数 int length = str.size(); char shadow[31];//作为num[31]的影子寄存器 int reminder = 0;str = "";int t;//remainder为余数,str记录能整除的k bool flag = false;//记录有没有能整除的数 while (k<=9){ for (int i = 0; i < length; ++i) {//每次整除判断时,都对影子寄存器初始化 shadow[i] = num[i]; } reminder = 0;int t; for (int i = 0; i < length; ++i) { t = (reminder*10 + shadow[i] - '0')%k; shadow[i] = (reminder*10 + shadow[i] - '0')/k + '0'; reminder = t; } if (reminder == 0){ flag = true; str += char (k + '0'); } ++k; } if (flag) return str; else return "none"; } void Init(){//对num和str初始化 for (int i = 0; i < 31; ++i) { num[i]='\0'; } str.clear(); } int main(){ while (scanf("%s",num)!=EOF){ if (num[0] == '-') break; if (num[0] == '1' && num[1] == '\0'){ printf("none\n"); } else if (num[0] == '0' && num[1] == '\0'){ printf("2 3 4 5 6 7 8 9\n"); } else{ string result = divsionJudge(); if (result != "none"){ for (int i = 0; i < result.size()-1; ++i) { printf("%c ",result[i]); } printf("%c\n",result[result.size()-1]); Init(); } else{ printf("%s\n",result.c_str()); Init(); } } } }