CPP----C++练习100题

CPP----Q100


1.从键盘读入2个整数,分别代表一个长方形的长和宽,请计算长方形的周长和面积;

#include <bits/stdc++.h> 
using namespace std;
int main(){
	int chang,kuan;
	cin>>chang>>kuan;
	cout<<(chang+kuan)*2<<endl;
	cout<<(chang*kuan)<<endl;
	return 0;
} 

2.已知一个圆的半径,求解该圆的面积和周长

#include <bits/stdc++.h> 
using namespace std;
int main(){
	const double pi = 3.1415926; 
	double circle;
	cin>>circle;
//	cout<<pi*(circle*circle)<<endl;
//	cout<<(pi*circle)*2<<endl;
    printf("%.2lf\n",pi*circle*circle);
    printf("%.2lf",pi*circle*2);
	return 0;
} 

3.有一块n \times nn×n(n≥5n≥5,且 nn 是奇数)的红玫瑰花圃,由 n \times nn×n 个小正方形花圃组成,现要求在花圃中最中间的一行、最中间的一列以及 44 个顶点处种植粉色玫瑰,请问粉玫瑰占地面积占整个玫瑰花圃总面积的百分比是多少?

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	int s = n*n;
	int pink_area = 2 * n + 3;
//	cout << s << pink_area<<endl;
	printf("%.1f%%",pink_area*1.0/s*100);
	return 0;
} 

4.编一程序,将摄氏温度换为华氏温度。公式为:f=9/5*c+32f=9/5∗c+32 。其中 ff 为华氏温度, cc 是摄氏温度。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	double f = 9*1.0/5* n + 32;
	printf("%.2f",f);
	return 0;
} 

5.任意读入一个四位整数,颠倒后输出。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int num,g,s,b,q;
	cin >> num;
	g = num%10;
	s = num/10%10;
	b = num/100%10;
	q = num/1000;
	printf("%d",1000*g+100*s+10*b+q);
	return 0;
} 

6.对于一个任意的三位自然数 xx ,编程计算其各个数位上的数字之和 SS 。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int num,g,s,b;
	cin >> num;
	g = num%10;
	s = num/10%10;
	b = num/100%10;
	printf("%d",g+s+b);
	return 0;
} 

7.某军事单位用 44 位整数来传递信息,传递之前要求先对这个 44 位数进行加密。加密的方式是每一位都先加上 55 然后对 1010 取余数,再将得到的新数颠倒过来。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int num,g,s,b,q;
	cin >> num;
	g = (num%10+5)%10;
	s = (num/10%10+5)%10;
	b = (num/100%10+5)%10;
	q = (num/1000+5)%10; 
	printf("%d",1000*g+100*s+10*b+q);
	return 0;
} 

8.输入一个整数,判断是否为偶数。是输出 y e s ,否则输出n o。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	if (n % 2 == 0)
	    printf("y e s");
	else
		printf("n o");
	return 0;
} 

9 有 AA,BB 两个不相等的数,请将其中较大数打印出来。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m,n;
	cin >> m >> n;
	if (m >= n)
		cout<< m <<endl;
	else
	    cout << n << endl;
	return 0;
} 

10 请从键盘读入一个三位整数,判断这个三位整数是否是对称数(对称数指的是,这个数正过来和倒过来是同一个数,比如:121、686、808121、686、808 等数都是对称数),如果是对称数,则输出“Y” ,否则输出“N” 。(请注意字母的大小写)。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m;
	cin >> m;
	int g = m %10;
	int s = m/10%10;
	int b = m/100;
//	cout << g << endl << b<< endl;
//	if (m == 100*g+s*10+b)
	if (g == b)
		cout<< "Y";
	else
	    cout << "N";
	return 0;
} 

11 请从键盘读入一个六位整数,判断这个六位整数是否是对称数(对称数指的是,这个数正过来和倒过来是同一个数。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m;
	cin >> m;
	int g = m %10;
	int s = m/10%10;
	int b = m/100%10;
	int q = m/1000%10;
	int w = m/10000%10;
	int sw = m/100000;
//	cout << g << endl << b<< endl;
//	if (m == 100*g+s*10+b)
	if (g == sw and s == w and b == q)
		cout<< "Y";
	else
	    cout << "N";
	return 0;
} 

12 超市有一门公共电话,收费标准是,如果通话时间在 1010 分钟内,那么按照 1.5元/1.5元/ 分钟收费,如果通话时间超过 1010 分钟(含 1010 分钟)按照 1.2元/1.2元/ 分钟收费

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m;
	cin >> m;
	if (m < 10)
		printf("%.1lf",(m*1.5));
	else
	    printf("%.1lf",(m*1.2));
	return 0;
} 

13 输入三个整数,表示3条线段的长度,判断这三条线段能否构成三角形。能构成就输出 Yes ,否则输出No

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m,n,x;
	cin >> m >> n>>x;
	if (m + x > n and m+n >x and n+x>m)
		cout<< "Yes" <<endl;
	else
	    cout << "No" << endl;
	return 0;
} 

14 已知电水箱的容量为 n 升( n≤10L ),同学们带的杯子平均容量为 xx 毫升( x在100∼300 之间),请问烧一箱开水,最多能倒多少杯(不足 1 杯算 1 杯)。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,x;
	cin >> n >> x;
//	cout << n*1000*1.0/x;
	printf("%.0lf",ceil(n*1000.0/x));
	return 0;
} 

15 已知有三个不等的数,将其中的最大数找出来。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b,c;
	cin >> a >> b >> c;
	int max = a;
	if (max < b) max =b;
	if (max < c) max =c;
    cout << max << endl;
	return 0;
} 

16 已知有四个不等的数,将其中的最大数找出来。

#include <bits/stdc++.h>
#include <climits.h>
using namespace std;
int main(){
	int q,w,e,r,max = INT_MIN;
	cin >> q>>w>>e>>r;
	if(max<q) max =q;
	if(max<w) max =w;
	if(max<e) max =e;
	if(max<r) max =r;
	cout<<max<<endl;
	return 0;
} 

17 在社会实践活动中有三项任务分别是:种树、采茶、送水。依据小组人数及男生、女生人数决定小组的接受任务,人数小于 10 人的小组负责送水(输出 water),人数大于等于 10 人且男生多于女生的小组负责种树(输出 tree),人数大于等于 10 人且男生不多于女生的小组负责采茶(输出tea)。输入小组男生人数、女生人数,输出小组接受的任务。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int b,g,t=0;
	cin >> b>>g;
	int all = b + g;
	if (all < 10) cout<<"water"<<endl;
	else if (all >= 10 and b > g) cout << "tree" <<endl;
	else if (all >= 10 and b <= g) cout <<"tea" <<endl;
	return 0;
} 

18 输入年份,判断是否为闰年。如果是,则输出 yes ,否则输出 no 。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int year;
	cin >> year;
	if ((year%4==0 and year%100!=0) or (year%400==0)) cout<<"yes"<<endl;
	else cout << "no" <<endl;
	return 0;
} 

19 从键盘读入一个整数 n ,请循环输出 1∼n 之间所有的整数,每行输出 1 个

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for(int i=1; i<n+1; i++) cout<< i <<endl;
	return 0;
} 

20 请输出所有的 3 位对称数,对称数指的是一个整数 n正过来和倒过来是一样的。

#include <bits/stdc++.h>
using namespace std;
int main(){
	for (int i=100; i<1000; i++){
		int b = i/100;
		int s = i/10%10;
		int g = i%10;
		if(b == g) cout<< i <<endl;
	}
	return 0;
} 

21 有一个数列,该数列的前 4 个数是: 1 4 7 10 ;
请从键盘读入一个正整数 n ,请通过观察前 4 项的规律,输出1∼n 之间所有满足该规律的数字。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for(int i=1; i<=n; i+=3){
		cout << i <<endl;
	}
} 

22 输出 1∼n 中含有数字 3 或者含有数字 5 ,且因数有 2 (即能被 2 整除)的所有整数。( n<1000

#include <bits/stdc++.h>
using namespace std;
//int main(){
//	int n;
//	cin >> n;
//	for(int i=1; i<=n; i++){
//		int g = i %10;
//		int s = i/10%10;
//		int b = i/100%10;
//		cout << b << endl;
//		if (g%2==0 and(s==3 or s ==5 or b ==3 or b==5))
//		cout << i <<endl;
//	}
//}

int main(){
	int n ,t;
	cin >> n;
	for (int i = 1; i <= n; i++){
		bool flag = false;
		t =i;
		while(t!=0){
			if(t%10==3 || t%10==5){
				flag = true;
				break;
			}
			t /= 10;
		}
		if(flag && i %2 == 0) cout << i << endl;
	}
	return 0;
} 

23 所谓水仙花数,就是指各位数字立方之和等于该数的数;a3 称为 a 的立方,即等于 a×a×a 的值。

#include <bits/stdc++.h>
using namespace std;
int main(){
	for(int i=100; i<=999; i++){
		int g = i %10;
		int s = i/10%10;
		int b = i/100%10;
		if(i == (g*g*g + s*s*s +b*b*b))
		cout << i <<endl;
	}
} 

24 问 555555 的约数中最大的三位数是多少?

#include <bits/stdc++.h>
using namespace std;
int main(){
	int max;
	for(int i=100; i<=999; i++){
		if(555555%i == 0)  max = i;
	}
	cout << max <<endl;
	return 0;
} 

25 找出所有 3 位的既是回文数,又是偶数的数

#include <bits/stdc++.h>
using namespace std;
int main(){
	for(int i=100; i<=999; i++){
		int g = i%10;
		int b = i/100%10;
		if(i%2 == 0 && g ==b)  	cout << i <<endl;
	}
	return 0;
} 

26 如果连续 2 位的差值相等,就是等差数

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m,n;
	cin >> m >> n;
	for(int i = m; i <= n; i++){
		int g = i%10;
		int s = i/10%10;
		int b = i/100%10;
		int q = i/1000%10;
		if (q == 0){
			if(s-g==b-s) cout << i << endl;
		}
		else{
			if (q-b==b-s && b-s==s-g) cout << i <<endl;
		}
	}
} 

27 请输出 1∼n 中至少能够被 2、3、5、7 中两个及两个以上的数整除的数?

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++){
		int flag = 0;
		if(i%2==0) flag++;
		if(i%3==0) flag++;
		if(i%5==0) flag++;
		if(i%7==0) flag++;
		if(flag>=2) cout << i<< endl; 
	}
} 

28 编程求 1+1/2+1/3+⋯+1/n1+1/2+1/3+⋯+1/n 。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	double sum=0;
	cin >> n;
	for(int i = 1; i <= n; i++){
		sum += 1.0/i;
	}
	printf("%.3lf\n",sum);
} 

29 请问 100∼n中连续递增或者连续递减的 3位数有总和是多少,有多少个?

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,flag=0,sum=0;
	cin >> n ;
	for(int i = 100; i <= n; i++){
		int g = i%10;
		int s = i/10%10;
		int b = i/100;
		if(g <s && s <b || g>s && s>b){
			sum+=i;
		    flag++;
		} 
	}
	cout << sum << endl << flag;
} 

30 请打印n行的数字倒直角三角形。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n ;
	for(int i = 1; i <= n; i++){
	    for(int j =1; j <= n-i+1; j++)
	    {
	    	cout << n-i+1;
		}
	    cout << endl;
	}
	return 0;
} 

31 输入一个整数打印平行四边形。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n; //里面的东西要流出去
	for (int i =1; i<= n; i++){
		for (int j=1; j<=i-1; j++) cout<<" ";
		for (int k=1; k<=n; k++) cout<<"*";
		cout <<endl;
	} 
	return 0;	
} 

32 输入一个整数打印数字三角。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for (int i =1; i<= n; i++){
		for (int j=1; j<=n-i; j++) cout<<" ";
		for (int k=1; k<=2*i-1; k++) cout<<k;
		for (int j=1; j<=n-i; j++) cout<<" ";
		cout <<endl;
	} 
	return 0;	
} 

33 输入数字形成数字矩形

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for (int i =1; i<= n; i++){
		for (int j=1; j<=n; j++) cout<<j;
		cout <<endl;
	} 
	return 0;	
} 

34 打印n行的空心正方形。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for (int i =1; i<= n; i++){
		if(i==1 || i==n){
			for(int j=1; j<=n; j++) cout<<"*";
			cout << endl;
		}else{
			cout<<"*";
			for(int j=1;j<=n-2;j++){
		        cout<<" ";		
			}
		    cout<<"*" <<endl;
		}
	} 
	return 0;	
} 

35 打印乘法表

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for (int i =1; i<= n; i++){
		for (int j=1; j<=i; j++) 
		cout<< i<<"*"<<j<<"="<<j*i<<" ";
		cout <<endl;
	} 
	return 0;	
} 

36 输入一个正整数n,求1!-2!+3!-4!+……+N!的结果。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,sum=0,s = 1;
	cin >> n;
	for (int i=1; i <= n; i++){
		s *= i;
		if(i%2==0) sum += s*(-1);
		else sum += s;
	}
	cout << sum <<endl;
	return 0;
} 

37 韩信有一对士兵,他想知道有多少人,他就让士兵报数,如果按照 1 到 5 报数,最末一个士兵报的数为 1 。按照 1 到 6 报数,最末一个士兵报的数为 5 。按照 1 到 7 报数,最末一个士兵报的数为 4 。最后再按 1 到11 报数,最末一个士兵报的数为 10 。请问韩信这队士兵最少有多少人?

#include <bits/stdc++.h>
using namespace std;
int main(){
	for (int i=1;; i++){
		if(i%5==1 and i%6==5 and i%7==4 and i%11==10){
		    cout << i << endl;
		    break;
		}  
	}
	return 0;
} 

38 小球从100米高处自由落下,着地后又弹回高度的一半再落下。经过多少次落地后,小球弹起的高度才会低于0.5 米?

#include <bits/stdc++.h>
using namespace std;
int main(){
	double s =100.0;
	int times = 0;
	while (s >= 0.5){
		s *= 0.5;
		times++;	
	}
	cout << times <<endl;
	return 0;
} 

39 任给一个自然数,若为偶数则除以 2 ,若为奇数则乘 3 加 1 ,得到一个新的自然数后按上面的法则继续演算。若干次后得到的结果必为 1 。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,times = 0;
	cin >> n;
	while (n != 1){
		if (n%2==0) n /= 2;
		else n = n*3+1;
		times++;
	}
	cout << times <<endl;
	return 0;
} 

40 编写一个程序,从接收到的数字 n 中获取这 2 个数字信息

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,times = 0;
	cin >> n;
	int sum = 0;
	while (n!= 0){
		if (n % 10 % 2 == 0) sum += n%10;
		n /= 10;
		times++;
	}
	cout << times <<" " << sum;
	return 0;
} 

41 试计算在区间1到n的所有整数中,数字x(0≤x≤9)共出现了多少次

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,x,times = 0;
	cin >> n >>x;
	for(int i=1; i<=n; i++){
		int temp = i;
		while (temp!= 0)
		{
			if (temp % 10 == x){
				times++;			
			}
			temp /= 10;
	    }
	}
	cout << times <<endl;
	return 0;
} 

42 给出一个正整数 n ( 1≤n≤10000 ),求出 1,2,…,n之中(包括 1 和 n )的回文数的个数

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,times = 0;
	cin >> n ;
	for(int i=1; i<=n; i++){
		int s=0,temp = i;
		while (temp!= 0)
		{
			int t = temp%10;
			s = s*10+t;	
			temp /= 10;
		}
		if (s == i) times++;
	}
	cout << times << endl;
	return 0;
} 

43 输入一个正整数,判断它是否为质数,如是质数则输出Yes,否则输出这个数的大于 1 的最小的约数。

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n ;
	if (n==2) cout << "Yes" <<endl;
	for(int i=2; i<n; i++){
		if (n % i == 0)
		{
			cout << i <<endl;
			break;
		}
		else if (i==n-1)
		    cout << "Yes" <<endl;
	}
	return 0;
} 
**```
44 请编程计算出 1∼N 之间(包括 N )的全部同构数有多少个?**

```cpp
#include <bits/stdc++.h>
using namespace std;

int main(){
	int n,nums=0;
	cin >> n ;
	for(int i=1; i<=n; i++){
		int temp=i*i,x =1;
		while(temp!=0){
			x *=10;
			int s = i*i % x ;
		    temp /= 10;
	    	if (s == i) nums++;	
	    }
	}
    cout << nums <<endl;
	return 0;
} 

45 任意输入一正整数 N ,求出它的所有质因子。

#include <bits/stdc++.h>
using namespace std;

int main(){
	int n,i=2;
	cin >> n ;
	while(n!=1){
		if(n%i==0){
			cout << i <<endl;
			n /= i;
		}else{
			i++;
		}
	}
	return 0;
} 

46 从键盘输入整数L,统计出边长为整数,周长为 L 的不是等边三角形的个数

#include <bits/stdc++.h>
using namespace std;
int main(){
	int L,times=0;
	cin >> L;
	for (int i=1; i < L; i++){
		for(int j=i; j<L; j++){
			for(int k=j; k<L; k++){
				if (!(i==j && j==k) && (i+j+k==L) && (i+j >k)) {
					//cout <<i <<" " <<j <<" "  <<k <<endl;
					times++;
				}
			}
		}
	}
	cout << times << endl; 
	return 0;	     
} 

47 输入 n( 1≤n≤5000 )个正整数,每个数都在 1到20000 之间;要求对这 n 个数中的奇数和偶数分别求和。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
int a[maxn];
int main(){
	int n;
	cin >>n;
	int o=0,j=0;
	for (int i=1; i <= n; i++){
	    cin >> a[i];
	    if (a[i] % 2==0) o+=a[i];
	    else j+=a[i];
	}
	cout << j <<endl <<o <<endl; 
	return 0;	     
} 

48 陶陶摘苹果
包括两行数据。
第一行包含 10 个 100 到 200之间(包括 100和 200 )的整数(以厘米为单位)分别表示 10 个苹果到地面的高度,两个相邻的整数之间用一个空格隔开。第二行只包括一个 100 到 120 之间(包含 100和 120 )的整数(以厘米为单位),表示陶陶把手伸直的时候能够达到的最大高度。

#include <bits/stdc++.h>
using namespace std;
int a[20],h,total;
int main() {
	for (int i=1; i <= 10; i++){
		cin >> a[i];
	}
	cin >> h;
	for(int i=1; i <= 10; i++){
        if(h+30>=a[i]) total++;
	}
	cout << total<<endl;
	return 0;
}

49 任意输入 n 个整数,把它们的最大值,最小值求出来。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n,MAX=INT_MIN,MIN=INT_MAX;
int a[maxn];
int main() {
	cin >> n;
	for (int i=1; i <= n; i++){
		cin >> a[i];
	}
	for(int i=1; i <= n; i++){
		MAX = max(MAX, a[i]);
		MIN = min(MIN, a[i]);
	}
	cout << MAX <<" " <<MIN<<endl;
	return 0;
}

50 编一个程序,计算 1∼n 范围内素数的个数。

#include <bits/stdc++.h>
using namespace std;
int x;
bool is_prime(int n){
	if(n<=1) return false;
	for(int i=2;i<sqrt(n);i++){
		if(n%i==0) return false;
	}
	return true;
}
int main() {
	while(true)
	{
	cin >>x;
	if(x==0) break;
	int total = 0;
	for (int i=1; i <= x; i++){
		if (is_prime(i)){
			total++;
		}
	}
	cout << total<<endl;
}
	return 0;
}

51 从键盘输入一个整数 N ,输入 N (5≤N≤20)个元素,输入一个整数,判断这个数组中最后一次出现该数的位置,如果没有请输出−1。

#include <bits/stdc++.h>
using namespace std;
const int maxn=100;
int n,x;
bool flag=false;
int a[maxn];
int main(){
	cin >> n;
	for (int i=1;i <= n; i++){
		cin >> a[i];
	}
	cin >> x;
	for (int i=n;i>=1; i--){
		if(a[i] == x ){
			flag =true;
			cout << i << endl; 
			break;
		}
	}
	if (flag == false){
		cout << -1 <<endl;
	}
	return 0;
	
	
}  

52 数组元素的移动,把数组的第 x 个位置的元素先保存起来,然后把 x+1 到 n 的元素,依次往前移一位,最后原来的第 x 个位置的元素放在最后。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n,x,temp;
int a[maxn];
int main(){
	cin >>n;
	for (int i=1; i<=n; i++) cin >> a[i];
	cin >> x;
	temp = a[1];
	for(int i=1; i<=n-1; i++){
		a[i] = a[i+1];
	}
	a[n] = temp;
	for(int i=1; i <= n; i++) cout << a[i] << " ";
	return 0;
	
}

53 在一个数组的第 x 个位置插入一个新的数y。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n,x,y;
int a[maxn];
int main(){
	cin >>n;
	for (int i=1; i<=n; i++) cin >> a[i];
	cin >> x;
	cin >> y;
	for(int i=n+1; i>=x+1; i--){
		a[i] = a[i-1];
	}
	a[x] = y;
	for(int i=1; i <= n+1; i++) cout << a[i] << " ";
	return 0;
}

54 把一个数组的第 x 个位置的元素删除掉。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n,x,y;
int a[maxn];
int main(){
	cin >>n;
	for (int i=1; i<=n; i++) cin >> a[i];
	cin >> x;
	for(int i=x; i<=n; i++){
		a[i] = a[i+1];
	}
	for(int i=1; i <= n-1; i++) cout << a[i] << " ";
	return 0;
}

55 对数组元素从大到小排序

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n;
int a[maxn];
int main(){
	cin >>n;
	for (int i=1; i<=n; i++) cin >> a[i];
	for(int i=1; i<=n; i++){
		for(int j =1; j<=n-i;j++){
			if(a[j] > a[j+1]){
				swap(a[j], a[j+1]);
			}
		}
	}
	for(int i=1; i <= n; i++) cout << a[i] << " ";
	return 0;
}

56 输入 n 个不超过 30000 的整数(n≤10)。然后求出每个数的数字和,再按每个数的数字和由小到大排列输出。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n,x;
int a[maxn];
int main(){
	cin >> n;
	for (int i=1;i<=n;i++){
		int total =0;
		cin >> x;
		while(x!=0){
			total += x%10;
			x/=10;
		}
		a[i] = total;
	}
	sort(a+1, a+n+1);
	for(int i =1; i<=n; i++) cout << a[i] <<" ";
	return 0;	
} 

57 给定一个长度为 N ( 0<n≤10000)的序列,保证每一个序列中的数字 a[i] 是正整数 ,编程要求求出整个序列中第 k 大的数字减去第 k 小的数字的值 m ,并判断 m 是否为质数。(0<k≤n)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000;
int n,k;
int a[maxn];
bool is_prime(int x){
	if(x<1) return false;
	for(int i=2; i<=sqrt(x); i++){
		if(x%i==0) return false;
	}
	return true;
}
int main(){
	cin >> n >> k;
	for (int i=1;i<=n;i++) cin >>a[i];
	sort(a+1,a+n+1);
	int p1 = a[n-k+1];
	int p2 = a[k];
	int m = p1-p2;
	if (is_prime(m)) cout << "YES" <<endl;
    else cout << "NO" << endl;
    cout << m <<endl;
    return 0;
} 

58 给你 N 个数(N≤100),每个数都在(0∼1000)之间,其中由很多重复的数字,请将重复的数字只保留一个,并将剩下的数由小到大排序并输出。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n,x,cnt;
int a[maxn];
int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d",&x);
		a[x]=1;
	}
	for (int i=1;i<=maxn;i++){
		if(a[i] == 1) cnt++;
	}
	for(int i=1;i<=maxn;i++){
		if(a[i]==1){
			printf("%d\n",i);			
		}
	}
}

59 小明从一副扑克牌中(没有大小王, J 认为是数字 11 , Q 是 12 , K 是 13 , A 是 1 )抽出 2 张牌求和,请问能够组合出多少个不相等的数,按照由小到大输出这些数。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int nums[78]; // 存放不相等的数
    int cnt = 0; // 数组中已经存放的数的个数
    for (int i = 1; i <= 13; i++) {
        for (int j = i + 1; j <= 13; j++) {
            int sum = i + j; // 计算出新数
            bool exist = false; // 标记新数是否已经存在
            for (int k = 0; k <; k++) {
                if (nums[k] == sum) { // 如果新数已经存在,就标记一下
                    exist = true;
                    break;
                }
            }
            if (!exist) { // 如果新数不存在,就将它存入数组中
                nums[cnt++] = sum;
            }
        }
    }
    sort(nums, nums + cnt); // 对数组进行排序
    for (int i = 0; i <; i++) { // 输出不相等的数
        cout <<[i] << " ";
    }
    cout << endl;
    return 0;
}

60 给出一个正整数 n ( 2≤n≤1000000 ),求连续非素数的最大长度

#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int primes[N / 10], cnt; // 存放素数
bool st[N + 10]; // 标记非素数

void init() { // 欧拉筛
    for (int i = 2; i <= N; i++) {
        if (!st[i]) { // i 是素数
            primes[cnt++] = i;
        }
        for (int j = 0; primes[j] <= N / i; j++) {
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}

int main() {
    int n;
    cin >> n;
    init(); // 先预处理素数
    int res = 0, len = 0; // res 存放最终结果,len 存放当前长度
    for (int i = 2; i <= n; i++) {
        if (st[i]) { // i 是非素数
            len++;
        } else { // i 是素数
            res = max(res, len);
            len = 0; // 重置长度
        }
    }
    res = max(res, len); // 特别注意要处理最后一段
    cout << res << endl;
    return 0;
}

61 有一对兔子,从出生后第 3 个月起每个月都生一对兔子,一对小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死。问第 n 个月( n≤50 )的兔子总数为多少对?

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int f[51] = {0};
    f[1] = f[2] = 1;
    for (int i = 3; i <= n; i++) {
        f[i] = f[i - 1] + f[i - 2];
    }
    cout << f[n] << endl;
    return 0;
}

62 求 S=1+2+4+7+11+16…的值刚好大于等于5000 时 S 的值。

#include <bits/stdc++.h>
using namespace std;

int main() {
    int s = 0;
    for (int i = 1; ; i++) {
        int a = i * (i + 1) / 2;
        if (a >= 5000) {
            s += i;
            break;
        }
        s += a;
    }
    cout << s <<;
    return 0;
}

63 汉诺塔的问题大家都已经很熟悉了,有三个柱子,每个柱子上有一些大小不一的金片,要把金片从 A 柱移动到 C 柱,可以借助 B 柱,请问 n 个金片的情况下,需要最少移动多少次?

#include <bits/stdc++.h>
using namespace std;

void hanoi(int n, char A, char B, char C) {
    if (n == 1) {
        cout <<move disk " << n << " from " << A << " to " << C << endl;
    } else {
        hanoi(n - 1, A, C, B);
        cout <<move disk " << n << " from " << A << " to " << C << endl;
        hanoi(n - 1, B, A, C);
    }
}

int main() {
    int n;
    cout <<enter the number of disks: ";
    cin >> n;
    hanoi(n, 'A', 'B', 'C');
    return 0;
}

64 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const int MAXN=10000012;
const int N=1e6+7;
const int INF=0x3f3f3f3f;
const int P=131;
 
int apple[20][20],maxn,minn,n,m;
int main(){
    maxn=0,minn=999;  
    cin >> n >> m;
    for(int i=0;i<n;i++){   //需要两个循环用来输入数组元素
        for(int j=0;j<m;j++){
            cin >> apple[i][j];
            if(maxn<apple[i][j]){  //比较求数组中的最大元素
                maxn=apple[i][j];
            }
            if(minn>apple[i][j]){  //比较求数组中的最小元素
                minn=apple[i][j];
            }
        }
    }
    cout << maxn-minn << "\n";
    return 0;
}

65 在一个 n 行 m 列的矩阵王国中,生活着一些整数,其中一些是素数,一些不是素数。如果一个素数的上下左右、左上、右上、左下、右下相邻的数中都没有素数,我们就认为这是一个孤独的素数。

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1005;
bool isPrime[MAXN][MAXN];

void sieve(int n, int m) {  // 筛法求出所有素数
    memset(isPrime, true, sizeof(isPrime));  // 初始化全部为素数
    isPrime[0][0] = isPrime[0][1] = isPrime[1][0] = isPrime[1][1] = false;  // 0 和 1 不是素数

    /** 埃拉托色尼筛法 **/
    for (int i = 2; i <= min(n, m); i++) {
        if (isPrime[i][i]) {
            for (int j = 2 * i; j <= n; j += i) {
                isPrime[j][i] = isPrime[i][j] = false;
            }
            for (int j = 2 * i; j <= m; j += i) {
                isPrime[i][j] = isPrime[j][i] = false;
            }
        }
    }
}

bool isLonelyPrime(int x, int y) {  // 判断某个数是否为孤独的素数
    if (!isPrime[x][y]) {  // 如果当前数不是素数,返回 false
        return false;
    }
    // 判断所有相邻的数是否都不是素数
    return !isPrime[x - 1][y - 1] && !isPrime[x - 1][y] && !isPrime[x - 1][y + 1] &&
        !isPrime[x][y - 1] && !isPrime[x][y + 1] && !isPrime[x + 1][y - 1] &&
        !isPrime[x + 1][y] && !isPrime[x + 1][y + 1];
}

int main() {
    int n, m;
    cout <<enter the number of rows and columns: ";
    cin >> n >> m;

    sieve(n, m);  // 筛法求素数

    int cnt = 0;  // 记录孤独的素数的数量
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (isLonelyPrime(i, j)) {
                cnt++;
            }
        }
    }

    cout << "the number of lonely primes is: " <<;

    return 0;
}

66 扫雷游戏是一款十分经典的单机小游戏。在 n 行 m 列的雷区中有一些格子含有地雷(称之为地雷格),其他格子不含地雷(称之为非地雷格)。玩家翻开一个非地雷格时,该格将会出现一个数字——提示周围格子中有多少个是地雷格。游戏的目标是在不翻出任何地雷格的条件下,找出所有的非地雷格。
现在给出 n 行 m 列的雷区中的地雷分布,要求计算出每个非地雷格周围的地雷格数。注:一个格子的周围格子包括其上、下、左、右、左上、右上、左下、右下八个方向上与之直接相邻的格子。

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 105;
int n, m;
int mine[MAXN][MAXN];

void countMine(int x, int y) {
    int cnt = 0;
    for (int i = x - 1; i <= x + 1; i++) {
        for (int j = y - 1; j <= y + 1; j++) {
            if (i >= 1 && i <= n && j >= 1 && j <= m && mine[i][j] == 1) {
                cnt++;
            }
        }
    }
    mine[x][y] = cnt;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> mine[i][j];
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (mine[i][j] == 0) {
                countMine(i, j);
            } else {
                cout <<";
            }
        }
        cout <<;
    }

    return 0;
}

67 爬梯子

// 递归
#include <bits/stdc++.h>
using namespace std;

int climbStairs(int n) {
    if (n == 1) {
        return 1;
    }
    if (n == 2) {
        return 2;
    }
    return climbStairs(n - 1) + climbStairs(n - 2);
}

int main() {
    int n;
    cin >> n;
    cout <<Stairs(n) << endl;
    return 0;
}
// 动态规划
#include <bits/stdc++.h>
using namespace std;

int climbStairs(int n) {
    int dp[n+1];
    dp[1] = 1;
    dp[2] = 2;
    for (int i = 3; i <= n; i++) {
        dp[i] = dp[i-1] + dp[i-2];
    }
    return dp[n];
}

int main() {
    int n;
    cin >> n;
    cout << climbStairs(n) <<;
    return 0;
}

68 参议院

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

class Solution {
public:
    string predictPartyVictory(string senate) {
        // R = true表示本轮循环结束后,字符串里依然有R。D同理
        bool R = true, D = true;
        // 当flag大于0时,R在D前出现,R可以消灭D。当flag小于0时,D在R前出现,D可以消灭R
        int flag = 0;
        while (R && D) { // 一旦R或者D为false,就结束循环,说明本轮结束后只剩下R或者D了
            R = false;
            D = false;
            for (int i = 0; i < senate.size(); i++) {
                if (senate[i] == 'R') {
                    if (flag < 0) senate[i] = 0; // 消灭R,R此时为false
                    else R = true; // 如果没被消灭,本轮循环结束有R
                    flag++;
                }
                if (senate[i] == 'D') {
                    if (flag > 0) senate[i] = 0;
                    else D = true;
                    flag--;
                }
            }
        }
        // 循环结束之后,R和D只能有一个为true
        return R == true ? "Radiant" : "Dire";
    }
};

int main() {
    string dota_string = "RRDDDR";
    Solution solution;
    cout << solution.predictPartyVictory(dota_string)<<endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值