C# by Dissection 第三章(语句) 练习题(请教)
13.编写一程序,要求首先询问"how many in a row should I wait for",然后运行直到某一个数字在一行中连续出现指定的次数为止(例如:在显示"how many in a row should I wait for"后,用户输入"2".那么如果输入"12345677",程序终止)
14.编写一程序,用来生成最短边长不大于n的所有毕达哥拉斯三元数组.假设n<200.(当有三个数a,b,c,满足 a*a+b*b=c*c 时,由这三个数组成的三元数组就是毕达哥拉斯三元数组)
以上两题,我做不出来了.希望有高手帮忙解答.
随着对该书学习的深入,还会有其他的未解习题向大家请教
问题点数:40、回复次数:7Top
1 楼Knight94(愚翁)回复于 2006-03-27 09:14:08 得分 5
console下的?
to 13(没怎么看明白)
Console.WriteLine ("how many in a row should I wait for");
string strLine;
while (true)
{
strLine = Console.ReadLine ();
if( strLine == "2" )
//Do what you want here
else if( strLine == "12345677" )
break;
}
return 0;
//我现在用的是字符串,你也可以转化成整型在处理
to 14
private bool IsBedgasla( int a, int b, int c )
{
return ( (a*a + b*b) == c*c) ? true:false;
}
// in your main func
Console.WriteLine ("Input a,b,c");
string strLine;
int a, b,c;
strLine = Console.ReadLine ();
a = Convert.ToInt32( strLine, 10 );
strLine = Console.ReadLine ();
b = Convert.ToInt32( strLine, 10 );
strLine = Console.ReadLine ();
c = Convert.ToInt32( strLine, 10 );
if( IsBedgasla( a,b,c ) )
Console.WriteLine( "Is Bedgasla" );
else
Console.WriteLine( "Is not Bedgasla" );
return 0;
Top
2 楼terry1021_82(小狐狸)回复于 2006-03-27 19:50:41 得分 0
不好意思啊
13题,你可能没有理解对(也可能是我表述不够清楚)
13题,题目的意思是:先键入允许连续重复的数字的次数,然后再换一行输入数字串,并对数字串进行校检.(比如:当询问"how many in a row should I wait for"后键入"3",那么在接下来输入字符串的时候,如果发生了连续按3下"7"的情况,那么程序就会自动退出.)
Top
3 楼Macosx(结贴)回复于 2006-03-27 20:29:50 得分 35
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("how many in a row should I wait for");
int max=Convert.ToInt32(Console.ReadLine());
StringBuilder line=new StringBuilder();
ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey(true);
char ch = cki.KeyChar.ToString()[0];
if (!Char.IsDigit(ch))
continue;
line.Append(ch);
Console.Write(ch);
int count = 0;
int idx = 0;
while ((idx = line.ToString().IndexOf(ch, idx+1)) > -1)
{
count++;
}
if (count == max)
return;
}
while (cki.Key != ConsoleKey.Escape);
}
}
}
加分Top
4 楼Macosx(结贴)回复于 2006-03-27 20:33:42 得分 0
14题有问题吧 是不是应该是最长边长不大于nTop
5 楼terry1021_82(小狐狸)回复于 2006-03-28 19:57:11 得分 0
书上说是最短边长Top
6 楼terry1021_82(小狐狸)回复于 2006-03-28 19:58:32 得分 0
这本书自称是为大一新生准备的
怎么做起后面的习题,感觉总有个别题目不太像呢Top
7 楼terry1021_82(小狐狸)回复于 2006-03-28 21:31:40 得分 0
13题,Macosx()正解!
14题,忘记打书上的提示了:用两个for来枚举最短边长可能的值,然后进行测试,判断结果值是否为一个整数的平方
这本书光有习题,没有答案.感觉难缠Top




