如何用c#做下面的题目

WYT1991922 2011-03-11 03:32:00
编写一个程序,求出200到300之间的数,且满足条件:它们三个数字之积为42,三个数字之和为12。
...全文
436 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
aswillll 2011-03-14
  • 打赏
  • 举报
回复
纯进来看看,有没有 更有优化的遍历啊?
Daqing 2011-03-14
  • 打赏
  • 举报
回复
public static void Cacul(int ifrom,int ito)
{

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);
hhc123 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 dfhdhq 的回复:]
引用 13 楼 pittroll 的回复:
C# code

for (int i = 201; i < 300; i++)
{
int baiwei = i / 100;//百位
int shiwei = (i % 100) / 10;//十位
int gewei = (i % 100) ……


正解。
[/Quote]
算得这么累做什么呢,
200-300不用看,肯定全是三位数,直接用取字就行了.
hhc123 2011-03-13
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 dfhdhq 的回复:]
引用 13 楼 pittroll 的回复:
C# code

for (int i = 201; i < 300; i++)
{
int baiwei = i / 100;//百位
int shiwei = (i % 100) / 10;//十位
int gewei = (i % 100) ……


正解。
[/Quote]
算得这么累做什么呢,
200-300不用看,肯定全是三位数,直接用取字就行了.
feitianhu112 2011-03-12
  • 打赏
  • 举报
回复
同意13楼
weakey 2011-03-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 pittroll 的回复:]
C# code

for (int i = 201; i < 300; i++)
{
int baiwei = i / 100;//百位
int shiwei = (i % 100) / 10;//十位
int gewei = (i % 100) ……
[/Quote]

正解。
下一站 2011-03-12
  • 打赏
  • 举报
回复
我用Java写的
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);
}

}

}

}
flylong0204 2011-03-12
  • 打赏
  • 举报
回复
同意29楼的
sugarforever 2011-03-12
  • 打赏
  • 举报
回复
遍历,然后分别取出个十百位求和与积。
冰岛男孩 2011-03-12
  • 打赏
  • 举报
回复
int i = 2;
for(int j=1;j<10;j++)

if ( j * (10-j) == 21)
{
Console.WriteLine(i * 100 + j * 10 + (10-j));
}
}
脾气不坏 2011-03-12
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 aqinshou 的回复:]

神马?!
首先排除200和300
所以百位必为2
将21分解质因数,只有3*7(排除21*1)
答案就只有2个,237,273
汗啊,这还需要穷举遍历神马的吗?
神马都是浮云?!
[/Quote]

如果42 和12不会变 的确不用遍历
Strive超 2011-03-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();
果粉叔叔 2011-03-12
  • 打赏
  • 举报
回复
神马?!
首先排除200和300
所以百位必为2
将21分解质因数,只有3*7(排除21*1)
答案就只有2个,237,273
汗啊,这还需要穷举遍历神马的吗?
神马都是浮云?!
Hamber_Bao 2011-03-12
  • 打赏
  • 举报
回复
思路:
一、利用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);
}
}


祝楼主学业有成
=PNZ=BeijingL 2011-03-12
  • 打赏
  • 举报
回复


num=201;
int num1 = num / 100;//获取百位上的数
int num2 = (num % 100) / 10;//获取十位上的数
int num3 = (num % 100) % 10;//获取个位上的数

amoy_yang 2011-03-12
  • 打赏
  • 举报
回复
先定义一个数I给个初始值200 循环条件I<300 I++

用 IF 判断 把循环得到的数放到一个数组里面在算
代码就不写了 自己理解
hch126163 2011-03-11
  • 打赏
  • 举报
回复
int i = 2;
for(int j=1;j<10;j++)

if ( j * (10-j) == 21)
{
Console.WriteLine(i * 100 + j * 10 + (10-j));
}
}
hch126163 2011-03-11
  • 打赏
  • 举报
回复
int i = 2;
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);
}
}
Daqing 2011-03-11
  • 打赏
  • 举报
回复
[code=C#][/ 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);
}
}
Console.ReadKey();]
Daqing 2011-03-11
  • 打赏
  • 举报
回复
个位欠妥,但是实在找不到好的方法了!
加载更多回复(15)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧