周六周日老师给我们出了10道题,还剩一道没有做出来,请大家指点一下,记住要用C#写奥~!谁有想法都可以写上,看看谁的方法最简单还能看明白,可以写点注释!
试题一
创建一个控制台应用程序, 接受用户分别输入的1个整数,该整数范围是1-20,
当X= 1,打印 1
当X= 2,打印 1,1
当X= 3,打印 1,1,2
当X= 4,打印 1,1,2,3
当X= 5,打印 1,1,2,3,5
当X= 6,打印 1,1,2,3,5,8
当X= 7,打印 1,1,2,3,5,8,13
以此类推
试题一
创建一个控制台应用程序, 接受用户分别输入的1个整数,该整数范围是1-20,
当X= 1,打印 1
当X= 2,打印 1,1
当X= 3,打印 1,1,2
当X= 4,打印 1,1,2,3
当X= 5,打印 1,1,2,3,5
当X= 6,打印 1,1,2,3,5,8
当X= 7,打印 1,1,2,3,5,8,13
以此类推
解决方案 »
- C#winform datagridview的问题
- datagridview中的button列,如何设置button的标题与图标
- 谁有压缩和解压缩的代码
- 我想问个类的问题!本人菜鸟一直不要扔东西给我谢谢
- 如何用C#的ArrayList存储一个三维矩阵中的每个元素??? <详细请进>
- 想学net编写Form程序,请问微软提没提供现成的例子?
- 数组转换成object,然后怎么再转换成数组,急!!!!
- 异步回调发送变长消息
- ADO.NET简单问题
- !!急急急急!!急急急!!socket文件传输莫名其妙问题,高分求救(100分)高手一句话能搞定!!!我认为可能是个小问题!!!!
- 咨询一个问题:b/S是不是ASP项目结构,谢谢?
- 我对 “int?” 的理解?
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num;
string str = "";
Console.WriteLine ("请输入一个1-20内的数字");
num = Console.Read();
for (int i = 1; i <= num - 48; i++)// Console.Read()读取的是字符,字符以int型(32位整数)返回变量.0为48
{
str += i.ToString() + ",";
}
str = str.Substring(0,str.Length-1);
Console.WriteLine(str );
}
}
}
怎么能不对呢,我在我家的电脑上试过VS2005
file-new-project-ConsoleApplication2把下面代码粘过去就好用.
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num;
string str = "";
Console.WriteLine("请输入一个1-20内的数字");
num = Console.Read();
for (int i = 1; i <= num - 48; i++)// Console.Read()读取的是字符,字符以int型(32位整数)返回变量.0为48
{
str += i.ToString() + ",";
}
str = str.Substring(0, str.Length - 1);
Console.WriteLine(str);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;namespace 正则Regex
{
class Program
{
static int[] j;
static void Main(string[] args)
{
//Console.WriteLine(Regex.IsMatch("00","0"));
for(int i=1;i<8;i++)
{
Print(i);
PrintInt();
Console.WriteLine();
}
Console.Read();
} private static void PrintInt()
{
//throw new Exception("The method or operation is not implemented.");
foreach (int m in j)
{
Console.Write(m);
Console.Write(" ");
}
} static void Print(int i)
{
if (i<1||i>20)
{
throw new Exception("input error!");
}
j = new int[i];
j[0] = 1;
if (i < 2)
return;
j[1] = 1; if (i < 3)
return;
for (int k = 2; k < i; k++)
{
j[k] = j[k - 1] + j[k - 2];
}
}
}
}
using System.Collections;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ int num;
int num2;
bool isDec=false;
int [] str =new int[20];
Console.WriteLine ("请输入一个1-20内的数字");
num = Console.Read();
num2 = Console.Read(); if (num2>=48&&num<=57)//num2在48到57之间说明是2位数
{
isDec=true;
}
num=num-48;// Console.Read()读取的是字符,字符以int型(32位整数)返回变量.0为48
num2=num2-48;
//是9以上的的数时
if (isDec)
{
//判断num2是否是0-9之间的数
if (num2>9||num2<0)
{
Console.Write("错误:不是1-20内的数字!!");
return;
} num=(num*10)+num2;
//大于20的数字则错误
if (num>20)
{
Console.Write("错误:不是1-20内的数字!!");
return;
}
else
{
str[0]=1;
str[1]=1;
for (int i=2;i<num;i++)
{
str[i]=str[i-1]+str[i-2];
}
for (int i=0;i<num;i++)
{
if (i==(num-1))
{
Console.Write(str[i]);
}
else
{
Console.Write(str[i]+",");
}
}
}
}
//只有个位时
else
{
//小于0的数字则错误
if (num<=0||num>9)
{
Console.Write("错误:不是1-20内的数字");
return;
}
else
{ if(num==1)
{
str[0]=1;
Console.WriteLine(str[0]);
}
else if (num==2)
{
str[0]=1;
str[1]=1;;
Console.WriteLine(str[0]+","+str[1]);
}
else
{
str[0]=1;
str[1]=1;
for (int i=2;i<num;i++)
{
str[i]=str[i-1]+str[i-2];
}
for (int i=0;i<num;i++)
{
if (i==(num-1))
{
Console.Write(str[i]);
}
else
{
Console.Write(str[i]+",");
}
}
}
}
}
}
}
}写的有点多了~~
你既然要完整的代码 就不要嫌多了~~~
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
int temp1= 1;
int temp2 = 1;
string print=""; Console.WriteLine("请输入一个数");
i = int.Parse( Console.ReadLine());
while (i> 0)
{
Console.Write(print=temp1.ToString()+" ");
if (--i == 0) break;
Console.Write(print = temp2.ToString()+" ");
if (--i == 0) break;
temp1 = (temp1 <= temp2) ? (temp1 + temp2) : temp1;
temp2 = (temp2 <=temp1) ? (temp1 + temp2) : temp2;
}
Console.ReadKey();
}
}
}
别忘了给分啊~!
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i;
int temp1=1;
int temp2=1;
Console.WriteLine("请输入一个数");
i = int.Parse( Console.ReadLine());
while (i> 0)
{
Console.Write(temp1.ToString()+" ");
if (--i == 0) break;
Console.Write(temp2.ToString()+" ");
if (--i == 0) break;
temp1 = (temp1 <= temp2) ? (temp1 + temp2) : temp1;
temp2 = (temp2 <=temp1) ? (temp1 + temp2) : temp2;
}
Console.ReadKey();
}
}
}