您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > C#求常见数学题目实例
C#求常见数学题目实例★求除以7余5,除以5余2,除以3余1的所有三位数之和程序代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceRemainerMaths{classProgram{staticvoidMain(string[]args){//求除以7余5,除以5余2,除以3余1的所有三位数之和Console.WriteLine(除以7余5,除以5余2,除以3余1的所有三位数如下:);intfrom=100;intto=1000;intsum=0;for(inti=from;ito;i++){if(i%7==5&&i%5==2&&i%3==1){Console.WriteLine(i);sum+=i;}}Console.WriteLine(除以7余5,除以5余2,除以3余1的所有三位数之和为:{0},sum);Console.ReadLine();}}}★答案:★1-9九个数字不重复组成一个三位数加法算式,求出所有组合:程序代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceRemainerMaths{classProgram{staticvoidMain(string[]args){intcount=0;//可能性个数int[]a={1,2,3,4,5,6,7,8,9};for(inti=0;ia.Length;i++){for(intj=0;ja.Length;j++){for(intk=0;ka.Length;k++){if(a[i]!=a[j]&&a[j]!=a[k]&&a[k]!=a[i]){//从9个数中随机找出3个数组成三位数intadd1=100*a[i]+10*a[j]+a[k];//将剩下的6个数字组成一个数组int[]b=GetNewArray(a,i,j,k);for(intx=0;xb.Length;x++){for(inty=0;yb.Length;y++){for(intz=0;zb.Length;z++){if(b[x]!=b[y]&&b[y]!=b[z]&&b[z]!=b[x]){//从这6个数中随机找出3个数组成三位数intadd2=100*b[x]+10*b[y]+b[z];//将剩下的3个数字组成一个数组int[]c=GetNewArray(b,x,y,z);//获得最后剩下的3个数字组成的所有三位数int[]lastNumber=GetAllThreeNumber(c);//如果两数之和等于第三个数就输出for(intindex=0;indexlastNumber.Length;index++){if(add1+add2==lastNumber[index]){count++;Console.WriteLine({0}+{1}={2},add1,add2,lastNumber[index]);break;}}}}}}}}}}Console.WriteLine(共有[{0}]种情形-因加法存在交换律,则实际有[{1}]种情形。,count,count/2);Console.ReadLine();}///summary///由原数组,去除掉索引在i、j、k处的元素后形成的新数组////summary///paramname=a/param///paramname=i/param///paramname=j/param///paramname=k/param///returns/returnsstaticint[]GetNewArray(int[]a,inti,intj,intk){Listintlist=newListint();for(inttemp=0;tempa.Length;temp++){if(temp!=i&&temp!=j&&temp!=k){list.Add(a[temp]);}}int[]b=list.ToArray();returnb;}///summary///获得由最后三个数字组成的所有三位数////summary///paramname=list/param///returns/returnsstaticint[]GetAllThreeNumber(int[]list){Listinttemp=newListint();if(list==null||list.Length!=3)returntemp.ToArray();temp.Add(100*list[0]+10*list[1]+list[2]);temp.Add(100*list[0]+10*list[2]+list[1]);temp.Add(100*list[1]+10*list[0]+list[2]);temp.Add(100*list[1]+10*list[2]+list[0]);temp.Add(100*list[2]+10*list[0]+list[1]);temp.Add(100*list[2]+10*list[1]+list[0]);returntemp.ToArray();}}}★运行后截图:
本文标题:C#求常见数学题目实例
链接地址:https://www.777doc.com/doc-4699970 .html