第四章-循环程序设计代码实例(C++蓝豹子)

例4.2

设计一个统计某班成绩最高分、最低分、平均分的程序

#include <iostream>
using namespace std;
int main(){
	int value, avg, max, min, numOfStudent;
	//变量初始化
	avg = 0;
	max = 0;
	min = 100;
	
	cout << "请输入学生人数:";
	cin >> numOfStudent;
	
	for(int i=1; i<=numOfStudent; i++){
		cout << "请输入第" << i << "个学生的成绩:";
		cin >> value;
		avg += value;
		
		if(value > max) max = value;
		if(value < min) min = value;
	}
	
	cout << "最高分:" << max << endl;
	cout << "最低分:" << min << endl;
	cout << "平均分:" << avg / numOfStudent << endl;
	 
	return 0;
} 

在这里插入图片描述

例4.3

输出字母A-Z的内码

#include <iostream>
using namespace std;

int main(){
	for (char ch = 'A'; ch <= 'Z'; ++ch){
		cout << ch << '(' << int(ch) << ')' << " ";
	}
	return 0;
} 

在这里插入图片描述

例4.4

计算1-100之间的素数和

#include <iostream>
using namespace std;

int main(){
	int num, k, count, sum = 2;
	for (num = 3; num <= 100; num+=2){
		count = 0;
		for (k = 1; k <= num; k+=2){
			if(num % k == 0) ++count;
			
		}
		if(count==2){
			sum += num;
		}
	}
	
	cout << "1-100之间的素数和为:" << sum << endl;
	return 0;
} 

在这里插入图片描述
例4.5 求输入整数的最大因子

#include <iostream>
using namespace std;

int main(){
	int num, fac;
	
	cout << "请输入一个数:";
	cin >> num;
	for (fac = num/2; num % fac != 0; --fac);
	cout << num << "的最大因子是:" << fac <<endl; 
	return 0;
} 

在这里插入图片描述

break使用 例4.6

输入一个数,检查是否为素数

#include <iostream>
using namespace std;

int main(){
	int num, k;
	cout << "请输入要检测的数:";
	cin >> num;
	
	if(num == 2){
		cout << num << "是素数";
		return 0;
	}
	
	//排除1和偶数 
	if(num % 2 == 0 || num ==1){
		cout << num << "不是素数\n";
		return 0;
	}
	
	for (k=3; k<num;k+=2){
		if(num % k ==0) break;
	}
	
	if(k < num) cout << num << "不是素数\n";
	else cout << num << "是素数\n";
	return 0;
}

在这里插入图片描述

continue使用 三层for循环输出A、B、C的全排列

#include <iostream>
using namespace std;

int main(){
	char ch1,ch2,ch3;
	for(ch1 = 'A'; ch1 <= 'C'; ++ch1){
		for(ch2 = 'A'; ch2 <= 'C'; ++ch2){
			if(ch1 == ch2)
				continue;
			else{
				for(ch3 = 'A'; ch3 <= 'C'; ++ch3){
					if(ch3 == ch1 || ch3 == ch2)
						continue;
					else
						cout << ch1 << ch2 << ch3 << '\n'; 
				}
			}
		}
	}
	return 0;
}

在这里插入图片描述

例4.8

用基于哨兵模式的循环处理统计分数程序

#include <iostream>
using namespace std; 

int main(){
	int value, avg, max, min, noOfInput;
	
	avg = 0;
	max = 0;
	min = 100;
	noOfInput = 0;//学生人数
	
	cout << "请输入第一个人的成绩:";
	cin >> value;
	while(value != -1){
		++noOfInput;
		avg += value;
		if(value > max){
			max = value;
			
		}
		if(value < min){
			min = value;
		}
		cout << "请输入第" << noOfInput + 1 << "个人的成绩:";
		cin >> value;
	}
	cout << "最高分为:" << max << endl;
	cout << "最低分为:" <<min << endl;
	cout << "平均分为:" <<avg / noOfInput << endl;
	
	return 0;
} 

在这里插入图片描述

例4.9

计算e^x的值
思路:根据无穷级数,e^x各项关系是第i项的值是x^i/i!,第i+1项是x^(i+1)/(i+1)!,设前一项为item,则后一项为item*x/(i+1),当item<0.000001时结束

#include <iostream>
using namespace std;

int main(){
	double ex, x, item;
	int i;
	
	cout << "请输入x:";
	cin >> x;
	
	ex = 0;
	item = 1;
	i = 0;
	
	while (item>1e-6){
		ex += item;
		++i;
		item = item * x / i;
	} 
	
	cout << "e的" << x << "次方等于:" << ex << endl;
	return 0;
}

在这里插入图片描述

例4.10

编写一个程序,输入一个句子(以句号结束),统计该句子中元音字母数、辅音字母数、空格数、数字数及其他字符数

字符输入用cin.get

#include <iostream>
using namespace std;

int main(){
	char ch;
	int numVowe1 = 0, numCons = 0, numSpace = 0, numDigit = 0, numOther = 0;
	
	cout << "请输入句子:";
	
	cin.get(ch);
	while(ch != '.'){
		if(ch >= 'A' && ch <= 'Z')
			ch = ch - 'A' + 'a';//大写字母转小写
		if(ch >= 'a' && ch <= 'z'){
			if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
				++numVowe1;
				
			}
			else
				++numCons;
		}
		else if(ch == ' ') ++numSpace;
		else if(ch >= '0' && ch <= '9') ++numDigit;
		else ++numOther;
		
		cin.get(ch); 
	}
	cout << "元音字母个数:" << numVowe1 << endl;
	cout << "辅音字母个数:" << numCons << endl;
	cout << "空格个数:" << numSpace << endl;
	cout << "数字个数:" << numDigit << endl;
	cout << "其它字符个数:" << numOther << endl;
	return 0;
} 

在这里插入图片描述

例4.11

计算方程f(x)=0在某一区间内的实根,使用弦截法
(1)令x1=a,x2=b
(2)用公式x=(x1f(x2) - x2f(x1))/(f(x2)-f(x1))
(3)若f(x)与f(x1)同符号则方程的根在(x1,x2)之间,x作为新的x1,
否则x作为新的x2,重复上述步骤,直到f(x)<某个指定的精度为止

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

int main(){
	double x, x1 = -1, x2 = 1, f2, f1, f, epsilon;
	
	cout << "请输入精度:";
	cin >> epsilon;
	
	do{
		f1 = x1 * x1 * x1 + 2 * x1 * x1 + 5 * x1 - 1;
		f2 = x2 * x2 * x2 + 2 * x2 * x2 + 5 * x2 - 1;
		
		x = (x1 * f2 - x2 * f1) / (f2 - f1);
		
		f = x * x * x + 2 * x * x + 5 * x - 1;
		
		if(f * f1 > 0) x1 = x;
		else x2 = x;
	}while(fabs(f) > epsilon);
	
	cout << "方程的根是:" << x << endl;
	return 0;
} 

在这里插入图片描述

例4.12

计算算式:ABCD x E = DCBA,其中A、B、C、D、E
代表不同的数字

法1:暴力枚举

#include <iostream>
using namespace std;

int main(){
	int A, B, C, D, E, num1, num2;
	
	for(A=1;A<=9;++A){
		for(B=0;B<=9;++B){
			if(A == B) continue;
			for(C=0;C<=9;++C){
				if(C == B || C == A) continue;
				for(D=1;D<=9;++D){
					if(D == C || D == B || D == A) continue;
					for(E=2;E<=9;++E){
						if(E == D || E == C || E == B || E == A) continue;
						
						num1 = A * 1000 + B * 100 + C *10 + D;
						num2 = D * 1000 + C *100 +B *10 + A;
						if(num1 * E == num2){
							cout << num1 << "*" << E << "=" << num2 << endl;
						} 
					}
				}
			}
		}
	}
	return 0;
} 

法2:枚举num1,分离数字确定num2,再枚举E

#include <iostream>
using namespace std;

int main(){
	int A, B, C, D, E, num1, num2;
	for(num1 = 1023; num1 <= 9876; ++num1){
		A = num1 / 1000;
		B = num1 % 1000 / 100;
		C = num1 % 100 /10;
		D = num1 % 10;
		if(D == 0 || A == B || A == C || A == D || B ==C || B == D || C == D) continue;
		num2 = D * 1000 + C * 100 + B * 10 + A;
		for(E = 2; E<=9; ++E){
			if(E == A || E == B || E == C || E == D) continue;
			if(num1 * E == num2){
				cout << num1 << "*" << E << "=" << num2 << endl;
			}
		} 
	}
	return 0;
}

在这里插入图片描述

例4.13

阶梯问题:有一个长阶梯,若每步上两个台阶,最后剩一阶。若每步上3阶,最后剩2阶。若每步上5阶,最后剩4阶。若每步上6阶,最后剩5阶。每步上7阶,最后正好1阶都不剩,寻找改楼梯至少有多少阶
思路:只需要找到一个数n满足除以2余1,除以3余2,除以5余4,除以7余0,而且台阶至少7阶以上,且能整除7,所以可以枚举7的倍数,找到第一个满足条件的n

#include <iostream>
using namespace std;

int main(){
	int n;
	
	for(n = 7; ; n+=7){
		if(n % 2 == 1 && n % 3 == 2 && n % 5 == 4 && n % 6 == 5 && n % 7 == 0){
			cout << "满足条件的台阶数至少为" << n << endl;
			break;
		}
	}
	return 0;
}

在这里插入图片描述

例4.14

用150元买了3种水果,各水果加起来一共100个,西瓜10一个,苹果3元一个,橘子1元1个。设计一个程序,输出每种水果各买了几个

#include <iostream>
using namespace std;

int main(){
	int mellon, apple, orange;
	
	for(mellon=1; mellon<15; ++mellon){
		for(apple=1; apple <= (150-10*mellon)/3; ++apple){
			orange = 150-10*mellon -3*apple;
			if(mellon+apple+orange == 100){
				cout << "mellon:" << mellon << ' ';
				cout << "apple:" << apple << ' ';
				cout << "orange:" << orange << endl;
			}
		}
	}
	return 0;
} 

在这里插入图片描述

例4.15

用贪婪法解硬币找零问题
假设有一种货币,有面值1分、2分、5分和1角的的硬币,最少需要多少个硬币,最少需要多少个硬币来找出K分钱的零钱。

#include <iostream>
using namespace std;

#define ONEFEN 1
#define TWOFEN 2
#define FIVEFEN 5
#define ONEJIAO 10

int main(){
	int money;
	int onefen = 0, twofen = 0, fivefen = 0, onejiao = 0;
	
	cout << "输入要找零的钱(以分为单位):";
	cin >> money;
	
	//不断尝试每一种硬币
	if(money >= ONEJIAO){
		onejiao = money / ONEJIAO;
		money %= ONEJIAO;
	}
	if(money >= FIVEFEN){
		fivefen = 1;
		money -= FIVEFEN;
	} 
	
	if(money >= TWOFEN){
		twofen = money / TWOFEN;
		money -= TWOFEN;
	}
	
	if(money >= ONEFEN){
		onefen = 1;
	}
	
	cout << "1角硬币数:" << onejiao << endl;
	cout << "5分硬币数:" << fivefen <<endl;
	cout << "2分硬币数:" << twofen << endl;
	cout << "1分硬币数:" << onefen << endl;
	
	return 0;
} 

在这里插入图片描述

例4.16

给定一组不重复的个位数组成的数字,例如 5、6、2、9、4、1,找出由其中3个数字组成的最大的3位数

#include <iostream>
using namespace std;

int main(){
	int num = 0, max = 10, current;
	for(int digit = 100;digit > 0; digit /=10){
		current = 0;
		for(int n:{5,6,2,4,9,1}){
			if(n > current && n < max)
				current = n;
		}
		num+=digit * current;
		max = current;
	}
	cout << num << '\t';
	
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值