C#程序设计教程编程题(一)
来源:互联网 发布:建筑三维计算软件 编辑:程序博客网 时间:2024/06/05 02:49
(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(); } }}
运行结果:
1 0
- C#程序设计教程编程题(一)
- C#程序设计教程编程题(二)
- C#程序设计教程笔记(一)
- C#程序设计(一)
- C#程序设计教程笔记(二)
- C#程序设计(一)---水仙花数
- C#程序设计(一)
- Linux程序设计(Linux shell编程一)
- C#进阶教程(一)
- [连载]C#程序设计(08)--- C#核心编程-6 --- 方法
- C#程序设计:绘制正方形并显示其信息(李乃文等:《C#程序设计实践教程》)
- C#编程实践(一)
- C# GDI+编程(一)
- C# GDI+编程(一)
- C#多线程编程(一)
- C#接口编程(一)
- C#编程基础(一)
- C#并发编程(一)
- 理解JVM工作原理总结
- iOS----网络开发之第三方库AFNetworking
- 通过一个大型项目来学习分布式算法(6)
- 颜色的类别 可以直接写 #333333 一句代码就能实现
- Sending 'NSProgress *__strong *' to parameter of incompatible type 'void (^ _Nullable)(NSProgress *
- C#程序设计教程编程题(一)
- 电话拨号
- JAVA排序算法---归并排序
- QT 从尾部截取字符串(从路径中截取文件名)
- Github使用学习笔记(一)
- Android Thread子线程和主线程的UI交互
- 技术点整理
- 不用递归,写出快速求幂的程序
- Perl正则表达式总结_2