趣味100题

【程序1】1~4组成无重复数字的三位数。
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;

int main()
{
	int cnt = 0;
	for(int i=1;i<5;i++)
		for(int j=1;j<5;j++)
			for (int k = 1; k < 5; k++)
			{
				if (i != j && i != k && j != k) {
					cnt++;
					cout << i << j << k << " ";
					if (cnt % 5 == 0)
						cout << endl;
				}
			}
	cout << endl;
	return 0;
}

在这里插入图片描述
【程序3】加100是完全平方数
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;

int main()
{
	
	for (int i = 1; i < 10000; i++)
	{
		int x = sqrt(i + 100);
		int y = sqrt(i + 168);
		if ((x*x == (i + 100)) && (y*y == (i + 168)))
		{
			cout<<i;
		}
	}
	cout << endl;
	return 0;
}

在这里插入图片描述
【程序4】年月日判断是年份的第几天。
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
情况,闰年且输入月份大于3时需考虑多加一天。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<ctime>
using namespace std;

bool fun(int y)
{
	if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
		return true;
	return false;
}
int main()
{
	int tab[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int year, month, day, days = 0;
	cin >> year >> month >> day;
	if (fun(year) && month > 2)
		days++;
	for (int i = 1; i < month; i++)
		days += tab[i];
	days += day;
	cout << year << "/" << month << "/" << day << "是该年的第" << days << "天" << endl;
	return 0;
}

在这里插入图片描述
【程序5】三个数,从小到大输出。
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,
然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int a, b, c;
	 cin >> a >> b >> c;
	 if (a > b)
		 swap(a, b);
	 if (a > c)
		 swap(a, c);
	 if (b > c)
		 swap(b, c);
	 cout << "从小到大排序:" << endl;
	 cout << a << setw(6) << b << setw(6) << c << endl;
	return 0;
}

在这里插入图片描述
【程序8】题目:输出9*9口诀。


#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 for (int i = 1; i <= 9; i++) {
		 for (int j = 1; j <= i; j++)
			 cout << i << "*" << j << "=" << i * j << "  ";
		 cout << endl;
	 }	 
	return 0;
}

在这里插入图片描述
【程序11】(古典问题)兔子问题。
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21…

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

int fun(int n)
{
	if (n == 1 || n == 2)
		return 1;
	else
		return fun(n - 1) + fun(n - 2);
}

 int main()
{
	 int cnt = 0;
	 for (int i = 1; i <= 20; i++)
	 {
		 cnt++;
		 cout << fun(i) << '\t';
		 if (cnt % 5 == 0)
			 cout << endl;
	 }
	return 0;
}

在这里插入图片描述
【程序12】判断101到200之间的素数。
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

bool isprime(int n)
{
	for (int i = 2; i <= sqrt(n); i++)
		if (n%i == 0)
			return false;
	return true;
}

 int main()
{
	 int cnt = 0;
	 for (int i = 101; i <= 200; i++)
		 if (isprime(i)) {
			 cout << i << '\t';
			 cnt++;
			 if (cnt % 5 == 0)
				 cout << endl;
		 }
	return 0;
}

在这里插入图片描述
【程序13】打印出所有的“水仙花数”。
题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

bool fun(int n)
{
	int i = n % 10;//个位数
	int j = n / 10 % 10;//十位数
	int k = n / 100;//百位数
	if (n == (i*i*i + j * j*j + k * k*k))
		return true;
	return false;
}

 int main()
{
	 int cnt = 0;
	 for (int i = 100; i <= 999; i++)
		 if (fun(i)) {
			 cout << i << '\t';
			 cnt++;
			 if (cnt % 5 == 0)
				 cout << endl;
		 }
	return 0;
}

在这里插入图片描述
题目:将一个正整数分解质因数。例如:输入90,打印出90=233*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
	int i,n;
	cout<<"输入一个数n:"<<endl;
	cin>>n;
	cout<<n<<"=";
	for(i=2;i<=n;i++)
		while(n!=i)
		{
			if(n%i==0)
			{
				cout<<i<<"*";
				n=n/i;
			}
			else
				break;
		}
	cout<<n<<endl;
	return 0;
}

在这里插入图片描述
【程序22】两个乒乓球队进行比赛
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定
比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出
三队赛手的名单。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 for(char a='x';a<='z';a++)
		 for (char b = 'x'; b <= 'z'; b++)
		 {
			 if (b != a)
				 for (char c = 'x'; c <= 'z'; c++)
					 if (c != b && c != a)
						 if (a != 'x'&&c != 'x'&&c != 'z') {
							 cout << "a--" << a << endl;
							 cout << "b--" << b << endl;
							 cout << "c--" << c << endl;
						 }
		 }
	return 0;
}

在这里插入图片描述
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int sum = 1;
	 for (int i = 1; i < 10; i++)
	 {
		 sum = (sum + 1) * 2;
	 }
	 cout <<"桃子总数"<< sum << endl;
	return 0;
}

在这里插入图片描述
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在
第10次落地时,共经过多少米?第10次反弹多高?

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 double sum = 0, h = 100;
	 for (int i = 1; i <= 10; i++)
	 {
		 sum += h + h / 2;
		 h = h / 2;
	 }
	 cout << "第10次落地经过:" << sum-h << "米  " << "第10次反弹" << h << "米" << endl;
	return 0;
}

在这里插入图片描述
【程序19】完数。
题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
找出1000以内的所有完数

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

bool fun(int n)
{
	int sum = 0;
	for (int i = 1; i < n; i++)
		if (n%i == 0)
			sum += i;
	if (n == sum)
		return true;
	return false;
}
 int main()
{
	 int n, cnt = 0;
	 for(int i=6;i<=1000;i++)
		 if (fun(i))
		 {
			 cnt++;
			 cout << i << ' ';
			 if (cnt % 5 == 0)
				 cout << endl;
		 }
	return 0;
}

在这里插入图片描述
【程序18】求s=a+aa+aaa+aaaa+aa…a的值
题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;


 int main()
{
	 int n, a;
	 cin >> a >> n;
	 int sum = 0;
	 int t=a;
	 for (int i = 1; i <= n; i++)
	 {
		 sum += t;
		 t = t * 10 + a;
	 }
	 cout << "a+aa+aaa+...+aaa...aaa=" << sum << endl;
	return 0;
}

在这里插入图片描述
程序17】输入一行字符,分别统计出其中英文字母、、、、的个数。
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;


 int main()
{
	 string s;
	 getline(cin, s);
	 int digit = 0, space = 0, letter = 0, other = 0;
	 for (int i = 0; i < s.size(); i++)
	 {
		 if (s[i] == ' ')
			 space++;
		 else if (s[i] >= '1'&&s[i] <= '9')
			 digit++;
		 else if (s[i] >= 'a'&&s[i] <= 'z' || s[i] >= 'A'&&s[i] <= 'Z')
			 letter++;
		 else
			 other++;
	 }
	 cout << "数字:" << digit << endl;
	 cout << "字母:" << letter << endl;
	 cout << "空格:" << space << endl;
	 cout << "其他:" << other << endl;
	return 0;
}

在这里插入图片描述
【程序16】求最大公约数和最小公倍数。
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。


#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

int gcd(int a, int b)
{
	if (a%b == 0)
		return b;
	else
		return gcd(b, a%b);
}
 int main()
{
	 int a, b;
	 cin >> a >> b;
	 cout << "最大公约数:" << gcd(a, b) << endl << "最小公倍数:" << a * b / gcd(a, b) << endl;
	return 0;
}

在这里插入图片描述
输出一个菱形
在这里插入图片描述

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int n;
	 cin >> n;
	 for (int i = 1; i <= n; i++)
	 {
		 for (int k = n - i; k >= 1; k--)
			 cout << ' ';
		 for (int j = 1; j <= i * 2 - 1; j++)
			 cout << "*";
		 cout << endl;
	 }
	 for (int i = 1; i <n; i++)
	 {
		 for (int k = 1; k <= i; k++)
			 cout << " ";
		 for (int j = (n - i) * 2 - 1; j >= 1; j--)
			 cout << "*";
		 cout << endl;
	 }
	return 0;
}

在这里插入图片描述
【程序24】 求数列的前20项和,2/1,3/2,5/3,8/5,、、
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 double f1=1, f2=2,t;
	 double sum = 0;
	 for (int i = 1; i <= 20; i++)
	 {
		 sum += f2 / f1;
		 t = f2;
		 f2 = f1 + f2;
		 f1 = t;
	 }
	 cout << "前20项之和为:" << sum << endl;
	return 0;
}

在这里插入图片描述
【程序25】 题目:求1+2!+3!+…+20!和

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

long fun(int n)
{
	if (n == 1)
		return 1;
	else
		return n * fun(n - 1);
}
 int main()
{
	 long sum = 0;
	 for (int i = 1; i <= 20; i++)
		 sum += fun(i);
	 cout <<"1+2!+3!+...+20!="<< sum << endl;
	return 0;
}

在这里插入图片描述
【程序26】 题目:利用递归方法求5!。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

long fun(int n)
{
	if (n == 1)
		return 1;
	else
		return n * fun(n - 1);
}
 int main()
{
	 cout << "5!="<<fun(5) << endl;
	return 0;
}

在这里插入图片描述
【程序27】利用递归函数调用方式,反向打印字符。
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	void palin(int n);
	int i=5;
	palin(i);
	cout<<endl;
	return 0;
}
void palin(int n)
{
	char next;
	if(n<=1)
	{
		next=getchar();
		cout<<next;
	}
	else
	{
		next=getchar();
		palin(n-1);
		putchar(next);
	}
}

在这里插入图片描述
【程序28】 五个人问岁数。
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第
3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后
问第一个人,他说是10岁。请问第五个人多大?

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

int fun(int n)
{
	if (n == 1)
		return 10;
	else
		return fun(n - 1) + 2;
}
 int main()
{
	 cout << fun(5) << endl;
	return 0;
}

在这里插入图片描述
【程序29】 不多于5位数,求是几位数。
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

void fun(int n)
{
	int cnt = 0;
	while (n)
	{
		cout << n % 10 << " ";
		cnt++;
		n = n / 10;
	}
	cout << "位数:" << cnt << endl;
}
 int main()
{
	 int n;
	 cin >> n;
	 fun(n);
	return 0;
}

在这里插入图片描述
【程序30】一个5位数,判断它是不是回文数。
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

bool fun(char *a)
{
	int n = strlen(a);
	for (int i = 0; i < n / 2; i++)
	{
		if (a[i] != a[n - i - 1])
			return false;
	}
	return true;
}
 int main()
{
	 char a[20];
	 cin >> a;
	 if (fun(a))
		 cout << "是回文!" << endl;
	 else
		 cout << "不是回文!" << endl;
	return 0;
}

在这里插入图片描述
【程序36】题目:求100之内的素数

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

bool fun(int n)
{
	for (int i = 2; i <=sqrt(n); i++)
		if (n%i == 0)
			return false;
	return true;
}
 int main()
{
	 int cnt = 0;
	 for(int i=2;i<=100;i++)
		 if (fun(i)) {
			 cout << i << " ";
			 cnt++;
			 if (cnt % 5 == 0)
				 cout << endl;
		 }
	return 0;
}

在这里插入图片描述
【程序37】题目:对10个数进行排序
方法一:冒泡排序:

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

void fun(int a[],int n)
{
	int t;
	for(int i=1;i<n;i++)
		for(int j=0;j<n-i;j++)
			if (a[j] > a[j + 1])
			{
				t = a[j];
				a[j] = a[j + 1];
				a[j + 1] = t;
			}
}
 int main()
{
	 int a[100];
	 for (int i = 0; i < 10; i++)
		 cin >> a[i];
	 fun(a, 10);
	 for (int i = 0; i < 10; i++)
		 cout << a[i] << ' ';
	 cout << endl; 
	return 0;
}

在这里插入图片描述
方法二:选择排序

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

void fun(int a[],int n)
{
	int t;
	for (int i = 0; i < n-1; i++)
	{
		int k = i;
		for (int j = i + 1; j < n; j++)
			if (a[k] > a[j])
				k = j;
		t = a[i];
		a[i] = a[k];
		a[k] = t;
	}
}
 int main()
{
	 int a[100];
	 for (int i = 0; i < 10; i++)
		 cin >> a[i];
	 fun(a, 10);
	 for (int i = 0; i < 10; i++)
		 cout << a[i] << ' ';
	 cout << endl; 
	return 0;
}

在这里插入图片描述
【程序38】题目:求一个3*3矩阵对角线元素之和

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int a[3][3], sum = 0;
	 for (int i = 0; i < 3; i++)
		 for (int j = 0; j < 3; j++)
		 {
			 cin >> a[i][j];
			 if (i == j)
				 sum += a[i][j];
		 }
	 cout << sum << endl;
	return 0;
}

在这里插入图片描述
【程序39】数组,插入数据。
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

  1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后
    此元素之后的数,依次后移一个位置。
// ConsoleApplication10.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

void sort(int a[], int n)
{
	int t;
	for (int i = 0; i < n - 1; i++) {
		int k = i;
		for (int j = i + 1; j < n; j++)
		{
			if (a[k] > a[j])
				k = j;
			if (k != i)
			{
				t = a[k];
				a[k] = a[i];
				a[i] = t;
			}
		}
	}
}
 int main()
{
	 int a[100];
	 int n;
	 cout << "输入数组个数:" << endl;
	 cin >> n;
	 cout << "输入具体数据:" << endl;
	 for (int i=0; i < n; i++)
		 cin >> a[i];
	 cout << "输入你要插入的数:" << endl;
	 cin >> a[n];
	 sort(a, a + n + 1);
	 for (int i = 0; i < n + 1; i++)
		 cout << a[i] << " ";
	 cout << endl;
	return 0;
}



在这里插入图片描述
【程序40】题目:将一个数组逆序输出。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

void fun(int a[], int n)
{
	for (int i = n - 1; i >= 0; i--)
		cout << a[i] << " ";
	cout << endl;
}
 int main()
{
	 int a[100];
	 int n;
	 cout << "输入数组个数:" << endl;
	 cin >> n;
	 cout << "输入具体数据:" << endl;
	 for (int i=0; i < n; i++)
		 cin >> a[i];
	 cout << "逆序:" << endl;
	 fun(a, n);
	return 0;
}


在这里插入图片描述
【程序41】题目:学习static定义静态变量的用法

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

void fun()
{
	int a = 0;
	static int s_a = 0;
	cout << "a=" << a << endl;
	cout << "s_a=" << s_a << endl;
	a++;
	s_a++;
}
 int main()
{
	 for (int i = 0; i < 3; i++)
		 fun();
	return 0;
}

在这里插入图片描述
【程序61】题目:打印出杨辉三角形(要求打印出10行如下图)

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int a[10][10];
	 for (int i = 0; i < 10; i++)
	 {
		 a[i][i] = 1;
		 a[i][0] = 1;
	 }
	 for (int i = 2; i < 10; i++)
		 for (int j = 1; j < i; j++)
			 a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
	 for (int i = 0; i < 10; i++) {
		 for (int j = 0; j <= i; j++)
			 cout << a[i][j] << ' ';
		 cout << endl;
	 }
	return 0;
}

在这里插入图片描述
【程序67】数组,最大的与第一个交换,最小的与最后一个交换。


#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int a[100], n;
	 cout << "输入数组个数:" << endl;
	 cin >> n;
	 cout << "输入具体数据:" << endl;
	 for (int i = 0; i < n; i++)
		 cin >> a[i];
	 int min = a[0], max = a[0], x=0, y=0;
	 for (int i = 0; i < n; i++)
	 {
		 if (max < a[i]) {
			 max = a[i];
			 x = i;
		 }
		 if (min > a[i]) {
			 min = a[i];
			 y = i;
		 }
	 }
	 //换位置
	 int t = a[0];
	 a[0] = a[x];
	 a[x] = t;

	 t = a[n - 1];
	 a[n - 1] = a[y];
	 a[y] = t;
	 for (int i = 0; i < n; i++)
		 cout << a[i] << " ";
		 cout << endl;
	return 0;
}

在这里插入图片描述
【程序68】数组,后移。

main() 
{ 
int number[20],n,m,i; 
printf("the total numbers is:"); 
scanf("%d",&n); 
printf("back m:"); 
scanf("%d",&m); 
for(i=0;i<n-1;i++) 
scanf("%d,",&number[i]); 
scanf("%d",&number[n-1]); 
move(number,n,m); 
for(i=0;i<n-1;i++) 
printf("%d,",number[i]); 
printf("%d",number[n-1]); 
} 
move(array,n,m) 
int n,m,array[20]; 
{ 
int *p,array_end; 
array_end=*(array+n-1); 
for(p=array+n-1;p>array;p--) 
*p=*(p-1); 
*array=array_end; 
m--; 
if(m>0) move(array,n,m); 
} 

【程序69】n个人,报数,去3,剩下几号。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int a[100] = { 0 };
	 int n;
	 cin >> n;
	 int i;
	 for (i = 0; i < n; i++)
		 a[i] = i + 1;
	 i = 0;
	 int cnt = 0,temp=n;
	 while (n > 1)
	 {
		 if (a[i] != 0)
			 cnt++;
		 if (cnt == 3)
		 {
			 a[i] = 0;
			 cnt = 0;
			 n--;
		 }
		 i++;
		 if (i == temp)
			 i = 0;
	 }
	 for (int i = 0; i < temp; i++)
		 if (a[i]!= 0)
			 cout << a[i]<< endl;
	return 0;
}

在这里插入图片描述
【程序70】求字符串的长度。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

int len(char *s)
{
	int cnt = 0;
	while (*s)
	{
		cnt++;
		s++;
	}
	return cnt;
}
 int main()
{
	 char a[100];
	 cin.getline(a, 100);
	 cout << "字符串长度:" << len(a) << endl;
	return 0;
}

在这里插入图片描述
【程序71】题目:编写input()和output()函数输入,输出5个学生的数据记录。

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

struct Student{
	string num;
	string name;
	float score;
}stu[5];
void input()
{
	cout << "输入学生信息:学号 姓名 成绩" << endl;
	for (int i = 0; i < 5; i++)
		cin >> stu[i].num >> stu[i].name >> stu[i].score;
}
void output()
{
	cout << "输出学生信息:学号 姓名 成绩" << endl;
	for (int i = 0; i < 5; i++)
		cout << stu[i].num << " " << stu[i].name << " " << stu[i].score << endl;
}
 int main()
{
	 input();
	 output();
	return 0;
}

在这里插入图片描述
程序76】偶数求1/2+1/4+…+1/n

#include <iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;

 int main()
{
	 int n;
	 cin >> n;
	 double sum = 0, t = 0;
	 for (int i = 2; i <= n; )
	 {
		 t = 1 / double(i);
		 sum += t;
		 i += 2;
	 }
	 cout << sum << endl;
	return 0;
}

在这里插入图片描述
【程序78】题目:找到年龄最大的人,并输出。

#include<iostream>
#include<string>
using namespace std;
const int n = 3;
struct man {
	string name;
	int age;
}person[n] = { "lili",23,"ming",12,"lijin",20 };
int main()
{
	struct man *p, *q;
	int i;
	p = person;
	int m = p->age;
	for (int i = 0; i < n; i++,p++)
	{
		if (m < p->age)
		{
			m = p->age;
		}
	}
	cout << "最大年龄:" << m << endl;
	return 0;
}

在这里插入图片描述
【程序79】题目:字符串排序。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	void compare(string &a,string &b,string &c);
	string a,b,c;
	cout<<"输入第1个字符串"<<endl;
	cin>>a;
	cout<<"输入第2个字符串"<<endl;
	cin>>b;
	cout<<"输入第3个字符串"<<endl;
	cin>>c;
	compare(a,b,c);
	cout<<"排序后的字符串。。。。。"<<endl;
	cout<<a<<endl<<b<<endl<<c<<endl;
	return 0;
}
void compare(string &a,string &b,string &c)
{
	void change(string &,string &);
	if(a>b)change(a,b);
	if(a>c)change(a,c);
	if(b>c)change(b,c);
}
void change(string &a,string &b)
{
	string temp;
	temp=a;
	a=b;
	b=temp;
}

在这里插入图片描述
【程序80】猴子分桃子。
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

#include<iostream>
#include<string>
using namespace std;
int main() 
{
	int i,m,j,k,count; 
	for(i=4;i<10000;i+=4) 
	{
		count=0; 
		m=i; 
		for(k=0;k<5;k++) 
		{ 
			j=i/4*5+1; 
			i=j; 
			if(j%4==0) 
				count++; 
			else 
				break; 
		} 
		i=m; 
		if(count==4) 
		{
			printf("%d\n",count); 
			break;
		} 
	} 
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值