(1)设计一个程序,输出所有的水仙花数。所谓水仙花数,是指一个三位整数,其各位数字的立方等于该数的本身。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int a,b,c;
for (int num = 100; num <= 999; num++)
{
a = num / 100;
b = num / 10 % 10;
c = num % 10;
if (a*a*a+b*b*b+c*c*c==num)
Console.WriteLine(num);
}
Console.ReadKey();
}
}
}
运行结果:
(2)判断s所指的字符串是否是“回文”
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
string s= Console.ReadLine();
char[] b = s.ToCharArray();
int len=s.Length;
int n = 0;
for (int i = 0; i <= len / 2; i++)
{
if (b[i] != b[len - 1 - i])
{
Console.WriteLine("字符串"+s+"不是回文");
n = 1;
break;
}
}
if (n==0)
Console.WriteLine("字符串" + s + "是回文");
Console.ReadKey();
}
}
}
运行结果:
(3)设计一个程序,输入10个数存入数组中,求最大值、最小值和平均值。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
string[] a = Console.ReadLine().Split(' ');
int[] b = new int[10];
for (int i = 0; i < a.Length; i++)
{
b[i] = int.Parse(a[i]);
}
int sum = 0, max = b[0], min = b[0];
for (int i = 0; i < a.Length; i++)
{
sum += b[i];
if (max < b[i])
{
max = b[i];
}
if (min > b[i])
{
min = b[i];
}
}
double avg = (double)sum / b.Length;
Console.WriteLine("最大值、最小值、平均分依次是{0},{1},{2}",max,min, avg);
Console.ReadKey();
}
}
}
运行结果:
(4)编写程序,输入一个整数,将其各位数字颠倒顺序后输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
string s=Console.ReadLine();
int num = Convert.ToInt32(s);
int[] a = new int[s.Length];
int i = 0;
while (num!=0)
{
a[i++] = num % 10;
num /= 10;
}
for (i = 0; i < s.Length; i++)
{
Console.Write(a[i]);
}
Console.ReadKey();
}
}
}
运行结果: