最后,计算并输出二维矩阵的主对角线及其右上方所有元素的累乘积(很容易“溢出”的哦!)。
main(){int a[20]={12,34,56,67,89,123,345,367,890,1234,1236,1345,1456,1789,1999,2111,2222,3333,4444,5555},i,n
printf("请输入要查找的数:")
scanf("%d",&n)
for(i=0i<20i++)
if(a[i]==n) {printf("该数的下标值是%d",i)break}
if(i==20) printf("no found")}
您好,软糖来回答罗。
已编码并调试成功!
代码如下
using Systemnamespace 简单数组程序 {
class Program {
static void Main(string[] args) {
Console.WriteLine("// 输入 x 退出本程序")
//请用户输入实数,循环直到正确输入
int 个数 = 10
decimal[] 实数 = new decimal[个数]
for (int i = 0 i < 个数 i++) {
bool 结果 = false
string 输入内容 = ""
while (结果 == false) {
Console.BackgroundColor = ConsoleColor.DarkBlue
Console.Write("请输入第 {0} 号实数: ", i + 1)
Console.BackgroundColor = ConsoleColor.Black
输入内容 = Console.ReadLine()
if (输入内容.Trim(' ').ToLower() == "x") { Environment.Exit(0) }
if (输入内容.Trim(' ').ToLower() == "") { continue }
结果 = 判断实数是否合法(输入内容)
}
实数[i] = decimal.Parse(输入内容)
Console.BackgroundColor = ConsoleColor.DarkGreen
Console.WriteLine("录入 {0} 号实数 = {1}。", i + 1, 实数[i])
Console.BackgroundColor = ConsoleColor.Black
}
Console.BackgroundColor = ConsoleColor.DarkCyan
Console.WriteLine("按任意键开始逆序输出...")
Console.BackgroundColor = ConsoleColor.Black
Console.ReadKey()
for (int i = 个数 - 1 i >= 0 i--) {
Console.Write("{0}", 实数[i])
if (i > 0) { Console.Write(",") }
}
Console.WriteLine()
Console.BackgroundColor = ConsoleColor.DarkCyan
Console.Write("按任意键退出...")
Console.ReadKey()
}
static bool 判断实数是否合法(string 输入内容) {
decimal 转换的实数
bool 是数字 = decimal.TryParse(输入内容, out 转换的实数)
if (是数字 == false) //转换数字失败就再次请求输入正确值。
{ Console.WriteLine("实数不正确!") return false } else {
return true
}
}
}
}
满意请采纳,谢谢。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)