产品和LCMS之间的差异总和【难度:1级】:

答案1:

using System.Linq;public class Kata {static int gcd(int a, int b) {if(a == 0) return b;if(b == 0) return a;return gcd(b, a % b);}static int lcm(int a, int b) {if(a == 0 || b == 0) return 0;return a * b / gcd(a, b);}public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs) {return pairs.Select(p => p[0] * p[1] - lcm(p[0], p[1])).Sum();}
}​

答案2:

using System.Linq;
public class Kata
{public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){return Enumerable.Range(0, pairs.Length).Select(x => pairs[x][1] == 0 ? 0 : (pairs[x][0] * pairs[x][1]) - GetLCM(pairs[x][0], pairs[x][1])).Sum();}static int gcf(int a, int b){while (b != 0){int temp = b;b = a % b;a = temp;}return a;}static int GetLCM(int a, int b) => (a / gcf(a, b)) * b;
}​

答案3:

using System;
using System.Collections.Generic;public class Kata
{public static int GetResultFromMultiArray(int[] array){int a,b,c,remainder,total;a = array[0];b = array[1];c = a * b;total = a * b;while (b != 0){remainder = a % b;a = b;b = remainder; }if(a == 0 ){return 0;}c = c / a;total = total - c;return total;}public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){List<int> Product = new List<int>();List<int> LCM = new List<int>();for (var i = 0; i < pairs.GetLength(0); i++){var Num = GetResultFromMultiArray(pairs[i]);Product.Add(Num);}int TotalSum = 0;for (var i = 0; i < Product.Count; i++){TotalSum += Product[i];}Console.WriteLine("[{0}] \n The total sum is {1}", string.Join(", ", Product), TotalSum);return TotalSum;}
}​

答案4:

public class Kata
{public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){int multiply = 0;int lcm = 0;int res = 0;foreach (int[] row in pairs){if (row[1] != 0){var i = 0;multiply = row[0] * row[1];do{i++;}while ((i * row[0]) % row[1] != 0);lcm = i * row[0];res += (multiply - lcm);}}return res;}
}​

答案5:

using System;public class Kata
{public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){int[] arrayOfProducts = new int[pairs.GetLength(0)];int[] arrayOfLCM = new int[pairs.GetLength(0)];int result = 0;for (int i = 0; i < pairs.GetLength(0); ++i){arrayOfProducts[i] = pairs[i][0] * pairs[i][1];arrayOfLCM[i] = calculateLCM(pairs[i][0], pairs[i][1]);}for (int i = 0; i < pairs.GetLength(0); ++i){result += arrayOfProducts[i] - arrayOfLCM[i];}return result;}public static int calculateLCM(int x, int y){if (x == 0 || y == 0) return 0;return Math.Abs(x * y) / calculateNOD(x, y);}public static int calculateNOD(int x, int y){while (x != y){if (x > y)x = x - y;elsey = y - x;}return x;}
}​

答案6:

using System;public class Kata {public static int SumDifferencesBetweenProductsAndLCMs( int[][] pairs ) {var sum = 0;foreach ( var pair in pairs ) {long a = pair [ 0 ];long b = pair [ 1 ];var diff = a*b - Lcm( a, b );sum += ( int ) diff;}return sum;}private static long Lcm( long a, long b ) {var gcd = Gcd( a, b );return ( a*b )/Math.Max( gcd, 1 );}private static long Gcd( long a, long b ) {return b == 0 ? a : Gcd( b, a%b );}
}​

答案7:

public class Kata
{public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){int result = 0;for (int i = 0; i < pairs.Length; i++){result += (pairs[i][0] * pairs[i][1]) - LCM(pairs[i][0], pairs[i][1]);}return result;}static int GCD (int a, int b){      if (a == 0)return b;if (b == 0)return a;return GCD(b, a % b);}static int LCM (int a, int b){if (a == 0 || b == 0)return 0;return (a * b) / GCD(a, b);}
}​

答案8:

using System.Linq;public class Kata
{public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){int sum = 0;for (int i=0; i<pairs.Length; i++){int product = (pairs[i][0] * pairs[i][1]);sum += product - (product / GCD(pairs[i][0], pairs[i][1]));}return sum;}public static int GCD(int a, int b){if (a + b == 0)return 1;else if (b == 0)return a;return GCD(b, a%b);//}
}​

答案9:

using System;public class Kata
{public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){int[,] arrLCM = new int[pairs.Length, 1];int[,] arrProd = new int[pairs.Length, 1];for (int i = 0; i < pairs.Length; i++){Console.WriteLine(pairs[i][0]);Console.WriteLine(pairs[i][1]);Console.WriteLine();arrProd[i, 0] = pairs[i][0] * pairs[i][1];}for (int i = 0; i < pairs.Length; i++){int LCM = 0;int start;if (pairs[i][0] <= pairs[i][1])start = pairs[i][0];elsestart = pairs[i][1];if (pairs[i][0] == pairs[i][1])LCM = pairs[i][0];elsefor (int j = start + 1; j <= pairs[i][0] * pairs[i][1]; j++){if (j % pairs[i][0] == 0 &amp;&amp; j % pairs[i][1] == 0){LCM = j;break;}}arrLCM[i, 0] = LCM;}int ret = 0;for (int i = 0; i < arrLCM.Length; i++){ret += arrProd[i, 0] - arrLCM[i, 0];}return ret;}
}​

答案10:

using System.Linq;public class Kata
{public static int calc(int i1, int i2) {int sum1 = 0;for (int i = 1 ; i  <= i1 * i2; i++){if ((i%i1 ==0) &amp;&amp; (i%i2 ==0) ){ sum1 = i;break;}}    return sum1;}public static int SumDifferencesBetweenProductsAndLCMs(int[][] pairs){int i = pairs.Select(t => t[0]*t[1] - calc(t[0],t[1]) ).Sum();return i;}
}​

C#练习题答案: 产品和LCMS之间的差异总和【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战相关推荐

  1. C#练习题答案: 字母战争 - 核打击【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    字母战争 - 核打击[难度:3级]: 答案1: using System; using System.Text.RegularExpressions; using System.Linq; publi ...

  2. C#练习题答案: 寻找恩人【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    寻找恩人[难度:1级]: 答案1: using System; using System.Linq;public class NewAverage {public static long NewAvg ...

  3. C#练习题答案: 反恐精英系列【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    反恐精英系列[难度:1级]: 答案1: namespace CS {using System;using System.Collections.Generic;public class Kata{pr ...

  4. C#练习题答案: 图片#1 - 重建巴别塔【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    图片#1 - 重建巴别塔[难度:1级]: 答案1: using System.Linq;public static class Kata {public static string Babel(int ...

  5. C#练习题答案: TO DE-RY-PO-陆琪暗号【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    TO DE-RY-PO-陆琪暗号[难度:1级]: 答案1: using System.Linq;public class Kata{public static string Encode(string ...

  6. C#练习题答案: 英雄的根【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    英雄的根[难度:1级]: 答案1: using System;public class IntSqRoot {const int error = 1;public static long IntRac ...

  7. C#练习题答案: scytale的编码器/解码器(古斯巴达密码)【难度:3级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    scytale的编码器/解码器(古斯巴达密码)[难度:3级]: 答案1: using System; using System.Linq; using System.Text.RegularExpre ...

  8. C#练习题答案: 巴路士惠勒改造【难度:4级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    巴路士惠勒改造[难度:4级]: 答案1: using System; using System.Collections.Generic; using System.Linq;public class ...

  9. C#练习题答案: 折叠用自己的方式去月球【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

    折叠用自己的方式去月球[难度:1级]: 答案1: using System; using System.Collections.Generic;public class Kata {public st ...

最新文章

  1. python中列表常用方法_Python中列表的常用方法
  2. R语言缺失值替换:缺失的值(NA)替换每个分组最近的非缺失值
  3. JQuery对XML文件的操作
  4. 一个民工的数字化生活
  5. linux系统内核从3.2.0-100-generic升级到3.13版本
  6. 学计算机二级分数,2020年9月计算机二级考试成绩可以查了 多少分及格
  7. 【渝粤题库】广东开放大学民法 形成性考核
  8. 工作86:防抖和节流的问题
  9. android 按钮带图标 阴影_android中带图标的按钮(ImageButton)怎么用
  10. 【git】git提交忽略不必要的文件或文件夹
  11. bzoj 5029 poj 2528 nyoj 1009: 贴小广告(线段树)
  12. 一口气说出8种幂等性解决重复提交的方案,面试官懵了!(附代码)
  13. AfxGetMainWnd 函数
  14. 一文带你浏览Graph Transformers
  15. CAD 卸载工具,完美彻底卸载清除干净cad各种残留注册表和文件【转载】
  16. c++把字符串逆序输出
  17. linux无线网卡信道,linux如何列出网卡支持的wifi信道?
  18. 如何批量下载央视CNTV的节目视频
  19. 【笃行】Button的选中与改变
  20. 第三章 Elasticsearch Query DSL -- 查询

热门文章

  1. 云队友丨如何“优雅”地进行职场沟通?
  2. 构建之法10,11,12章的读后感
  3. 一个执行计划异常变更的案例 - 外传之SQL Profile(上)
  4. 画一个秘密花园 | Scratch 3.0 艺术项目
  5. .m3u8视频文件的初步探索
  6. 解锁三星bl锁有几种方法_手机ROOT之前的必备工作三星Bootloader解锁教程
  7. 音响系统相关技术术语解释大全
  8. woocommerce对接paypal如何进行沙盒测试?
  9. 0160 十分钟看懂时序数据库(I)-存储
  10. 想要学好大数据需掌握这十二大技术!