24 C++ 循环 for/while 练习

  • Post author:
  • Post category:其他


#include<iostream>
#include<string>
using namespace std;

// 输入n,输出1-n的数值,用不同的方法
//方法一:
void forInFun() {
	cout << "请输入数值" << endl;
	int n;
	cin >> n;
	cout << "for方法输出:\n" << endl;
	for (int i = 1; i < n + 1; i++)
	{
		printf("%d\n", i);
	}

}

// 方法二:
void whileFun() {
	cout << "请输入数值" << endl;
	int n, i;
	cin >> n;
	cout << "while方法输出:\n" << endl;
	i = 1;
	while (i < n + 1) {
		printf("%d\n", i);
		i++;
	}

}


// 用for打印26个字母
template <class T>
int getArrayLen(T& array)
{// 获取数组元素总量
	return sizeof(array) / sizeof(array[0]);
}
void forLetterFun() {

	string letter[] = { "A","B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z", };
	int length = getArrayLen(letter);
	cout << "for方法输出:\n" << endl;
	for (int i = 0; i < length; i++)
	{
		printf("%s\n", letter[i].c_str());
	}
}


void print_interval_integers() {
	
	int input01, input02, temp;
	cout << "请输入2个数值,以空格隔开;" << endl;
	cin >> input01 >> input02;
	// 先判断2个数是否从小到大,如果不是他们的顺序向调换
	if (input02 < input01){
		temp = input02;
		input02 = input01;
		input01 = temp;
	}
	cout << "输出:\n" << endl;
	for (int i = input01+1; i < input02; i++)
	{
		printf("%d\n",i);
	}
}





// 打印区间整数(不包含输入的两个整数)
void Factorial() {
	// 输入1个整数,输出n的阶乘;
	long double n, num;
	cout << "请输入1个非负整数;" << endl;
	cin >> n ;
	cout << "输出:\n" << endl;
	if (n < 0) {
		printf("小于0的数值没有阶乘");
	}
	else if(n==0)
	{
		printf("1");
	}
	else {
		num = 1;// 初始化阶乘,从1开始
		for (long double i = 1; i < n+1; i++)
		{
			num *= i;
			
		}
		cout << num << endl;
		printf("%.2lf", num);// 用%lf输出long double类型数值
	}

}


#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
// 连续输入n个非负整数数值,数值之间用","号相隔,求这段数值的最大值、最小值和平均值
void continuous_input() {
	cout << "请连续输入5个非负整数数值,数值之间用\", \"号相隔,求这段数值的最大值、最小值和平均值" << endl;
	string input;
	string result_str;
	string result_array[50];
	cin >> input;
	const char* delim = ","; // 参数delim则为分隔字符串
	char sentence_char[50]; // 创建char数组类型
	strcpy(sentence_char, input.c_str());//将string复制为char数组
	char* result;// 我们要的结果
	result = strtok(sentence_char, delim);
	int index = 0;
	// 分割字符串,加入字符串数组
	while (result)
	{
		result_str = result; // char -> string
		printf("%s\n", result_str.c_str()); // string类型
		result_array[index] = result_str.c_str(); //把string放入string数组;
		result = strtok(NULL, delim);// 第二次调用该函数strtok(NULL,","),第一个参数设置为NULL。结果返回分割依据后面的字串,即第二次输出windows
		index++;
	}

	int num[5];
	double sum=0;
	// 把字符串数组转换为整数数组
	for (int i = 0; i < index; i++)
	{
		num[i] = atoi(result_array[i].c_str());
		cout << "num[i]:" << num[i] <<endl;// 最大值
		cout << typeid(num[i] - 0).name() << endl;// 查看result变量类型
		sum += (double)num[i];
	}
	int len = sizeof(num) / sizeof(int); // 数组长度
	cout << "数组长度:" << len << endl;// 最大值
	cout<<"最大值:" << *max_element(num, num + len) << endl;// 最大值
	cout << "最小值:" << *min_element(num, num + len) << endl;// 最小值
	double average;
	average = sum / (double)len; // 平均值
	printf("平均值为:%.2f", average);
}



//求64格棋盘米粒数量,每格式前格的2倍
int rice_chessboard() {
	long double rice_count = 1;
	int n = 1;
	while (n<=64)
	{
		rice_count *= 2;
		//cout << rice_count << endl;
		printf("%.0lf\n", rice_count);
		n++;
	}
	cout << "rice_count:" << rice_count << endl;
	return 0;
}






// 钱够花吗?
void enough_money_spend() {
	double principal = 2000000;// 本金
	double expenditure = 200000; // 支出
	int years = 1;
	while (years<50)
	{
		principal = principal + principal * 0.12 - expenditure;
		printf("第%d年剩余: %.2f元\n", years,principal);
		if (principal<=0) {
			printf("用了%d年取完。",years);
			break;
		}
		years++;
	}
}





// 逼近
void approach() {
	float result = 1;
	int  n=1;
	float numerator =1; // 分子
	float temp=1.0;
	float old_result=1.0;// 上一个结果
	bool same = false;
	while (n<550000)
	{
		old_result = result;
		numerator = (numerator + 2);
		temp = 1.0 / numerator;
		//cout << n % 2 << endl;
		if (n % 2 != 0) {  // 单数
			result = result - temp;
		}
		else
		{
			result = result + temp;
		}

		if (to_string(old_result) == to_string(result)) {
			same = true;
		}
		else {
			same = false;
		}

		printf("第%d次,分数为%f,上次结果为:%f,结果为:%f,判断一致的结果为%d\n",n, temp, old_result, result,same);

		if (same) {
			//printf("第%d次,分数为%f,结果为:%f\n", n, temp, result);
			printf("第%d次结果不再变化",n);
			break;
		}
		n++;
	}
}



版权声明:本文为weixin_41622043原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。