7-1 输入一个字符串,统计其中有多少个单词。单词之间用空格分开。
如果字符串为空的话,那肯定个数就是0;
如果字符串不为空,高低都会有一个单词在内,题目说单词之间用空格分开,那么最少都会有一个。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
if (s.Length != 0)
{
int sum = 1;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ') sum++;
}
Console.WriteLine(sum);
}
else Console.WriteLine(0);
Console.ReadKey();
}
}
}
7-2 设定一个有大小写字母的字符串,先将字符串的大写字母输出,再将字符串的小写字母输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
for(int i=0;i<s.Length;i++)
{
if (s[i] >= 'A' && s[i] <= 'Z') Console.Write(s[i]);
}
for(int i=0;i<s.Length;i++)
{
if (s[i] >= 'a' && s[i] <= 'z') Console.Write(s[i]);
}
Console.ReadKey();
}
}
}
7-3 设定一个有大小写字母的字符串和一个查询字符串,使用String类的IndexOf判断在该字符串中要查找的字符串出现的次数。
知识点:
IndexOf方法用于搜索一个字符串,某个特定的字符或字串第一次出现的位置,该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或字串,则返回-1。
(类似c++ strstr......)
提取字串:利用String类提取字串方法Substring可以从一个字符串中得到子字符串。
public String Substring(int startIndex);
返回一个从 startIndex开始到结束的子字符串;
public String Substring(int startIndex ,int length);
返回一个从 startIndex开始长度为length的子字符串。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
String s = Console.ReadLine();//主串
String c = Console.ReadLine();//要查找的串
int index = 0;
int sum = 0;
while (index != -1)
{
index = s.IndexOf(c);
if (index != -1)
{
s = s.Substring(index+c.Length);
//返回一个截去第一个查找串的子字符串;
sum++;
}
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
7-4 编写一个程序,打印输出包含20个元素的double型数组dblArray中的最大值和最小值。
忘记改数组名称了,问题不大。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
int t = 20;
bool flag = true;
while (flag)
{
double[] a = new double[30];
double maxn=0, minn=0;
for (int i = 0; i < 20; i++)
{
a[i] = double.Parse(Console.ReadLine());
if (i == 0)
{
maxn = a[i];
minn = a[i];
}
else if (a[i] > maxn) maxn = a[i];
else if (a[i] < minn) minn = a[i];
}
Console.WriteLine(maxn + " " + minn);
t--;
if (t == 0) flag = false;
}
Console.ReadKey();
}
}
}
7-5 求出一个5*5矩阵对象元素的和。
ing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[6, 6];
int sum = 0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
{
int x = int.Parse(Console.ReadLine());
sum += x;
}
Console.WriteLine(sum);
Console.ReadKey();
}
}
}
7-6 将一个5*3的二维数组转置输出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[6, 6];
for(int i=1;i<=5;i++)
{
for(int j=1;j<=3;j++)
{
a[i,j] = int.Parse(Console.ReadLine());
}
}
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 5; j++)
{
Console.Write(a[j, i]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
7-7 设一个一维数组中有20个元素,编写一个查找程序,从中查找值为98的元素所在的数组中的下标。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[30];
int idx = -1;
for(int i=1;i<=20;i++)
{
int x = int.Parse(Console.ReadLine());
if (x == 98) idx = i;
}
if (idx == -1) Console.WriteLine("该数组中不存在98");
else Console.WriteLine(idx);
Console.ReadKey();
}
}
}
7-8 输入10个数到一维数组中,分别实现数据的输入、排序以及输出。
用了个最简单的冒泡排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[30];
for(int i=1;i<=10;i++)
{
a[i] = int.Parse(Console.ReadLine());
}
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
for (int i = 1; i <= 10; i++)
Console.Write(a[i] + " ");
Console.ReadKey();
}
}
}
7-9 设计一个程序,求一个5*5矩阵两对角线元素之和。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第7章课后习题
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[10, 10];
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
a[i, j] = int.Parse(Console.ReadLine ());
}
}
int sum = 0;
for (int i = 1; i <= 5; i++)
sum += a[i, i];
Console.WriteLine(sum);
sum = 0;
for (int i = 1; i <= 5; i++)
sum += a[i, 6 - i];
Console.WriteLine(sum);
Console.ReadKey();
}
}
}