C#第八节课
2016-03-09 16:48
330 查看
for的穷举迭代、while、do while
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace for的穷举迭代
{
class Program
{
static void Main(string[] args)
{
//穷举
//把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况
//单位给发了张150元的购物卡,
//拿着到超市买三类洗化用品。
//洗发水15元,香皂2元,牙刷5元。
//求刚好花完150元,有多少种买法
//每种卖法都是各买几样
//设洗发水x 150/15==10
//牙刷y 150/5==30
//香皂z 150/2==75
//int biao = 0;
//for (int x = 0; x <= 10; x++)
//{
// for (int y = 0; y <= 30; y++)
// {
// for (int z = 0; z <= 75; z++)
// {
// if (x * 15 + y * 5 + z * 2 == 150)
// {
// biao++;
// Console.WriteLine("这是第"+biao+"种买法:洗发水买"+x+"种,牙刷买"+y+"种,香皂买"+z+"种");
// }
// }
// }
//}
//Console.WriteLine("一共有"+biao+"种买法");
//Console.ReadLine();
//迭代:
//从初始情况按照规律不断求解中间情况,最终推导出结果。
//纸张可以无限次对折,纸张厚度为0.07毫米。
//问多少次对折至少可以超过8848?
//int a = 7;
//int i = 1;
//for (; ; i++)
//{
// a *= 2;
// if(a>=884800000)
// {
// Console.WriteLine(i);
// Console.WriteLine(a);
// break;
// }
//}
//Console.ReadLine();
//while语句
//int a = 7;
//int b = 0;
//while (a <= 884800000)
//{
// a *= 2;
// b++;
//}
//Console.WriteLine(b);
//do while语句
int a = 7;
int b = 0;
do
{
a *= 2;
b++;
} while (a <= 884800000);
Console.WriteLine(b);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace for的穷举迭代
{
class Program
{
static void Main(string[] args)
{
//穷举
//把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况
//单位给发了张150元的购物卡,
//拿着到超市买三类洗化用品。
//洗发水15元,香皂2元,牙刷5元。
//求刚好花完150元,有多少种买法
//每种卖法都是各买几样
//设洗发水x 150/15==10
//牙刷y 150/5==30
//香皂z 150/2==75
//int biao = 0;
//for (int x = 0; x <= 10; x++)
//{
// for (int y = 0; y <= 30; y++)
// {
// for (int z = 0; z <= 75; z++)
// {
// if (x * 15 + y * 5 + z * 2 == 150)
// {
// biao++;
// Console.WriteLine("这是第"+biao+"种买法:洗发水买"+x+"种,牙刷买"+y+"种,香皂买"+z+"种");
// }
// }
// }
//}
//Console.WriteLine("一共有"+biao+"种买法");
//Console.ReadLine();
//迭代:
//从初始情况按照规律不断求解中间情况,最终推导出结果。
//纸张可以无限次对折,纸张厚度为0.07毫米。
//问多少次对折至少可以超过8848?
//int a = 7;
//int i = 1;
//for (; ; i++)
//{
// a *= 2;
// if(a>=884800000)
// {
// Console.WriteLine(i);
// Console.WriteLine(a);
// break;
// }
//}
//Console.ReadLine();
//while语句
//int a = 7;
//int b = 0;
//while (a <= 884800000)
//{
// a *= 2;
// b++;
//}
//Console.WriteLine(b);
//do while语句
int a = 7;
int b = 0;
do
{
a *= 2;
b++;
} while (a <= 884800000);
Console.WriteLine(b);
Console.ReadLine();
}
}
}
相关文章推荐
- C#成魔之路<11> GDI+绘图
- C# 文件相关操作
- C#动态编译代码并执行
- C#委托 、事件
- 继承与嵌套类(C#)
- C#动态选择调用某个指定内部函数
- C# 在Word文档中生成条形码
- C# 获取并判断操作系统版本,解决Win10、 Windows Server 2012 R2 读取失败的方案
- csharp:Chart
- C# 在Word文档中生成条形码
- C#中DataTable中的Compute方法使用收集(续)
- C#第七节课
- C#中DataTable中的Compute方法使用收集
- 【转】C#编译器错误 CS0246综述
- C# DataView数据筛选与排序
- C# 字符串转换为计算公式
- csharp: Socket
- c# 代码调用c++生成的dll过程
- C#转义字符总结
- C# Devexpress gridControl 自定义编辑器 设置弹出框(弹出框包含gridLookUpEdit)