C语言一般解法:
key:1.使用stoi函数一键字符串转数字。
2.因式分解求出最大因子数,并且判断是否素数。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int i;
getchar();
for(i=0;i<n;i++)
{
int j,k=0,temp,temp2,max,f=0;
char a[1000],t[1000];
gets(a);
for(j=0;j<strlen(a);j++)
{
if(a[j]>='0'&&a[j]<='9') t[k++]=a[j];
}
t[k]='\0';
k=0;
temp=atoi(t);
temp2=temp;
for(j=2;j<=sqrt(temp);j++)
{
while (temp%j==0)
{
max=j;
temp/=j;
f=1;
}
}
if(temp>1) max=temp;
if(f==0) printf("%d\n",temp2);
else printf("%d\n",max);
}
}
return 0;
}
京公网安备 11010502036488号