using System;using System.Linq;using System.Collections.Generic;publicclassPrimeDecomp{publicstaticStringfactors(int lst){var del =newList<int>();var i =2;while(lst !=1){if(lst % i ==0){
del.Add(i);
lst = lst / i;}else{
i++;}}returnstring.Join("", del.Select(x =>"("+(del.Count(y => y == x)>1? x +"**"+ del.Count(y => y == x): x.ToString())+")").Distinct());}}
答案4:
using System;publicclassPrimeDecomp{publicstaticStringfactors(int lst){string result = String.Empty;int c=0;for(int i=2; i<=lst && lst>1; i++,c=0){for(c=0; lst >1&& lst % i ==0; c++) lst/=i;
result +=(c >0)?( c==1?"("+ i.ToString()+")":"("+ i.ToString()+"**"+ c.ToString()+")"): String.Empty;}return result;}}
using System;using System.Collections.Generic;using System.Text;publicclassPrimeDecomp{publicstaticStringfactors(int lst){var collection =newDictionary<int,int>();foreach(var factor inFindFactors(lst)){if(!collection.ContainsKey(factor))
collection[factor]=1;else
collection[factor]++;}var stringBuilder =newStringBuilder();foreach(var item in collection)
stringBuilder.Append(item.Value >1? $"({item.Key}**{item.Value})": $"({item.Key})");return stringBuilder.ToString();}publicstatic IEnumerable<int>FindFactors(int num){var result =newList<int>();// Take out the 2s.while(num %2==0){
result.Add(2);
num /=2;}// Take out other primes.var factor =3;while(factor * factor <= num){if(num % factor ==0){// This is a factor.
result.Add(factor);
num /= factor;}else{// Go to the next odd number.
factor +=2;}}// If num is not 1, then whatever is left is prime.if(num >1)
result.Add(num);return result;}}
答案7:
using System;using System.Text;publicclassPrimeDecomp{publicstaticStringfactors(int num){StringBuilder res =newStringBuilder();for(int b =2; num >1; b++)if(num % b ==0){int x =0;while(num % b ==0){
num /= b;
x++;}
res.AppendFormat((x ==1)?"({0})":"({0}**{1})", b, x);}return res.ToString();}}
答案8:
using System.Collections.Generic;using System.Linq;using System;publicclassPrimeDecomp{publicstatic IEnumerable<int>EnumerPrimes(){for(var n =2; n <1000000; n++){var isPrime =true;for(var p =2; p*p <= n; p++){if(n%p ==0){
isPrime =false;break;}}if(isPrime){yieldreturn n;}}}privatestatic IEnumerable<int>GetPrimes(int lst){foreach(var p inEnumerPrimes()){while(lst % p ==0){
lst = lst / p;yieldreturn p;}if(lst == p){yieldbreak;}}}publicstaticstringfactors(int lst){var seq =GetPrimes(lst).GroupBy(i => i).Select(g => g.Count()>1? $"({g.Key}**{g.Count()})": $"({g.Key})");returnstring.Concat(seq);}}
答案9:
using System;using System.Collections.Generic;using System.Linq;using System.Text;publicclassPrimeDecomp{static List<int>GetPrimes(int n){bool[] isPrime = Enumerable.Repeat(true, n +1).ToArray();int sqrtOfN =(int)Math.Sqrt(n);for(int i =2; i < sqrtOfN; i++)if(isPrime[i])for(int j = i * i; j <= n; j += i)if(isPrime[j])
isPrime[j]=false;
List<int> primes =newList<int>();for(int i =2; i <= n; i++)if(isPrime[i])
primes.Add(i);return primes;}publicstaticstringfactors(int lst){int sqrtOfLst =(int)Math.Sqrt(lst);
List<int> primes =GetPrimes(sqrtOfLst);
Dictionary<int,int> countByPrime =newDictionary<int,int>();foreach(int prime in primes)if(lst % prime ==0){
countByPrime.Add(prime,1);
lst /= prime;while(lst % prime ==0){
countByPrime[prime]++;
lst /= prime;}}StringBuilder result =newStringBuilder();foreach(int prime in countByPrime.Keys)if(countByPrime[prime]==1)
result.Append("("+ prime +")");else
result.Append("("+ prime +"**"+ countByPrime[prime]+")");if(lst !=1)
result.Append("("+ lst +")");return result.ToString();}}
答案10:
publicclassPrimeDecomp{publicstaticstringfactors(int lst){int[] primes ={2,3,5,7,11,13,17,19,23,29,31,37,41};int[] counts ={0,0,0,0,0,0,0,0,0,0,0,0,0};int i =0;string result ="";string final ="";while(i < primes.Length){if(lst%primes[i]==0){
counts[i]++;
lst = lst/primes[i];}else{
i++;}}for(int j =0; j < counts.Length; j++){switch(counts[j]){case0:continue;case1:
result +="("+ primes[j]+")";break;default:
result +="("+ primes[j]+"**"+ counts[j]+")";break;}}int start =43;while(lst >1){if(lst%start ==0){
final +="("+ start +")";
lst = lst/start;continue;}
start +=2;}return result+final;}}
在数字素数【难度:3级】:答案1:using System;using System.Collections.Generic;public class PrimeDecomp { public static String factors(int lst) { var primes = new List<string>(); for (var number ...