int i = 2; for(int j=0;j<10;j++) for (int k = 0; k < 10; k++) { if (j + k == 10 && j * k == 21) { Console.WriteLine(i * 100 + j * 10 + k); } }
for (int i = 201; i < 300; i++) { int baiwei = i / 100;//百位 int shiwei = (i % 100) / 10;//十位 int gewei = (i % 100) % 10;//个位 if (baiwei * shiwei * gewei == 42 && (baiwei + shiwei + gewei == 12)) { Console.WriteLine(i); } }测试了一下,有:237和273,嘿嘿。
贴个Linq Func<int, int> calcMul = n => { var ret = 1; n.ToString().ToList().ForEach(c => { ret *= int.Parse(c.ToString()); }); return ret; }; var list = from n in Enumerable.Range(200, 100) where n.ToString().Sum(c=>int.Parse(c.ToString())) == 12 && calcMul(n) == 42 select n; foreach (var n in list) { Console.WriteLine("Num:{0}", n); }
应该用聚合函数的,没必要再搞个Func委托,重写一下: var list = from n in Enumerable.Range(200, 100) where n.ToString().Sum(c=>int.Parse(c.ToString())) == 12 && n.ToString().Aggregate(1, (pre, next) => pre * int.Parse(next.ToString())) == 42 select n;
System.out.print("满足条件的有:\n"); for (int n=200; n <=300; n++) { int a = n / 100;//百位 int b = (n % 100) / 10;//十位 int c = (n % 100) % 10;//个位 if (a * b * c == 42 && (a + b + c == 12)) { System.out.println(n); }
}
}}
先定义一个数I给个初始值200 循环条件I<300 I++ 用 IF 判断 把循环得到的数放到一个数组里面在算 代码就不写了 自己理解
num=201; int num1 = num / 100;//获取百位上的数 int num2 = (num % 100) / 10;//获取十位上的数 int num3 = (num % 100) % 10;//获取个位上的数
思路: 一、利用for循环遍历200-300之间的数 二、将循环遍历得到的每一个数的个位、十位、百位分解开来 三、if语句判断 代码如下//利用for循环遍历for(int i = 200;i <= 300;i ++) { //将得到的每一个数分解开来 int gewei = i % 10; //个位数 int shiwei = i / 10 % 10; //百位数 int baiwei = i / 100; //将两个条件声明为bool bool con1 = (gewei * shiwei * baiwei == 42); bool con2 = (gewei + shiwei + baiwei == 12);
List <int> count=new List <int>(); for (int i = 200; i <= 300; i++) { int b = i / 100; int s = (i - b * 100) / 10; int g = i % 100; int sum = b + s + g; int arr = b * s * g; if (sum == 12 && arr == 42) { count .Add (i); } } for (int i = 0; i < count.Count; i++) { Console.WriteLine(count[i].ToString ()); } Console.ReadKey();
int i = 2;
for(int j=0;j<10;j++)
for (int k = 0; k < 10; k++)
{
if (j + k == 10 && j * k == 21)
{
Console.WriteLine(i * 100 + j * 10 + k);
}
}
for (int i = 201; i < 300; i++)
{
int baiwei = i / 100;//百位
int shiwei = (i % 100) / 10;//十位
int gewei = (i % 100) % 10;//个位
if (baiwei * shiwei * gewei == 42 && (baiwei + shiwei + gewei == 12))
{
Console.WriteLine(i);
}
}测试了一下,有:237和273,嘿嘿。
{
var ret = 1;
n.ToString().ToList().ForEach(c =>
{
ret *= int.Parse(c.ToString());
});
return ret;
}; var list = from n in Enumerable.Range(200, 100)
where n.ToString().Sum(c=>int.Parse(c.ToString())) == 12
&& calcMul(n) == 42
select n; foreach (var n in list)
{
Console.WriteLine("Num:{0}", n);
}
var list = from n in Enumerable.Range(200, 100)
where n.ToString().Sum(c=>int.Parse(c.ToString())) == 12
&& n.ToString().Aggregate(1, (pre, next) => pre * int.Parse(next.ToString())) == 42
select n;
{
for(int i=ifrom;i<ito+1;i++)
{
int count = Convert.ToInt32(i / Math.Pow(10, ifrom.ToString().Length-1));//获取个位
int culm = Convert.ToInt32(i / Math.Pow(10, ifrom.ToString().Length-1));//获取个位
for (int j = 1; j < ifrom.ToString().Length; j++)
{
count += Convert.ToInt32(i % Math.Pow(10, j) / Math.Pow(10, (j-1)));//获取十位、百位、千位
culm = culm * Convert.ToInt32(i % Math.Pow(10, j) / Math.Pow(10, (j - 1)));
}
if (count == 12 && culm == 42)
{
Console.WriteLine(i);
}
else
{
count = 0;//和
culm = 1;//积
} }
Console.ReadKey();
}
{
int count = Convert.ToInt32(i / Math.Pow(10, ifrom.ToString().Length-1));//获取个位
int culm = Convert.ToInt32(i / Math.Pow(10, ifrom.ToString().Length-1));//获取个位
for (int j = 1; j < ifrom.ToString().Length; j++)
{
count += Convert.ToInt32(i % Math.Pow(10, j) / Math.Pow(10, (j-1)));//获取十位、百位、千位
culm = culm * Convert.ToInt32(i % Math.Pow(10, j) / Math.Pow(10, (j - 1)));
}
if (count == 12 && culm == 42)
{
Console.WriteLine(i);
}
}
Console.ReadKey();]
for(int j=1;j<10;j++)
for (int k = 10-j; k < 10; k++)
{
if (j + k == 10 && j * k == 21)
{
Console.WriteLine(i * 100 + j * 10 + k);
}
}
for(int j=1;j<10;j++)
if ( j * (10-j) == 21)
{
Console.WriteLine(i * 100 + j * 10 + (10-j));
}
}
for(int j=1;j<10;j++)
if ( j * (10-j) == 21)
{
Console.WriteLine(i * 100 + j * 10 + (10-j));
}
}
package com;public class Demo {
public static void main(String args []){
System.out.print("满足条件的有:\n");
for (int n=200; n <=300; n++)
{
int a = n / 100;//百位
int b = (n % 100) / 10;//十位
int c = (n % 100) % 10;//个位
if (a * b * c == 42 && (a + b + c == 12))
{
System.out.println(n);
}
}
}}
代码就不写了 自己理解
int num1 = num / 100;//获取百位上的数
int num2 = (num % 100) / 10;//获取十位上的数
int num3 = (num % 100) % 10;//获取个位上的数
一、利用for循环遍历200-300之间的数
二、将循环遍历得到的每一个数的个位、十位、百位分解开来
三、if语句判断
代码如下//利用for循环遍历for(int i = 200;i <= 300;i ++)
{
//将得到的每一个数分解开来
int gewei = i % 10; //个位数
int shiwei = i / 10 % 10; //百位数
int baiwei = i / 100; //将两个条件声明为bool
bool con1 = (gewei * shiwei * baiwei == 42);
bool con2 = (gewei + shiwei + baiwei == 12);
//if语句进行判断
if(con1 && con2)
{
Console.WriteLine(i);
}
}
祝楼主学业有成
List <int> count=new List <int>();
for (int i = 200; i <= 300; i++)
{
int b = i / 100;
int s = (i - b * 100) / 10;
int g = i % 100;
int sum = b + s + g;
int arr = b * s * g;
if (sum == 12 && arr == 42)
{
count .Add (i);
}
}
for (int i = 0; i < count.Count; i++)
{
Console.WriteLine(count[i].ToString ());
}
Console.ReadKey();
{
for(int i=ifrom;i<ito+1;i++)
{
int count = 0;
int culm = 1;
for (int j = ifrom.ToString().Length; j >0 ; j--)
{
count += Convert.ToInt32(i % Math.Pow(10, j) / Math.Pow(10, (j-1)));//获取十位、百位、千位
culm = culm * Convert.ToInt32(i % Math.Pow(10, j) / Math.Pow(10, (j - 1)));
}
if (count == 12 && culm == 42)
{
Console.WriteLine(i);
} }
Console.ReadKey();
}
//完解
//调用Cacul(100,10000);