1.回文是指顺读和倒读都一样的字符串。写一个方法,判断一个字符串str1,是否是回文,是回文返回true,否则返回false。例如字符串b是ag7ga是回文,而字符串abc6es就不是回文。要求编写应用程序,来检验方法的正确性。
public static bool huiwen(string s)
{
bool a;
string str = null;
for (int i = s.Length - 1; i >= 0; i--)
{
str += s[i];
}
if (str == s)
{
a = true;
}
else
a = false;
return a;
}
string s = Console.ReadLine();
bool a = huiwen(s);
if (a == true)
Console.WriteLine("是回文");
else
Console.WriteLine("不是回文");
Console.ReadLine();
2.写一个方法,统计一个字符串中单词的个数,返回值为单词个数。规定单词之间由若干个空格隔开。例如若输入字符串" I am a student ",得到结果为 4。要求编写应用程序,来检验方法的正确性。
public static int words(string s)
{
int count = 1;
for (int i = 0; i < s.Length; i++)
{
*****if (s[i] == ' ' && s[i + 1] != ' ')*****
{
count++;
}
}
return count;
}
string s = Console.ReadLine();
int a = words(s);
Console.WriteLine("单词个数为{0}", a);
Console.ReadLine();
3.写一个方法,判断的一个正整数是否是素数,返回值为布尔类型。要求编写应用程序,求1-100之间的所有素数。
public static bool sushu(int a)
{
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
return false;
}
}
return true;
}
public static void Main(string[] args)
{
int aaa =int.Parse(Console.ReadLine());
if (sushu(aaa) == true)
{
Console.WriteLine("是素数");
}
else
Console.WriteLine("不是素数");
for (int i = 1; i < 100; i++)
{
if (sushu(i) == true)
Console.WriteLine(i);
}
4.输入一个字符串,统计字符串中英文字母、数字字符和其他它符号的个数并输出。要求编写应用程序,来检验方法的正确性。
static void Letterdigitfuhao(string st)
{
string ssletter = "";
string ssdigit = "";
string ssfuhao = "";
int letter = 0;
int digit = 0;
int fuhao = 0;
for (int i = 0; i < st.Length; i++)
{
***if (char.IsLetter(st[i]))***
{
letter++;
ssletter += st[i];
}
else if (char.IsDigit(st[i]))
{
digit++;
ssdigit += st[i];
}
else
{
fuhao++;
ssfuhao += st[i];
}
}
Console.WriteLine("字母个数为{0}个,其中有{1}", letter, ssletter);
Console.WriteLine("数字个数为{0}个,其中有{1}", digit, ssdigit);
Console.WriteLine("其他符号个数为{0}个,其中有{1}", fuhao, ssfuhao);
}
public static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串:");
string st = Console.ReadLine();
Letterdigitfuhao(st);
Console.ReadLine();
}
5.写一个方法,对正整数m求和,其中求和公式为s= 1/2+1/3+…+1/m,方法返回s的值。要求编写应用程序,来检验方法的正确性。
public static double ssum(double n)
{
double sum = 0;
for (double i = 1; i < n; i++)
{
sum += 1 / (i + 1);
}
return sum;
}
double aaa =double.Parse( Console.ReadLine());
Console.WriteLine(ssum(aaa));
public static string AddDollar(string sentence)
{
string st = string.Empty;
for (int i = 0; i < sentence.Length; i++)
{
***if (char.IsLetter(sentence[i]))***
{
st += sentence[i] + "$";
}
else
{
st += sentence[i];
}
}
return st;
}
string st = Console.ReadLine();
Console.WriteLine("加¥后结果为{0}", AddDollar(st));
6.写一个方法,将一个字符串中所有英文字符后加一个$字符,并返回处理后的字符串。例如输入:A1B23CD45,则方法返回值为:A$1B
23
C
23C
23CD$45,要求编写应用程序,来检验方法的正确性。
public static string AddDollar(string sentence)
{
string st = string.Empty;
for (int i = 0; i < sentence.Length; i++)
{
***if (char.IsLetter(sentence[i]))***
{
st += sentence[i] + "$";
}
else
{
st += sentence[i];
}
}
return st;
}
public static void Main(string[] args)
{
Console.WriteLine("请输入一个字符串:");
string st = Console.ReadLine();
Console.WriteLine("加¥后结果为{0}", AddDollar(st));
}
7.写一个方法,删去所有字符串中的小写字符,其余字符不变。方法返回转变后的字符串。str=“AbC” 转变为串为=“AC”,要求编写应用程序,来检验方法的正确性。
public static string Dellower(string stt)
{
string st1 = string.Empty;
for (int i = 0; i < stt.Length; i++)
{
if (char.IsUpper(stt[i]))
{
st1 += stt[i];
}
}
return st1;
}
public static void Main(string[] args)
{
Console.WriteLine("请输一个字符串:");
string sttttt = Console.ReadLine();
Console.WriteLine(Dellower(sttttt));
Console.ReadLine();
}
8.写一个方法,对一个字符串,按如下规则加密:如果是英文字母则大写变小写、小写变大写,对非英文字符则保持不变。返回值为返回加密字符串。要求编写应用程序,来检验方法的正确性。
public static string UpperLowerDigit(string stt)
{
string st = string.Empty;
for (int i = 0; i < stt.Length; i++)
{
if (char.IsUpper(stt[i]))
{
st += char.ToLower(stt[i]);
}
else if (char.IsLower(stt[i]))
{
st += char.ToUpper(stt[i]);
}
else if (char.IsDigit(stt[i]))
{
st += stt[i];
}
}
return st;
}
public static void Main(string[] args)
{
Console.WriteLine("请输一个字符串:");
string st = Console.ReadLine();
Console.WriteLine(UpperLowerDigit(st));
Console.ReadLine();
}
9.写一个方法,求两个整数m和 n 的最大公约数,并作为返回值返回。要求编写应用程序,来检验方法的正确性。
public static int Gongyushu(int m, int n)
{
int gys = 0;
int temp = 0;
if (m < n)
{
temp = m;
m = n;
n = temp;
}
***for (int i = n; i > 0; i--)***
{
***if (m % i == 0 && n % i == 0)***
{
gys = i;
break;
}
}
return gys;
}
public static void Main(string[] args)
{
Console.WriteLine("求两个整数m和 n 的最大公约数");
int m = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());
Console.WriteLine(Gongyushu(m, n));
Console.ReadLine();
}
10.写一个方法,求两个整数m和 n 的最小公倍数,并作为返回值返回。要求编写应用程序,来检验方法的正确性。
public static int Gongbeishu(int m, int n)
{
int gys = 0;
int temp = 0;
if (m < n)
{
temp = m;
m = n;
n = temp;
}
for (int i = m; i <= m * n; i++)
{
if (i % m == 0 && i % n == 0)
{
gys = i;
break;
}
}
return gys;
}
public static void Main(string[] args)
{
Console.WriteLine("求两个整数m和 n 的最大公约数");
int m = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());
Console.WriteLine(Gongbeishu(m, n));
Console.ReadLine();
}
11.写一个方法,求s=1/a+1/aa+1/aaa+1/aaaa+1/aa…a的值,其中a是用户定义的数字。例如1/2+1/22+1/222+1/2222+1/22222(此时共有5个数相加),返回值为和s。要求编写应用程序,来检验方法的正确性。
***public static double Aaaaa(double aa, int nn)***
{
double s = 0;
double b = 1;
for (int i = 1; i < nn; i++)
{
b = b * 10 + 1;
s += 1 / (b * aa);
}
return s + 1 / aa;
}
public static void Main(string[] args)
{
Console.WriteLine("请输入数字a(a是1-9之间的数):");
double aaa = double.Parse(Console.ReadLine());
Console.WriteLine("次数:");
int nnnn = int.Parse(Console.ReadLine());
Console.WriteLine(Aaaaa(aaa, nnnn));
Console.ReadLine();
}
12.写一个方法,判断一个数是否是完数,返回值为布尔类型。一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3。要求编写应用程序,来检验方法的正确性。
public static bool wanshu(double n)
{
double sum = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
sum += i;
}
}
if (sum == n)
return true;
else
return false;
}
double aaa = double.Parse(Console.ReadLine());
Console.WriteLine(wanshu(aaa));