c# 经典521例(2)
有一函数:当x<0时,y=-1;当x=0时,y=0;当x>0时,y=1。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数:");
int sum = 0, i;
for (i = 1; i <= 100; i++)
{ sum += i; }
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
有一函数:
x (x<1);
Y= 2x-1 (1<=x<10);
3x-11 (x>=10);
写一程序,输入X的值,输出Y的值。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.Write("输入x的值:");
int y = 0;
int x = Convert.ToInt32(Console.ReadLine());
if (x < 1)
{ y = x; }
else if (x < 10)
{ y = 2 * x - 1; }
else
{ y = 3 * x - 11; }
Console.WriteLine("y值为:{0}", y);
Console.ReadKey();
}
}
}
编程求1+2+3+…+100的值,并输出结果
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int sum = 0, i;
for (i = 1; i <= 100; i++)
{ sum += i; }
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
判断m是否为素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.Write("输入m的值:");
int m = Convert.ToInt32(Console.ReadLine());
bool isSuShu=false;
for (int i = 2; i <= Math.Sqrt(m); i++)
{
if (m % i == 0)
{
isSuShu =false ;
break;
}
else
{ isSuShu = true; }
}
if (isSuShu)
{ Console.WriteLine("是素数"); }
else
{ Console.WriteLine("不是素数"); }
Console.ReadKey();
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.Write("输入x的值:");
int m = Convert.ToInt32(Console.ReadLine());
for (int i = 2; i <= Math.Sqrt(m); i++)
{
//错误的方法,哪位高手能提供这种方法的实现!
if (m % i != 0)
{
Console.WriteLine("是素数");
}
else
{
Console.WriteLine("不是素数");
}
}
Console.ReadKey();
}
}
}
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int letters = 0, space = 0, digit = 0, others = 0;
string strs = Console.ReadLine();
char[] chars = strs.ToCharArray();
foreach (char c in chars)
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letters++;
else if (c == ' ')
space++;
else if (c >= '0' && c <= '9')
digit++;
else
others++;
}
Console.WriteLine("字母有{0}个\n空格有{1}个\n数字有{2}个\n其他{3}个
", letters,space,digit,others);
Console.ReadKey();
}
}
}
求S=a+aa+aaa+...+aaa...aa之值,其中a是一个数字,n表示a的位数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int sum=0, a=0, an=0,count=0,n;
Console.Write("请输入一个数字:");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入位数:");
n = Convert.ToInt32(Console.ReadLine());
while(count<n)
{
an += a;
a = a * 10;
sum += an;
++count;
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
求1+2!+3!+4!+…+20!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int n, s = 0, t = 1;
for (n = 1; n <= 20; n++)
{
t = t * n;
s = s + t;
}
Console.WriteLine("1!+2!+…+20!={0}", s);
Console.ReadKey();
}
}
}
输出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i, j, k, n;
Console.WriteLine("water flower number is:");
for (n = 100; n < 1000; n++)
{
i = n / 100;
j = n / 10 % 10;
k = n % 10;
if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k)
Console.WriteLine(n);
}
Console.ReadKey();
}
}
}
有一分数序列:2/1,3/2,5/3,8/5,….,21/13,……前二十项之和。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
float i, t, n = 20;
float a = 2, b = 1, s = 0;
for (i = 1; i <= n; i++)
{
s = s + a / b;
t = a;
a = a + b;
b = t;
}
Console.WriteLine("sum={0}", s);
Console.ReadKey();
}
}
}