算法

截图

十进制转化任意进制


#include <iostream>
#include<cstring>
#include <algorithm>//reverse()函数使用所需头文件
using namespace std;
string convertToBase8(int num) {

        string ret;
        bool isNegative = false;

        if (num < 0)
        {
            num = num * (-1);

            isNegative = true;
        }

        if (num == 0)
        {
            return "0";
        }

        while (num > 0)
        {
            ret.push_back(num % 8 + '0');

            num = num / 8;
        }

        if (isNegative)
        {
            ret.push_back('-');
        }
//使用了string类中的一些操作


        reverse(ret.begin(), ret.end());//reverse()会将区间[beg,end)内的元素全部逆序


        return ret;
    }
    int main() {
        int n = 0 ;
        cin >> n;
        cout << convertToBase8(n);
    }

截图


全排列

1.问题解决输入遇到逗号停止

下面这个只能单个逗号才会停止

string a;
    string list[10];
    int c = 0;
    while (cin>>a) {
        if (a == ",") break;
        else {
            list[c] = a;
            c++;
        }
    }
#include<iostream>
#include<cstring>
using namespace std;

inline void Swap(string& a, string& b) {
    string temp = a;
    a = b;
    b = temp;
}

void Perm(string list[], int k, int m) {
    if (k == m) {
        for (int i = 0; i <= m; i++) cout << list[i];
        cout << " ";
    }
    else {
        for (int i = k; i <= m; i++) {
            Swap(list[k], list[i]);
            Perm(list, k + 1, m);
            Swap(list[k], list[i]);
        }
    }
}
int main() {
    char a;
    string list[10];
    int c = 0;
    while (cin>>a) {
        if (a == ',') break;
        else {
            list[c] = a;
            c++;
        }
    }
    Perm(list, 0, c);
}
//借助al的头文件解决字典顺序输入的全排列
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

void Perm(string list[],  int m) {
        do {
            for (int i = 0; i <= m; i++) {
                cout << list[i];
            }
            cout << " ";
        } while (next_permutation(list, list + m));
    
}
int main() {
    char a;
    string list[10];
    int c = 0;
    while (cin >> a) {
        if (a == ',') break;
        else {
            list[c] = a;
            c++;
        }
    }
    sort(list, list + c);
    Perm(list,  c );
}

第k小

#include<iostream>
using namespace std;
int Partition(int a[], int l, int r)
{
    int i = l, j = r + 1;
    int x = a[l];
    while (true)
    {
        while (a[++i] < x && i <= r);
        while (a[--j] > x);
        if (i >= j)
            break;
        int m;
        m = a[i];
        a[i] = a[j];
        a[j] = m;
    }
    a[l] = a[j];
    a[j] = x;
    return j;
}
int select(int a[], int left, int right, int k)
{
    if (left == right)
        return a[left];
    int i = Partition(a, left, right);
    int j = i - left + 1;
    if (k <= j)
        return select(a, left, i, k);
    else
        return select(a, i + 1, right, k - j);

}
int main()
{
    int m, k;
    cin >> m >> k;
    int* p = new int[m];
    for (int i = 0; i < m; i++)
        cin >> p[i];
    int h = select(p, 0, m - 1, k);
    cout << h;
    return 0;
}
//这个算法没有使用以五个为一组

快速幂及取余

int PowerMod(int a, int b, int c)
{
    int ans = 1;
    a = a % c;
    while (b > 0)
    {
        if (b%2 == 1)
            ans = (ans * a) % c;
        b = b / 2;
        a = (a * a) % c;
    }
    return ans
}//快速幂取余,a的b次方对c取余
#include<iostream>
#include<algorithm>
using namespace std;

int qpow(int a, int n)
{
    if (n == 0)
        return 1;
    else if (n % 2 == 1)
        return qpow(a, n - 1) * a;
    else
    {
        int temp = qpow(a, n / 2);
        return temp * temp;
    }
}
int zz(int aa) {
    int num = 0;
    for (int i = 0; i <= aa; i++) {
        num += qpow(i, i);
    }
    return num % 100000007 ;
}
int main() {
        int x,y,z;
        cin >> x >> y >> z;
        cout << zz(x) << endl;
        cout << zz(y) << endl;
        cout << zz(z) << endl;
}
#include<iostream>
using namespace std;
const long long N = 100000007;
#define lx long long
lx m;
lx hs(lx n,lx m,lx k)
{
    lx ans=1%k;
    while(m)
    {
        if (m&1)
            ans=ans*n%k;
        n=n*n%k;
        m>>=1;
    }
    return (ans%k);
}
int main(){
	int m;
	while(cin>>m){
		lx res=1;
		for(int i=1;i<=m;i++){
			lx t = hs(i,i,N)%N;
			res=(res+t)%N;
		}
		cout<<res%N<<endl;
	}
}

Joseph问题

#include<iostream>
using namespace std;
int vis[16] = { 0 };
int fond(int k)
{
    if (vis[k])                                  //如果k是曾经出现过的,已经计算了答案的,就直接输出;
        return vis[k];
    else
    {
        for (int i = k + 1;; i++)                   //从k+1开始找
        {
            int sum = k * 2, flag = 0;                //sum为所剩的人数,flag来标记一个数是否为后k个数,如果flag==1,这个i不用继续找下去了;
            for (int j = i; flag == 0; j += i - 1)
            {
                if (j > sum)
                    j = j % sum ? j % sum : sum;
                if (j <= k)
                    break;
                else
                    sum--;
                if (sum == k)
                    flag = 1;
            }
            if (flag)
            {
                vis[k] = i;
                return vis[k];
            }
        }
    }
}
int main()
{
    int n;
    while (cin >> n && n)
        cout << fond(n) << endl;
    return 0;
}

火柴

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main() {
	int n=1, val=0;
	while (n) {
		cin >> n;
		int a = 0;
		for (int i = 0; i < n; i++) {
			cin >> val;
			a ^= val;
		}
		if (a != 0) {
			cout << "Yes" << endl;
		}
		else {
			cout << "No" << endl;
		}
		b--;
	}
}

Factstone Benchmark

//位数的意思是:比如4位可以表示2^4=16个0到15 16个整数 
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int y; //y表示年份
	int w; //w是指数,2^w表示位数(字长),即len
	int n; //n为FactStone评级
	int len;  //len是字长(位数),
	//如w=4,即len=2^4=16,位数为16位,可以表示2^16个整数(0~2^16-1)
	
	while (1)  //不断执行循环,跳出条件为输入0,下面写出
	{
		cin >> y;
		if (y == 0) //程序结束执行的条件  
		{
			break;
		}
		int w = (y / 10) - 194;    //w分别取2,3,4,5……  
		len = pow(2, w);      //求出位数(字长)len
		n = 1;
		double result = 0;
		while (result < len*log(2))  //这是纸上推出来的
		{
			result = result + log(n);  //乘法转换为了加法
			n++;
		}
		cout << n - 2 << endl;   //关于这里为什么是n-2,请看下面解释
	}
	return 0;
}

截图

内部收益率

#include<iostream>
#include<cmath>
#include <stdio.h>
using namespace std;

double polyval(int*x,int y, double z)
{
	double val=0.0;
	for (int i = 0; i <= y; i++) {
		val += x[i] / pow(z+1,i);
	}
	return val;
}
double ddd(double &c) {
	int T;
	cin >> T;
	int CF[30] = { 0 };
	if (T > 0) {
		for (int i = 0; i <= T; i++) {
			cin >> CF[i];
		}
		double eps = 0.01;
		double x2 = 1, x1 = -1;
		double x = (x2 - x1) / 2;
		while ((x2 - x1) >= eps)
		{
			if (polyval(CF, T, x1) * polyval(CF, T, x) > 0)
			{
				x1 = x;
				x = (x1 + x2) / 2;
			}
			else if (polyval(CF, T, x1) * polyval(CF, T, x) == 0)
			{
				break;
			}
			else
			{
				x2 = x;
				x = (x1 + x2) / 2;
			}
		}
		c = x;
		return c;
	}
	else {
		c = -2;
		return c;
	}
}//多项式利用二分法进行求解EDGE
int main() {
	double c1 = 0.0, c2 = 0.0,c3 = 0.0;
	ddd(c1);
	ddd(c2);
	ddd(c3);
	if (c1 != -2) {
		printf("%.2lf", c1);
		cout << endl;
	}
	if (c2 != -2) {
		printf("%.2lf", c2);
	}
	if (c3 != -2) {
		printf("%.2lf", c3);
	}
	return 0;
}

跳台阶

#include<iostream>
using namespace std;

int jump(int n) {
	if (n == 1 || n == 0) {
		return 1;
	}
	else {
		int c = (jump(n - 2) + jump(n - 1)) % 1000000007;
		return c;
	}
}

int main() {
	int n;
	while (cin >> n) {
		cout << jump(n) << endl;
	}
	return 0;
}//递归解法,时间会超限

最长递减子序列

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[1000],b[1000],c[1000]={0}, res = 1;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j < i; j++) {
			if (a[i] < a[j] && b[i] < (b[j] + 1)) {
				b[i] = b[j] + 1;
				c[i] = j;//核心代码
			}
		}
		if (b[i] > b[res]) {
			res = i;
		}
	}
	stack<int> Q;
	for (int i = res; i != 0; i = c[i])
		Q.push(a[i]);
	cout << Q.top();
	Q.pop();
	while (!Q.empty())
	{
		cout << " " << Q.top();
		Q.pop();
	}
	return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
int dp[502][502], map[502][502];
int x[4] = { 0,0,-1,1 }, y[4] = { -1,1,0,0 };//移动方向
int r, c;//r行c列的二维矩阵

int search(int i, int j) {
	if (dp[i][j] > 1) {
		return dp[i][j];
	}
	for (int k = 0; k < 4; k++) {
		if (i + x[k] > 0 && i + x[k] <= r && j + y[k] > 0 && j + y[k] <= c && map[i][j] > map[i + x[k]][j + y[k]]) 
			dp[i][j] = max(dp[i][j], search(i + x[k], j + y[k]) + 1);
		}
			return dp[i][j];
}

int main() {
	cin >> r >> c;
	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			dp[i][j] = 1;
			cin >> map[i][j];
		}
	}
	int cc = 0;
	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			dp[i][j] = search(i, j);
			cc = max(cc, dp[i][j]);
		}
	}
	cout << cc << endl;
	return 0;
}//好理解一点动态规划

矩阵滑雪场

#include<bits/stdc++.h>
using namespace std;
int n, m, a[1001][1001], ans, fx[4][2] = { 0,1,1,0,-1,0,0,-1 }, step[1001][1001];
int dfs(int x, int y) {
	int mmax = 0;
	if (step[x][y] != 0)return step[x][y];
	step[x][y] = 1;//最小的点为1 
	for (int i = 0; i < 4; i++) {
		int xx = x + fx[i][0];
		int yy = y + fx[i][1];
		if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[xx][yy] < a[x][y])step[x][y] = max(step[x][y], dfs(xx, yy) + 1);
	}
	return step[x][y];
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)cin >> a[i][j], step[i][j] = 0;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			ans = max(ans, dfs(i, j));
		}
	cout << ans;
	return 0;
}

最长子序列

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	int a[1000], b = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int sum = 0;
	for (int i = 0; i < n; i++) {
		if (b > 0) {
			b += a[i];
		}//b>0证明前面子序列是正整数并且予以sum保存
		else {
			b = a[i];
		}
		if (b > sum) {
			sum = b;
		}
	}
	cout << sum;
}//仅要求返回子序列的最大和

元素整除问题

#include<iostream>
using namespace std;
int main() {
	int a[20] = { 0 };
	for (int i = 0; i < 20; i++) {
		cin >> a[i];
	}
	int b[20] = { 0 };
	for (int i = 0; i < 20; i++) {
		int c = a[i];
		for (int j = i + 1; j < 20; j++) {
			if (a[j] % c == 0) {
				b[j] = 1;
			}
		}
	}
	for (int j = 0; j < 20; j++) {
		if (b[j] == 1) {
			cout << a[j] << endl;
		}
	}
}#include<iostream>
using namespace std;
int main() {
	int a[20] = { 0 };
	for (int i = 0; i < 20; i++) {
		cin >> a[i];
	}
	int b[20] = { 0 };
	for (int i = 0; i < 20; i++) {
		int c = a[i];
		for (int j = i + 1; j < 20; j++) {
			if (a[j] % c == 0) {
				b[j] = 1;
			}
		}
	}
	for (int j = 0; j < 20; j++) {
		if (b[j] == 1) {
			cout << a[j] << endl;
		}
	}
}//遍历数组将能被整除的数用另一数组作为标记,最终输出只需输出被标记位置元素

渊子赛马

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int n;
	int a[1005], b[1005];
	while (cin >> n && n) {
		for (int i = 0; i < n; i++) cin >> a[i];
		for (int i = 0; i < n; i++) cin >> b[i];
		sort(a, a + n);
		sort(b, b + n);
		int count = 0;
	    int A_min = 0, A_max = n - 1;
		int B_min = 0, B_max = n - 1;
		while (A_min <= A_max) {
			     if (a[A_max] > b[B_max]) {
                 count++;
                 A_max--;
                 B_max--;
			     }
				 else if (a[A_min] > b[B_min]) {
                 count++;
                 A_min++;
                 B_min++;
					}
                 else {
					count--;
					A_min++;
					B_max--;
			}
		}
    if (count > n / 2) cout << "YES" ;
    else  cout << "NO" ;
	}
	return 0;
}//这个没过
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
int a[1005], b[1005];
void paixu(int* c, int n)
{
	int t, i = 0, j = 0;
	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - i - 1; j++)
		{
			if (c[j] > c[j + 1])
			{
				t = c[j];
				c[j] = c[j + 1];
				c[j + 1] = t;
			}
		}
	}
}
int main()
{
	int n, i, j, win, lose;
	while (1)
	{
		cin >> n;
		if (n == 0)
		{
			break;
		}
		for (i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (i = 0; i < n; i++)
		{
			cin >> b[i];
		}
		paixu(a, n);
		paixu(b, n);
		for (i = 0, j = 0, win = 0, lose = 0; i < n; i++)
		{
			if (a[i] > b[j])
			{
				j++;
				win++;
			}
			else
			{
				lose++;
			}
			if (win > n / 2 || lose > n / 2)
				break;
		}
		if (win > n / 2)
		{
			cout << "YES" << endl;
		}
		else
		{
			cout << "NO" << endl;
		}
	}
	return 0;
}

货币背包问题(这个有格式化输出)


#include<bits/stdc++.h>
using namespace std;
long long int dp[40005];
int coin[] = { 5,10,20,50,100,200,500,1000,2000,5000,10000 };

int main(){
	dp[0] = 1;
	for (int i = 0; i <= 10; i++)
		for (int j = coin[i]; j <= 30000; j++)
			dp[j] += dp[j - coin[i]];

	double n;
	while (cin >> n && n) {
		int m = (int)(n * 100 + 0.0001);

		printf("%6.2f%17lld\n", n, dp[m]);

	}
	return 0;
}

沙子质量

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

const int N = 1000;
int x[N + 1], dp[N + 1][N + 1], s[N + 1];
int n,sum;

int main() {
    while (cin >> n) {
        for (int i = 1; i <= n; i++) {
            cin >> x[i];
            s[i] = s[i - 1] + x[i];
        }
        for (int i = 1; i <= n; i++) {
            dp[i][i] = x[i];
        }
        for (int r = 2; r <= n; r++) {
            for (int i = 1; i <= n - r + 1; i++) {
                int j = i + r - 1;
                dp[i][j] = dp[i + 1][j]+dp[i][j-1];
                for (int k = i + 1; k < j; k++) {
                    int t = min(dp[i][j],dp[i][k] + dp[k + 1][j]+s[j]-s[i-1]);
                    if (t < dp[i][j]) {
                        dp[i][j] = t;
                    }
                }
            }
        }
        cout << dp[1][n] << endl;
    }
    return 0;
}//自己代码,没过待查证
e//沙子的质量(跟矩阵的连乘问题类似,都是自底向上)
#include<iostream>
#include<cstring>
using namespace std;
int f[310][310];
int a[310];
int main(){
    memset(f,0x3f,sizeof f);//将矩阵初始化,各元素无穷大
    int n;
    cin>>n;
    for (int i = 0; i < n; ++i){
        cin>>a[i];
    }
    for (int i = 0; i < n; i++)
        f[i][i] = 0;
    for (int i = 1; i < n; ++i)
        a[i] = a[i] + a[i - 1];
    for (int k = 2; k <= n; ++k){
            for (int i = 0; i + k - 1 < n; ++i){
                    int j = i + k - 1;
                for (int z = i; z < j; ++z){
                    f[i][j] = min(f[i][j],(f[i][z] + f[z+1][j] + a[j] - a[i - 1]));
                }
            }
    }
    cout<<f[0][n - 1]<<endl;
    return 0;
}对比吧

最长公共子序列

# include <iostream>
# include <cstring>
using namespace std;
int f[2010][2010];
char a[2010],b[2010];
int max(int a,int b)
{
   if (a>=b)  return a;
    return b; 
}
int main()
{
    int i,j,len1,len2;    
    cin>>a>>b;
    len1=strlen(a);
    len2=strlen(b);      
    for(i=1;i<=len1;i++)
       {
       for(j=1;j<=len2;j++)
           {
           if (a[i-1]==b[j-1]) f[i][j]=f[i-1][j-1]+1;
           else f[i][j]=max(f[i-1][j],f[i][j-1]);
           }
       }     
 cout<<f[len1][len2];
    return 0;
}

三角形权

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	int a[26][26],dp[26][26]={0}//数组最好还是初始化,尤其大的时候;
	for (int i = 0; i < n; i++) {//C中有memset库函数就是用来初始化
		for (int j = 0; j <= i; j++) {
			cin >> a[i][j];
		}
	}
	for (int i = n - 1; i >= 0; i--)
		for (int j = 0; j <= i; j++)
			dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + a[i][j];

	cout<<dp[0][0];
	return 0;

}

跳跃游戏2

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	int a[101];
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int i = 0;
	int count = 0;
	if (n == 1)
	{
		cout << 0 << endl;
		return 0;
	}//只有一个直接return 0

	while (true)
	{ 
		if (i + a[i] >= n - 1)
		{
			count++;
			break;
		}//终止条件
		int maxNum = -1;
		int maxIndex;
		for (int j = i + 1; j <= i + a[i]; j++)
			if (a[j] >= maxNum)
			{
				maxNum = a[j];
				maxIndex = j;
			}
		i = maxIndex;  // 跳跃到最合适的位置 
		count++;
	}

	cout << count << endl;
	return 0;

}//无法AC通过,贪心算法,存在问题:无法回跳完的情况

homework

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

struct qwe {
	double t;
	double v;
}a[21];

bool cmp(qwe x, qwe y)
{
	return x.v / x.t > y.v / y.t;
}

int main() {
	qwe a[21];
	int M, N;
	while (cin >> M >> N && M && N) {
		for (int i = 0; i < M; i++) {
			cin >> a[i].t >> a[i].v;
		}
		sort(a, a + M, cmp);//按照价值/时间的值进行排序
		double sum = 0;
		int i;
		for (i = 0; i < M; i++)
		{
			if (a[i].t <= N)  // 如果时间够的话
			{
				N -= a[i].t;
				sum += a[i].v;
			}
			else
				break;
		}
		if (i < M)  // 如果能够完成所有题, 就不用进入这个循环了
			sum += 1.0 * a[i].v / a[i].t * N; // 剩余的时间能完成多少价值.

		printf("%.2lf\n", sum);
	}
}//一个背包问题

法师康的工人 (贪心)

#include<iostream>
#include<algorithm>
using namespace std;
struct Worktime {
	int start;
	int end;
};
bool cmp(Worktime a,Worktime b)
{
	if (a.start != b.start)
		return a.start < b.start;
	return a.end < b.end;
}//排序规定
int main()
{
	int N;
	cin >> N;
	Worktime* p = new Worktime[N];
	for (int i = 0; i < N; i++)
	{
		int x;
		cin >> x;
		p[i].start = x;
		int y;
		cin >> y;
		p[i].end = y;
	}
	sort(p, p + N, cmp);
	int max1 = p[0].end - p[0].start;
	int max2 = 0;
	int start = p[0].start;
	int end = p[0].end;
	for (int i = 0; i < N - 1; i += 1)
	{
		if (end >= p[i + 1].start)
		{
			end = max( p[i + 1].end , end);
			max1 = max( end - start , max1 );
		}
		else
		{
			start = p[i + 1].start;
			max2 = max( p[i + 1].start - end , max2);
			end = p[i + 1].end;
		}
	}
	cout << max1 << ' ' << max2 << endl;
}

三值问题

#include<iostream>
using namespace std;

int a[1001], cnt[4];

int main()
{
	int n, i;
	cin>>n;

	for (i = 1; i <= n; i++)
	{
		cin>>a[i];
		cnt[a[i]]++;
	}

	int sum1 = 0, sum2 = 0, sum3 = 0;

	for (i = 1; i <= cnt[1]; i++)
	{
		if (a[i] != 1)
			sum1++;
	}

	for (i = cnt[1] + 1; i <= cnt[1] + cnt[2]; i++)
	{
		if (a[i] == 3)
			sum2++;
	}

	for (i = cnt[1] + cnt[2] + 1; i <= n; i++)
	{
		if (a[i] == 2)
			sum3++;
	}


	cout<<sum1 + (sum2 > sum3 ? sum2 : sum3);

	return 0;
}

思路:首先分析,无论交换前的数据如何,交换后一定可以分为三部分,第一部分是数据“1”,第二部分是数据“2”,第三部分是数据“3”,由于“1”永远在最前面,所以我门可以先固定第一部分,然后讨论第二、三部分

(1)首先找出数据中 “1”的个数sum1,即完成第一部分排序需要的步数
(2)然后要做的就只有 将第二部分中的“3”交换到第三部分、或者将第三部分的“2”交换到第二部分,分别记为sum2和sum3
(3)取sum1+max(sum2,sum3),至于为什么要取sum2和sum3之间的最大值,是因为如果取最小值,那么必定会有“2”遗留在第三部分或者“3”遗留在第二部分,取决于两者到底谁小

区间包含问题

#include <iostream>
#include <algorithm>
using namespace std;
int m, n;
struct node {
    int s, e;
}a[100010], b[100010];
int cmp(node a, node b) {
    if (a.e == b.e)
        return a.s < b.s;
    return a.e < b.e;
}
int fun1(node s) {
    int num1 = 0, num2 = 0;
    int left = 1;
    int right = m;
    int mid;
    while (left < right) {
        mid = (left + right) / 2;
        if (a[mid].e == s.e)
            return mid;
        else if (a[mid].e > s.e)
            right = mid - 1;
        else
            left = mid + 1;
    }
    return mid;
}//快排方法
int main() {
    while (cin >> m >> n)
    {
        for (int i = 1; i <= m; i++)
            cin >> a[i].s >> a[i].e;
        sort(a + 1, a + 1 + m, cmp);
        for (int j = 1; j <= n; j++)
        {
            int num = 0;
            cin >> b[j].s >> b[j].e;
            int t = fun1(b[j]);
            for (int i = 1; i <= t + 1; i++)
                if (a[i].s >= b[j].s && a[i].e <= b[j].e)
                {
                    num++;
                    b[j].s = a[i].e;
                }
            cout << num << endl;
        }
    }
    return 0;
}

The Hardest Problem Ever

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i, n;
    char a[205], str[205];
    char s[27] = { 'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U' };//字符串空间最好开大点
    while (cin.getline(a, 205))//多组输入
    {
        if (strcmp(a, "ENDOFINPUT") == 0)//如果输入的字符串a,与ENDOFINPUT相同,那么就跳出循环
        {
            break;
        }
        if (strcmp(a, "START") == 0)//如果输入的字符串a,与START相同,那么就执行以下程序
        {
            cin.getline(str, 205);
            n = strlen(str);
            for (i = 0; i < n; i++)
            {
                if (str[i] >= 'A' && str[i] <= 'Z')
                {
                    str[i] = s[str[i] - 'A'];//减去A对应的ASCII码就是相应的明文
                }
            }
            cout<<str;
        }
    }
    return 0;
}

8皇后问题

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

const int n = 8;

int tmp, ans;
int x[10];
int chess[10][10];

bool notDanger(int r, int c) {
	for (int i = 0; i < r; i++) {
		if (x[i] == c) return false;
		if (abs(r - i) == abs(c - x[i])) return false;
	}
	return true;
}

void queen(int k) {//放入第k行的皇后
	if (k == n) { 
		ans = max(ans, tmp); 
		return; }//所有行都尝试完毕,即求出了相应一组解

	for (int i = 0; i < n; i++) {
		if (notDanger(k, i)) {//没有冲突,尝试下一行
			x[k] = i;
			tmp += chess[k][i];
			queen(k + 1);
			x[k] = -1;//注意下层递归结束后及时更新相应变量值
			tmp -= chess[k][i];
		}
	}
}

int main() {
	int k;
	cin>>k;
	while (k--) {
		tmp = ans = 0;
		x[10] = { 0 };
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++)
				cin>>chess[i][j];
		}
		queen(0);
		cout << ans;
	}
	return 0;
}

无脑博士的试管们

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

int a, c, b, A, B, C;//a,b,c代表此时A,B,C容器有多少水 
int vis[25][25];//用VIS数组来表示每次变换时的A,B杯的状态 
void dfs(int a, int b, int c) {
	vis[a][b] = 1;//状态A杯为a,B杯为b 
	if (a < A) //当A容器没有装满时的情况 
	{
		if (c >= A - a && vis[A][b] == 0)dfs(A, b, c - A + a);//从c倒入a中,C容器里的大于A剩余空间,可装满 
		if (c < A - a && vis[a + c][b] == 0)dfs(a + c, b, 0); //c倒入a中,C容器里的液体小于A的剩余空间,以下也是 
		if (b >= A - a && vis[A][b - A + a] == 0)dfs(A, b - A + a, c);
		if (b < A - a && vis[a + b][0] == 0)dfs(a + b, 0, c);
	}
	if (b < B) {
		if (c >= B - b && vis[a][B] == 0)dfs(a, B, c - B + b);
		if (c < B - b && vis[a][b + c] == 0)dfs(a, b + c, 0);//从c倒入b 
		if (a >= B - b && vis[a - B + b][B] == 0)dfs(a - B + b, B, c);//从a倒入b 
		if (a < B - b && vis[0][a + b] == 0)dfs(0, a + b, c);
	}
	if (c < C) {
		if (a >= C - c && vis[a - C + c][b] == 0)dfs(a - C + c, b, C);
		if (a < C - c && vis[0][b] == 0)dfs(0, b, a + c);;//从a倒入c 
		if (b >= C - c && vis[a][b - C + c] == 0)dfs(a, b - C + c, C);
		if (b < C - c && vis[a][0] == 0)dfs(a, 0, b + c);//从b倒入c 
	}
}


int main() {
	cin >> A >> B >> C;
	vis[25][25] = { 0 };
	dfs(0, 0, C);
	int flag = 1;
	for (int i = B; i >= 0; i--) {
		if (vis[0][i]) {
			if (flag) flag = 0;//这个flag的存在可以去出最后多出来的空格
			else cout << " ";
			cout << C - i;
		}
	}
	return 0;
}

图的m着色问题

#include<iostream>
using namespace std;
int n, k, m, sum = 0;
int color[20000];
int connect[2000][2000];
void dfs(int d)
{
	if (d == n + 1)//所有顶点全部着色
	{
		sum++;//着色方案
		return;
	}
	for (int i = 1; i <= m; i++)//循环颜色
	{
		bool flag = 1;
		for (int j = 1; j <= n; j++)
		{
			if (connect[d][j] && color[j] == i)
			{
				flag = 0;
				break;
			}
		}
		if (flag)
		{
			color[d] = i;//尝试分配颜色;
			dfs(d + 1);
			color[d] = 0;//不行则回退;
		}
	}
}
int main()
{
	cin >> n >> k >> m;
	for (int i = 0; i < k; i++)
	{
		int tmp1, tmp2;
		cin >> tmp1 >> tmp2;
		connect[tmp1][tmp2] = 1;
		connect[tmp2][tmp1] = 1;
	}
	dfs(1);
	cout << sum;
	return 0;
}

组合运算式

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[20];
int n;
void DFS(int pos, int sum, int last)///三个参数:当前位置,不含当前位置的和,当前位置上的数
{
    int i;
    if (pos == n)
    {
        if (sum + last == 0)
        {
            cout<<s;
            cout << endl;
        }
        return;
    }
    s[pos * 2 - 1] = ' ';  //搜索‘ ’号
    if (last > 0)
    {
        DFS(pos + 1, sum, last * 10 + pos + 1);
    }
    else
    {
        DFS(pos + 1, sum, last * 10 - pos - 1);
    }
    s[pos * 2 - 1] = '+';  //搜索‘+’号
    DFS(pos + 1, sum + last, pos + 1);
    s[pos * 2 - 1] = '-';  //搜索‘-’号
    DFS(pos + 1, sum + last, -(pos + 1));
}

int main()
{
    int i;
    cin>>n;
    for (i = 0; i < n; i++)
    {
        s[i * 2] = i + '1';
    }
    DFS(1, 0, 1);
    return 0;
}

凯撒加密算法

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

int main() {
	int T,s;
	cin >> T;
	while (T--) {
		string p;
		cin >> p;
		cin >> s;
		s %= 26;
		for (int i = 0; i < p.length(); i++) {
			if (p[i] >= 'A' && p[i] <= 'Z')
				p[i] = 'A' + (p[i] - 'A' - s + 26) % 26;
			if (p[i] >= 'a' && p[i] <= 'z')
				p[i] = 'a' + (p[i] - 'a' - s + 26) % 26;
		}
		cout << p << endl;
	}
}

简单的密码

#include<bits/stdc++.h>
using namespace std;
int n, f[35] = { 0,0,0,1 };
int main()
{
    for (int i = 4; i <= 30; i++)
        f[i] = 2 * f[i - 1] + (1 << (i - 4)) - f[i - 4];
    while (cin >> n)
        cout << f[n] << endl;
    return 0;
}

数据加密

#include<bits/stdc++.h>
using namespace std;
int n, tip, top;
string s;
int main()
{
    while (cin >> n)
    {
        cin >> s;
        tip = 0; 
        top = n - 1;
        while (tip <= top)
        {
            if (s[tip] < s[top])
            {
                cout << s[tip]; 
                tip++;
            }
            else if (s[tip] > s[top])
            {
                cout << s[top];
                top--;
            }
            else
            {
                int ti = tip, to = top, f = 0;
                while (ti <= to)
                {
                    ti++; 
                    to--;
                    if (s[ti] < s[to])
                    {
                        f = 0; 
                        break;
                    }
                    else if (s[ti] > s[to])
                    {
                        f = 1; 
                        break;
                    }
                }
                if (f)
                {
                    cout << s[top];
                    top--;
                }
                else
                {
                    cout << s[tip]; 
                    tip++;
                }
            }
        }
        cout << endl;
    }
    return 0;
}


posted @ 2024-06-24 17:05  TTDB  阅读(30)  评论(0)    收藏  举报