杂七杂八的代码

#include <iostream>
 
using namespace std;
 
// 基类
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
 
// 派生类
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
 
int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;
 
   return 0;
}

文件

#include<bits/stdc++.h>
using namespace std;
int a[100009];
int main(){
	freopen("1.txt", "r",stdin);
	freopen("2.txt", "w", stdout);
	int n;
	n = 10;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n-i-1; j++){
			if(a[j] > a[j+1]){
				swap(a[j],a[j+1]);
			}
		}
	}
	for(int i = 0; i < n; ++i){
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;
}

蛇形填数

#include<bits/stdc++.h>
using namespace std;
int a[109][109];
int n;
int main(){
	cin >> n;
	int num = 1;
	int x = 1,y = 1;
	int sum = 0;
	while(1){
		while(y < n-sum){
			a[x][y] = num;
			num ++;
			y++;
		}
		if(num == n * n){
			a[x][y] = num;
			break;
		}
		while(x < n-sum){
			a[x][y] = num;
			num ++;
			x++;
		}
		if(num == n * n){
			a[x][y] = num;
			break;
		}
		while(y > 1+sum){
			a[x][y] = num;
			num ++;
			y--;
		}
		if(num == n * n){
			a[x][y] = num;
			break;
		}
		while(x > 2+sum){
			a[x][y] = num;
			num++;
			x--;
		}
		if(num == n * n){
			a[x][y] = num;
			break;
		}
		sum++;
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			printf("%2d ",a[i][j]);
		}cout << endl;
	}
	return 0;
}

旋转方阵

#include<bits/stdc++.h>
using namespace std;
int a[109][109];
int n;
int main(){
	cin >> n;
	int num = 1;
	int x = 1, y = 1;int sum = 0;
	while(1) {
		while(x < n-sum) {
			a[x][y] = num;
			num++;
			x++;
		}
		if(num == n*n) 
		{
			a[x][y] = num;
			break;
		}
		while(y < n-sum) {
			a[x][y] = num;
			num++;
			y++;
		}
		if(num == n*n) 
		{
			a[x][y] = num;
			break;
		}
		while(x > 1+sum) {
			a[x][y] = num;
			num++;
			x--;
		}
		if(num == n*n) 
		{
			a[x][y] = num;
			break;
		}
		while(y > 2+sum) {
			a[x][y] = num;
			num++;
			y--;
		}
		if(num == n*n) 
		{
			a[x][y] = num;
			break;
		}
		sum++;
	}
	for(int i = 1;i <= n;++i) {
		for(int j = 1;j <= n;++j) {
			printf("%2d ", a[i][j]);
		}
		cout << '\n';
	}
	return 0;	
}

组合数

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

int C(int m,int n){
	int a = 1,b = 1;
	for(int i = n-m+1; i <= n; i++){
		a *= i;
	}
	for(int i = 1; i <= m; i++){
		b *= i;
	}
	return (a / b);
}
int main(){
	int m,n;
	cin >> m >> n;
	int cmn;
	cmn = C(m,n);
	cout << cmn << endl;
}

跳马

#include<bits/stdc++.h>
using namespace std;
const int rr[4][2] = {2,1,1,2,2,-1,1,-2};
struct node{
	int x,y,step;
	node(){
	}
	node(int xx,int yy,int ss):x(xx),y(yy),step(ss){
	}
};
int x,y;
int xend,yend;
int ans = 0;

void bfs(int x,int y,int step){
	queue<node>A;
	A.push(node(x,y,step));
	while(!A.empty()){
		node u = A.front();
		A.pop();
		for(int i = 0; i < 4; i++){
			int xx = u.x + rr[i][0];
			int yy = u.y + rr[i][1];
			int ss = u.step + 1;
			if(xx == xend && yy == yend){
				ans++;
			}
			if(xx >= 0 && yy >= 0 && xx <= xend && yy <= yend){
				A.push(node(xx,yy,ss));
			}
		} 
	}
	
}

int main(){
	
	cin >> x >> y;
	cin >> xend >> yend;
	bfs(x,y,0);
	cout << ans << endl;
	return 0;
}

数塔

#include<bits/stdc++.h>
using namespace std;
int a[10][10];
int main(){
	int n;
	cin >> n;
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= i; j++){
			cin >> a[i][j];
		}
	}
	int max1 = -1;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= i; j++){
			if(i == 1 && j == 1) {
				
			}
			else if(i == 1) {
				
			}
			else if(j == 1) {
				a[i][j] = a[i][j] + a[i-1][j];
			}
			else {
				a[i][j] = a[i][j] + max(a[i-1][j-1],a[i-1][j]);	
			}
			if(i == n) {
				max1 = max(max1, a[i][j]);
			}	
		}
	}
	cout << max1 << endl;
	return 0;
}

最短路径


/*6 6
1 3 3 5 6 5
4 4 5 5 6 0
4 5 9 0 9 5
4 4 5 6 6 4
2 3 4 5 6 9 
2 3 4 5 6 9 */

#include<bits/stdc++.h>
using namespace std;
int a[109][109];
int main(){
	int m,n;
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin >> a[i][j]; 
		}
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			if(i == 1 && j == 1) {
			
			}
			else if(i == 1) {
				a[i][j] = a[i][j] + a[i][j-1];
			} 
			else if(j == 1) {
				a[i][j] = a[i-1][j] + a[i][j];
			}
			else {
				a[i][j] = a[i][j] + min(a[i-1][j], a[i][j-1]);
			}
		}
	}
	
	cout << a[n][m];
}

数字反转(各种数)

#include<bits/stdc++.h>
using namespace std;
int zs(string s){
	int flag = 0;
	for(int i = s.length()-1; i >= 0 ; i--){
		if(s[i] == '0') {
			
		}
		else {
			flag= 1;
		}
		if(flag) {
			cout << s[i];	
		}	
	}
	cout << endl;
}
int xs(string s,int a){
	int flag = 0;
	for(int i = a-1; i >= 0; i--){
		if(s[i] == '0') {
			
		}
		else {
			flag= 1;
		}
		if(flag) {
			cout << s[i];	
		}
	}
	cout << ".";int g = a;
	for(int i = a+1;i < s.length();++i) {
		if(s[i] != '0') {
			break;
		}
		else {
			g = i;
		}
	}
	if(g == s.length()-1) {
		g = s.length()-2;
	}
	for(int i = s.length()-1; i > g; i--){
		cout << s[i];	
	}
	cout << endl;
}
int fs(string s,int a){
	int flag = 0;
	for(int i = a-1; i >= 0; i--){
		if(s[i] == '0') {
			
		}
		else {
			flag= 1;
		}
		if(flag) {
			cout << s[i];	
		}
	}
	cout << "/";
	for(int i = s.length(); i > a; i--){
		if(s[i] == '0') {
			
		}
		else {
			flag= 1;
		}
		if(flag) {
			cout << s[i];	
		}
	}
	cout << endl;
}
int bfs(string s){
	int flag = 0;
	for(int i = s.length()-1; i >= 0 ; i--){
		if(s[i] == '0') {
			
		}
		else {
			flag= 1;
		}
		if(flag) {
			cout << s[i];	
		}
	}
	cout << "%" << endl;
}
int main(){
	string s;
	cin >> s;
	for(int i = 0; i < s.length(); i++){
		if(s[i] == '.'){
			xs(s,i);return 0;
		}
		if(s[i] == '/'){
			fs(s,i);return 0;
		}
		if(s[i] == '%'){
			bfs(s);return 0;
		}
	}
	zs(s);
	return 0;
}

连续数列

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

int a[109];
int n;
int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
	}
	int maxa = -0x3f3f3f;
	for(int i = 1; i <= n; i++){
		a[i] = max(a[i]+a[i-1],a[i]);
		if(maxa < a[i]){
			maxa = a[i];
		}
	}
	cout << maxa << endl;
	return 0;
} 

五位质数回文

#include<bits/stdc++.h>
using namespace std;
int prime(int n){
	int flag = 0;
	for(int i = 2; i <= sqrt(n); i++){
		if(n % i == 0){
			return 0;
			flag = 1;
		}
	}
	if(flag){
		return 1;
	}	
}
int a,s,b;
int main(){
	for(int i = 100; i < 1000; i++){
		s = (i / 10) % 10;
		b = (i / 100) % 10;
		a = i * 100 + s * 10 + b;
		if(prime(a)){
			cout << a << endl;
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int rr[4][2] = {2,1,1,2,2,-1,1,-2};
struct node{
	int x,y,step;
	node(){
	}
	node(int xx,int yy,int ss):x(xx),y(yy),step(ss){
	}
};
int x,y;
int xend,yend;
int ans = 0;

void bfs(int x,int y,int step){
	queue<node>A;
	A.push(node(x,y,step));
	while(!A.empty()){
		node u = A.front();
		A.pop();
		for(int i = 0; i < 4; i++){
			int xx = u.x + rr[i][0];
			int yy = u.y + rr[i][1];
			int ss = u.step + 1;
			if(xx == xend && yy == yend){
				ans++;
			}
			if(xx >= 0 && yy >= 0 && xx <= xend && yy <= yend){
				A.push(node(xx,yy,ss));
			}
		} 
	}
	
}

int main(){
	
	cin >> x >> y;
	cin >> xend >> yend;
	bfs(x,y,0);
	cout << ans << endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
/*
a->c f1


a1->b f1
a2->c
b -> c f1

a->b f2
a3->c
b->c f2

a->b f3
a4->c 
b->c f3 

*/

#include<iostream>
using namespace std;
void dfs(int n, char a, char b, char c){
	if(n == 1) {
		cout << 1 << a << "->"<< c << '\n';
		return ;
	}
	dfs(n-1, a, c, b);
	cout << n << a << "->" << c << '\n';
	dfs(n-1, b, a, c);
}
int main() {
	int n;
	cin >> n;
	dfs(n, 'a', 'b', 'c');
	return 0;
}
/* 
8
2 4 5 6 7 5 4 8
*/
#include<iostream>
using namespace std;
int dp[10009], a[10009];
int main() {
	int n;
	cin >> n;
	for(int i = 1;i <= n;++i) {
		dp[i] = 1;
	}
	for(int i = 1;i <= n;++i) {
		scanf("%d", &a[i]);
	}
	int max1 = 1;
	for(int i = 1;i <= n;++i) {
		for(int j = 1;j < i;++j) {
			if(a[i] > a[j]) {
				dp[i] = max(dp[i], dp[j]+1);	
			}
		}
		max1 = max(max1, dp[i]);
	}
	cout << max1 << '\n';
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
vector<int>A;
int book[10];
int main(){
	for(int i = 123; i <= 987; i++){
		if(int(sqrt(i)) * int(sqrt(i)) == i) {
			A.push_back(i);
		}
	}
	for(int i = 0;i < A.size();++i) {
		for(int j = i+1;j < A.size();++j) {
			for(int z = j+1;z < A.size();z++) {
				int a = A[i], b = A[j], c = A[z];
				int flag = 0;
				while(a) {
					book[a%10]++;
					if(book[a%10] > 1) {
						flag = 1;
					}
					a /= 10;
				}
				while(b) {
					book[b%10]++;
					if(book[b%10] > 1) {
						flag = 1;
					}
					b /= 10;
				}
				while(c) {
					book[c%10]++;
					if(book[c%10] > 1) {
						flag = 1;
					}
					c /= 10;
				}
				if(flag) {
					memset(book,0, sizeof(book));
				}
				else {
					cout << A[i] << " " <<  A[j] << " "<< A[z] << '\n';return 0;
				}	
			}
			
		}
	}
}


计算天数
/*2000年5月6日*/
#include<iostream>
using namespace std;
int n1,y1,r1,n2,y2,r2;
int run(int n){
	if(n%4==0&&n%100!=0||n%400==0){
		return 1;
	}
	return 0;
}
int main(){
	
	cin >> n1 >> y1 >> r1;
	cin >> n2 >> y2 >> r2;
	int a[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	int t,s,s1 = 0,s2 = 0;
	s = (n2 - n1) * 365;
	for(int i = n1;i < n2;++i) {
		if(run(i)) {
			s++;
		}
	}
	if(run(n1)) {
		a[2] = 29;
	}
	else {
		a[2] = 28;
	}
	for(int i = 1; i < y1; i++){
		s1 += a[i];
	}
	s1 += r1;
	
	
	if(run(n2)) {
		a[2] = 29;
	}
	else {
		a[2] = 28;
	}
	for(int i = 1; i < y2; i++){
		s2 += a[i];
	}
	s2 += r2;
	t = s - s1 + s2;
	
	cout << t << endl;
	return 0;
}

7.2 

#include<bits/stdc++.h>
using namespace std;
int a[109];
int book[109];
int main(){
	int n,m;
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		a[i] = 1;
	}
	int num = 0; 
	int r =1;
	int sn = 1;
	while(1){
		if(r == n + 1){
			r = 1;
		}
		if(sn == m && book[r] == 0){
			sn = 1;
			book[r] = 1;
			num++;
			if(num >= n-1){
				break;
			}
		}
		if(book[r] == 0){
			sn++;
		}
		r++;
	}
	for(int i = 1; i <= n; i++){
		if(book[i] == 0){
			cout << i << endl;break;
		}
	}
	return 0;
}

6.4 

#include<bits/stdc++.h>
using namespace std;
int main(){
	string a,b;
	cin >> a >> b;int flag = 0;
	for(int i = 0; i <= b.length(); i++){
		if(a[0] == b[i]) {
			int z = 0;
			while(b[i+z] == a[z]) {
				z++;
				if(i + z >= b.length()) {
					break;
				}
			}
			if(z == a.length()-1) {
				cout << "yes" << endl;flag = 1;break;
			}
		}
	}
	if(!flag) {
		cout << "no" << endl;
	}
}

6.108

#include<bits/stdc++.h>
using namespace std;
int main(){
	int f(int n);
	for(int n = 1000; n < 10000; n++){
		if(int(sqrt(n)) * int(sqrt(n)) == n) {
			if(f(n) == 1){
				cout << n << endl;
			}	
		}
	}
	return 0;
}
int f(int n){
	int a[5];
	for(int i = 4; i > 0; i--){
		a[i] = n % 10;
		n = n / 10;
	}
	int flag = 0;
	for(int i = 1; i < 5; i++){
		if(a[i] > a[i + 1]){
			flag = 1;break;
		}
	}
	if(flag){
		return 0;
	}else return 1;
}

6.5.3

#include<bits/stdc++.h> 
using namespace std;
int main() {
	int ehuo = 1;
	for(int z = 1;;++z) {
		int flag = 0;
		double eshenxia = 4.0*z;
		for(int i= 1;i <= 5;++i) {
			eshenxia = eshenxia * 5.0/4.0+1.0;
			if(eshenxia != int(eshenxia)) {
				flag = 1;break;
			}
			ehuo = eshenxia / 4;
			if(ehuo != int(ehuo)) {
				flag = 1;break;
			}
		}
		if(flag == 0) {
			cout << eshenxia << '\n';break;	
		}		
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	int l = 1, r = n;
	int find;
	cin >> find;int flag = 0;
	while(l <= r) {
		int mid = (l+r)/2;
		if(a[mid] == find) {
			cout << mid << '\n';
			flag = 1;
			break;
 		}	
 		else if(a[mid] < find) {
 			l = mid + 1;
		}
		else {
			r = mid - 1;
		}
	}
	if(flag == 0) {
		cout < '-1\n';
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值