基础编程练习(五)

7-41 三天打鱼两天晒网 (15point(s))
中国有句俗语叫“三天打鱼两天晒网”。假设某人从某天起,开始“三天打鱼两天晒网”,问这个人在以后的第N天中是“打鱼”还是“晒网”?

输入格式:
输入在一行中给出一个不超过1000的正整数N。

输出格式:
在一行中输出此人在第N天中是“Fishing”(即“打鱼”)还是“Drying”(即“晒网”),并且输出“in day N”。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	int n;
	cin>>n;
	if(n % 5 - 3 > 0 || n % 5 == 0)
	{
		cout<<"Drying in day "<<n;
	}
	else{
		cout<<"Fishing in day "<<n;
	}
} 

7-42 韩信点兵 (10point(s))
在中国数学史上,广泛流传着一个“韩信点兵”的故事:韩信是汉高祖刘邦手下的大将,他英勇善战,智谋超群,为汉朝建立了卓越的功劳。据说韩信的数学水平也非常高超,他在点兵的时候,为了知道有多少兵,同时又能保住军事机密,便让士兵排队报数:

按从1至5报数,记下最末一个士兵报的数为1;
再按从1至6报数,记下最末一个士兵报的数为5;
再按从1至7报数,记下最末一个士兵报的数为4;
最后按从1至11报数,最末一个士兵报的数为10;
请编写程序计算韩信至少有多少兵。

输入格式:
本题无输入

输出格式:
输出韩信至少拥有的士兵人数。

#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

int main()
{
	cout<<2111; //这里我是手算的,这里请自行参考中国剩余定理
} 

7-43 一帮一 (15point(s))
“一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。本题就请你编写程序帮助老师自动完成这个分配工作,即在得到全班学生的排名后,在当前尚未分组的学生中,将名次最靠前的学生与名次最靠后的异性学生分为一组。

输入格式:
输入第一行给出正偶数N(≤50),即全班学生的人数。此后N行,按照名次从高到低的顺序给出每个学生的性别(0代表女生,1代表男生)和姓名(不超过8个英文字母的非空字符串),其间以1个空格分隔。这里保证本班男女比例是1:1,并且没有并列名次。

输出格式:
每行输出一组两个学生的姓名,其间以1个空格分隔。名次高的学生在前,名次低的学生在后。小组的输出顺序按照前面学生的名次从高到低排列。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin>>n;
	vector<int> num;
	vector<string> w_name;
	vector<string> m_name;
	vector<string> name;
	for(int i = 0; i < n; i ++)
	{
		string str;
		int a;
		cin>>a>>str;
		name.push_back(str);
		num.push_back(a);
		if(a == 0)
		{
			w_name.push_back(str);	
		}else{
			m_name.push_back(str);
		}
	}
	for(int i = 0; i < n / 2; i ++)
	{
		cout<<name[i]<<" ";
		if(num[i] == 0)
		{
			cout<<m_name[m_name.size() - 1]<<endl;
			m_name.pop_back();
		}else{
			cout<<w_name[w_name.size() - 1]<<endl;
			w_name.pop_back();
		}
	}	
	
} 

7-44 猴子吃桃问题 (15point(s))
一只猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半加一个。到第N天早上想再吃时,见只剩下一个桃子了。问:第一天共摘了多少个桃子?

输入格式:
输入在一行中给出正整数N(1<N≤10)。

输出格式:
在一行中输出第一天共摘了多少个桃子。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin>>n;
	int sum = 1;
	for(int i = 0; i < n - 1; i ++)
	{
		sum = (sum + 1) * 2;
	}
	cout<<sum;
	
} 

7-45 数字加密 (15point(s))
输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。例如输入1257,经过加9取余后得到新数字0146,再经过两次换位后得到4601。

输入格式:
输入在一行中给出一个四位的整数x,即要求被加密的数。

输出格式:
在一行中按照格式“The encrypted number is V”输出加密后得到的新数V。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	string str;
	cin>>str;
	for(int i = 0; i < 4; i ++)
	{
		str[i] = char((str[i] - '0' + 9) % 10 + '0');
	}
	cout<<"The encrypted number is "<<str[2]<<str[3]<<str[0]<<str[1];
	
} 

7-46 人民币兑换 (15point(s))
1元5角钱人民币兑换5分、2分和1分的硬币(每一种都要有)共100枚,会有很多种兑换方案。请编写程序给出各种兑换方案。

输入格式:
输入为一个正整数n,表示要求输出前n种可能的方案。方案的顺序,是按照5分硬币从少到多排列的。

输出格式:
显示前n种方案中5分、2分、1分硬币各多少枚。每行显示一种方案,数字之间空一格,最后一个数字后没有空格。

注意:如果全部方案不到n种,就顺序输出全部可能的方案。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin>>n;
	int count = 1;
	for(int i = 1; i < 30; i ++)
	{
		for(int j = 0; j < 75 ;j ++)
		{
			for(int k = 0; k <= 100 - i - j; k ++)
			{
				if(5*i + 2*j + k == 150 && i + j + k == 100)
				{
					if(count <= n)
					{
						count++;
						cout<<i<<" "<<j<<" "<<k<<endl;
					}
				}
			}
		}
	}
} 

7-47 简化的插入排序 (15point(s))
本题要求编写程序,将一个给定的整数插到原本有序的整数序列中,使结果序列仍然有序。

输入格式:
输入在第一行先给出非负整数N(<10);第二行给出N个从小到大排好顺序的整数;第三行给出一个整数X。

输出格式:
在一行内输出将X插入后仍然从小到大有序的整数序列,每个数字后面有一个空格。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	int n;	
	cin>>n;
	int *p = new int[n + 1];
	for(int i = 0; i <= n; i ++)
	{
		cin>>p[i];
	}
	sort(p, p+n+1);
	for(int i = 0; i <= n; i ++)
	{

			cout<<p[i]<<" ";


	}
} 

7-48 藏头诗 (15point(s))
本题要求编写一个解密藏头诗的程序。

输入格式:
输入为一首中文藏头诗,一共四句,每句一行。注意:一个汉字占两个字节。

输出格式:
取出每句的第一个汉字并连接在一起形成一个字符串并输出。同时在末尾输入一个换行符。

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
	string str[4];
	for(int i = 0; i < 4; i ++)
	{
		cin>>str[i];
	}
	for(int i = 0; i < 4; i ++)
	{
		cout<<str[i][0]<<str[i][1];
	}
} 

7-49 复数四则运算 (15point(s))
本题要求编写程序,计算2个复数的和、差、积、商。

输入格式:
输入在一行中按照a1 b1 a2 b2的格式给出2个复数C1=a1+b1i和C2=a2+b2i的实部和虚部。题目保证C2不为0。

输出格式:
分别在4行中按照(a1+b1i) 运算符 (a2+b2i) = 结果的格式顺序输出2个复数的和、差、积、商,数字精确到小数点后1位。如果结果的实部或者虚部为0,则不输出。如果结果为0,则输出0.0。

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
double a1,b1,a2,b2;
void Judge_op(string&,string&);
void Output(double,double);
void Plus();
void Minus();
void Multiply();
void Divide();
int main()
{
	cin>>a1>>b1>>a2>>b2;
	string op1,op2;
	Judge_op(op1,op2);
	cout<<setiosflags(ios::fixed)<<setprecision(1)<<'('<<a1<<op1<<b1<<"i) "<<"+ ("<<a2<<op2<<b2<<"i) = ";
	Plus();cout<<'\n';
	cout<<setiosflags(ios::fixed)<<setprecision(1)<<'('<<a1<<op1<<b1<<"i) "<<"- ("<<a2<<op2<<b2<<"i) = ";
	Minus();cout<<'\n';
	cout<<setiosflags(ios::fixed)<<setprecision(1)<<'('<<a1<<op1<<b1<<"i) "<<"* ("<<a2<<op2<<b2<<"i) = ";
	Multiply();cout<<'\n';
	cout<<setiosflags(ios::fixed)<<setprecision(1)<<'('<<a1<<op1<<b1<<"i) "<<"/ ("<<a2<<op2<<b2<<"i) = ";
	Divide();cout<<'\n';
	return 0;
}
void Judge_op(string &op1,string &op2)
{
	if(b1>=0)
	op1="+";
	else
	op1="";
	if(b2>=0)
	op2="+";
	else
	op2="";
}
void Plus()
{
	double real=a1+a2;
	double imag=b1+b2;
	Output(real,imag);
}
void Minus()
{
	double real=a1-a2;
	double imag=b1-b2;
	Output(real,imag);
}
void Multiply()
{
	double real=a1*a2-b1*b2;
	double imag=a1*b2+a2*b1;
	Output(real,imag);
}
void Divide()
{
	double deno=a2*a2+b2*b2;
	double real=(a1*a2+b1*b2)/deno;
	double imag=(a2*b1-a1*b2)/deno;
	Output(real,imag);
}
void Output(double real,double imag)
{
	if((int)(real*10)==0)
	{
		if((int)(imag*10)==0)
		cout<<0.0;
		else
		cout<<setiosflags(ios::fixed)<<setprecision(1)<<imag<<'i';
	}
	else if((int)(imag*10)==0)
	cout<<setiosflags(ios::fixed)<<setprecision(1)<<real;
	else
	{
		cout<<setiosflags(ios::fixed)<<setprecision(1)<<real;
		if(imag>=0)
		cout<<'+';
		cout<<setiosflags(ios::fixed)<<setprecision(1)<<imag<<'i';
	}
}

7-50 整数的分类处理 (20point(s))
给定 N 个正整数,要求你从中得到下列三种计算结果:

A1 = 能被 3 整除的最大整数
A2 = 存在整数 K 使之可以表示为 3K+1 的整数的个数
A3 = 存在整数 K 使之可以表示为 3K+2 的所有整数的平均值(精确到小数点后 1 位)
输入格式:
输入首先在第一行给出一个正整数 N,随后一行给出 N 个正整数。所有数字都不超过 100,同行数字以空格分隔。

输出格式:
在一行中顺序输出 A1、A2、A3的值,其间以 1 个空格分隔。如果某个数字不存在,则对应输出NONE。

#include<vector>
#include<iomanip>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
	vector<int>a1;	
	vector<int>a2;	
	vector<int>a3;
	int n;
	cin>>n;
	int sum = 0;
	for(int i = 0; i < n; i ++)
	{
		int a;
		cin>>a;
		if(a % 3 == 0)
		{
			a1.push_back(a);
		}
		if(a % 3 == 1)
		{
			a2.push_back(a);
		}
		if(a % 3 == 2)
		{
			a3.push_back(a);
			sum += a;
		}
		
	}
	sort(a1.begin(), a1.end());
	if(a1.empty())
	{
		cout<<"NONE"<<" ";
	}else{
		cout<<a1[a1.size() - 1]<<" ";
	}
	if(a2.empty())
	{
		cout<<"NONE"<<" ";
	}else{
		cout<<a2.size()<<" ";
	}
	if(a3.empty())
	{
		cout<<"NONE";
	}else{
		cout<<fixed<<setprecision(1)<<float(sum)/a3.size();
	}
	
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值