JACK_C#_for循环
来源:互联网 发布:057型护卫舰数据和航速 编辑:程序博客网 时间:2024/06/08 01:49
// 作业A
#region 1、编程将所有“水仙花数”打印出来,并打印其总个数。“水仙花数”是一个各个位立方之和等于该整数的三位数。
int count = 0;
for (int i = 100; i < 1000; i++) {
double a = i / 100;
double b = (i / 10) % 10;
double c = i % 10;
if(i == Math.Pow(a,3) + Math.Pow(b,3) + Math.Pow(c,3)) //可以用int强转
{
++count;
Console.WriteLine ("水仙花数{0}",i);
}
}
Console.WriteLine ("总个数:{0}",count);
#endregion
#region 2、自定义一个数组并求数组中的所有元素最大值、最小值、平均值及各元素之和。
int[] arr = {234,5,24,77,98,4,25};
int max = arr[0];
int min = arr[0];
int sum = 0;
for (int i = 0; i < arr.Length; ++i) {
if(max < arr[i])
max = arr[i];
if(min > arr[i])
min = arr[i];
sum += arr[i];
}
Console.WriteLine ("最大值为{0},最小值为{1},平均值为{2}",max,min,sum/arr.Length);
#endregion
#region 3、已知abc+cba = 1333,其中a,b,c均为一位数,编程求出满足条件的a,b,c所有组合。
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
if (a * 100 + b * 10 + c + c * 100 + b * 10 + a == 1333){
Console.WriteLine ("{0}{1}{2}",a,b,c);
}
}
}
}
#endregion
// 作业B
#region 1、一个球从100m高度自由落下,每次落地后反跳回原来高度的一半,再落下,再反弹。求它在第10次落地时,共经过多少米?第10次反弹多高
float sum = 100.0f;
float rebound = 100.0f;
for (int i = 1; i < 10; i++) {
rebound = rebound / 2.0f;
sum += rebound * 2.0f; //第10次落地经过距离,不包括第10次反弹的距离
}
Console.WriteLine ("第10次落地时共经过{0}米,第10次反弹{1}米",sum,rebound / 2.0f);
#endregion
//
#region 2、输入n,分别用*输出边长为n的实心菱形。例如:n = 3时,输出
// 空格 星号 行数
// * 2 1 1
// *** 1 3 2
// ***** 0 5 3
// *** 1 3 4
// * 2 1 5
// |n-i| 2n-1-|2n-2i| i
Console.WriteLine ("n:");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i < 2 * n; ++i) {
int a = 0;
while(a < Math.Abs(n-i))
{
a++;
Console.Write(" ");
}
for (int j = 0; j < 2 * n - 1 - Math.Abs(2 * n - 2 * i); ++j) {
Console.Write ("*");
}
Console.WriteLine ();
}
#endregion
#region 1、编程将所有“水仙花数”打印出来,并打印其总个数。“水仙花数”是一个各个位立方之和等于该整数的三位数。
int count = 0;
for (int i = 100; i < 1000; i++) {
double a = i / 100;
double b = (i / 10) % 10;
double c = i % 10;
if(i == Math.Pow(a,3) + Math.Pow(b,3) + Math.Pow(c,3)) //可以用int强转
{
++count;
Console.WriteLine ("水仙花数{0}",i);
}
}
Console.WriteLine ("总个数:{0}",count);
#endregion
#region 2、自定义一个数组并求数组中的所有元素最大值、最小值、平均值及各元素之和。
int[] arr = {234,5,24,77,98,4,25};
int max = arr[0];
int min = arr[0];
int sum = 0;
for (int i = 0; i < arr.Length; ++i) {
if(max < arr[i])
max = arr[i];
if(min > arr[i])
min = arr[i];
sum += arr[i];
}
Console.WriteLine ("最大值为{0},最小值为{1},平均值为{2}",max,min,sum/arr.Length);
#endregion
#region 3、已知abc+cba = 1333,其中a,b,c均为一位数,编程求出满足条件的a,b,c所有组合。
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
if (a * 100 + b * 10 + c + c * 100 + b * 10 + a == 1333){
Console.WriteLine ("{0}{1}{2}",a,b,c);
}
}
}
}
#endregion
// 作业B
#region 1、一个球从100m高度自由落下,每次落地后反跳回原来高度的一半,再落下,再反弹。求它在第10次落地时,共经过多少米?第10次反弹多高
float sum = 100.0f;
float rebound = 100.0f;
for (int i = 1; i < 10; i++) {
rebound = rebound / 2.0f;
sum += rebound * 2.0f; //第10次落地经过距离,不包括第10次反弹的距离
}
Console.WriteLine ("第10次落地时共经过{0}米,第10次反弹{1}米",sum,rebound / 2.0f);
#endregion
//
#region 2、输入n,分别用*输出边长为n的实心菱形。例如:n = 3时,输出
// 空格 星号 行数
// * 2 1 1
// *** 1 3 2
// ***** 0 5 3
// *** 1 3 4
// * 2 1 5
// |n-i| 2n-1-|2n-2i| i
Console.WriteLine ("n:");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i < 2 * n; ++i) {
int a = 0;
while(a < Math.Abs(n-i))
{
a++;
Console.Write(" ");
}
for (int j = 0; j < 2 * n - 1 - Math.Abs(2 * n - 2 * i); ++j) {
Console.Write ("*");
}
Console.WriteLine ();
}
#endregion
阅读全文
0 0
- JACK_C#_for循环
- JACK_C#_循环语句
- 黑马程序员_for循环
- 13_for循环
- bat知识点3_for循环
- java基础_day0004_循环_for
- java基础_day0010_循环_for
- 黑马程序员_java入门_for循环实例
- 07黑马程序员_for循环,跳转语句
- 黑马程序员_for循环和函数
- 黑马程序员_for循环及增强for循环_java
- J2ME代码效率测试_for循环和除法
- asm source code note 1.3_for循环的实现
- 2015.4.25.13.53_for_2015.4.25_for循环总结
- bat知识点3_for循环_跳出嵌套
- bat知识点3_for循环_指定变数
- Java基础学习应用_for循环打印99乘法表
- Java基础学习应用_for循环打印水仙花数
- 阿里传奇工程师多隆的程序世界
- Android studio导出apk包只有400多KB,安装之后闪退问题
- CSS总结
- 搜索学习--Lucene中搜索的排序、范围区间搜索、分页搜索、多条件搜索
- keepalived error: ‘O_PATH’ undeclared
- JACK_C#_for循环
- 大数据时代的图表可视化利器——highcharts,D3和百度的echarts
- background-color和background-image一起用的问题
- 值得学习的C语言开源项目
- pandas 学习(一)
- KM小练
- 2017第十届中国国际线缆工业展览会会刊(参展商名录)
- WebView加载网页不显示图片解决办法
- Ubuntu 16.04 + Nvidia 显卡驱动 + Cuda 8.0 (问题总结 + 解决方案)