C#練習問題解答:最もよく見られない多重【難易度:3級】--景越C#クラシックプログラミング問題ライブラリ、1000本のC#基礎練習問題などに挑戦してください
37564 ワード
最も珍しい多重【難易度:3級】:
答え1:
答え2:
答え3:
答え4:
答え5:
答え6:
答え7:
答え8:
答え9:
答え10:
答え1:
using System.Linq;
using System.Collections.Generic;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if (nums.Count == 0) return 1;
if (nums.Contains(0)) return 0;
var sum = nums.Max();
while (nums.Count(i => sum % i == 0) != nums.Count)
sum += nums.Max();
return sum;
}
}
答え2:
using System.Linq;
using System.Collections.Generic;
public static class Kata
{
private static int Gcf(int a, int b) => b == 0 ? a : Gcf(b, a%b);
public static int Lcm(IEnumerable<int> nums) => nums.Any() ? nums.Aggregate((g, x) => g*x/Gcf(g,x)) : 1;
}
答え3:
using System;
using System.Collections.Generic;
using System.Linq;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if (nums.Count == 0) return 1;
if (nums.Contains(0)) return 0;
int highestNumber = nums.Max();
int tmpSum = highestNumber;
while (nums.Count(i => tmpSum % i == 0) != nums.Count)
{
tmpSum += highestNumber;
}
return tmpSum;
}
}
答え4:
using System;
using System.Collections.Generic;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if(nums.Count == 1) return nums[0];
int sayi=0;
for(int i=1 ; ;i++){
for(int j=0; j<nums.Count; j++){
if(nums[j]==0) return 0;
if(i % nums[j] == 0)
sayi++;
}
if( sayi== nums.Count) return i;
else sayi=0;
}
return 1;
}
}
答え5:
using System;
using System.Linq;
using System.Collections.Generic;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if (nums.Count == 0)
return 1;
if (nums.Contains(0))
return 0;
int sum = 0, max;
max = nums.Max();
for(;;){
sum += max;
bool fl = true;
for(int j = 0; j < nums.Count; j++){
if(sum % nums[j] == 0)
continue;
fl = false;
break;
}
if(fl)
return sum;
}
}
}
答え6:
using System;
using System.Linq;
using System.Collections.Generic;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if (nums.Count == 0)
return 1;
if (nums.Contains(0))
return 0;
int sum = nums.Max();
while (nums.Count(i => sum % i == 0) != nums.Count)
sum += nums.Max();
return sum;
}
}
答え7:
using System;
using System.Collections.Generic;
using System.Linq;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if(nums.Count() == 0)
{
return 1;
}
int highest = 1;
foreach(int num in nums)
{
highest *= num;
}
for(int index = nums.Max(); index <= highest; index ++)
{
if(0 == nums.Where(x => x != 0).ToList().Where(x => index % x != 0 && x > 0).Count())
{
return index;
}
}
return 0;
}
}
答え8:
using System;
using System.Collections.Generic;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if (nums.Count == 0)
return 1;
int lcm = nums[0];
for (int i = 1; i < nums.Count; ++i)
{
lcm = LocalLcm(lcm, nums[i]);
}
return lcm;
}
private static int LocalLcm(int a, int b)
{
int num1, num2;
if (a > b)
{
num1 = a;
num2 = b;
}
else
{
num1 = b;
num2 = a;
}
for (int i = 1; i < num2; ++i)
{
if ((num1 * i) % num2 == 0)
{
return num1*i;
}
}
return num1 * num2;
}
}
答え9:
using System;
using System.Collections.Generic;
using System.Linq;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if(nums == null || !nums.Any()){
return 1;
}
if(nums.Any(x => x == 0))
return 0;
var max = nums.Max();
while(nums.Any(x => max % x != 0)){
max++;
}
return max;
}
}
答え10:
using System;
using System.Collections.Generic;
using System.Linq;
public static class Kata
{
public static int Lcm(List<int> nums)
{
if (nums.Count == 0) return 1;
if (nums.Contains(0)) return 0;
var sum = nums.Max();
while (!nums.All(i => sum % i == 0))
sum += nums.Max();
return sum;
}
}