C++算法题_01

  • 01背包问

  

#include<iostream>
using namespace std;
#include <algorithm>
 
int main()
{
	int w[5] = { 0 , 2 , 3 , 4 , 5 };			//商品的体积2、3、4、5
	int v[5] = { 0 , 3 , 4 , 5 , 6 };			//商品的价值3、4、5、6
	int bagV = 8;					        //背包大小
	int dp[5][9] = { { 0 } };			        //动态规划表
 
	for (int i = 1; i <= 4; i++) {
		for (int j = 1; j <= bagV; j++) {
			if (j < w[i])
				dp[i][j] = dp[i - 1][j];
			else
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
		}
	}
 
	//动态规划表的输出
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 9; j++) {
			cout << dp[i][j] << ' ';
		}
		cout << endl;
	}
 
	return 0;
}
/*
#include<iostream>
using namespace std;
#define N 6
#define W 21

int B[N][W] = { 0 };
int w[N] = { 0, 2, 3, 4, 5, 9 };
int v[N] = { 0, 3, 4, 5, 8, 10 };

void knapsack()
{
	int k, c;
	for (k = 1; k < N; k++)
	{
		for (c = 1; c < W; c++)
		{
			if (w[k] > c)
				B[k][c] = B[k - 1][c];
			else
			{
				int value1 = B[k - 1][c - w[k]] + v[k];
				int value2 = B[k - 1][c];
				if (value1 > value2)
				{
					B[k][c] = value1;
				}
				else
				{
					B[k][c] = value2;
				}
			}
		}
	}
}

int main()
{
	knapsack();
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < W; j++)
		{
			cout << B[i][j] << ' ';
		}
		cout << endl;
	}  
	cout << B[5][20] << endl;
	return 0;
}
*/

  

  • 心型
#include <stdio.h>
#include <math.h>

float f(float x, float y, float z) {
    float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
    return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}

float h(float x, float z) {
    for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
        if (f(x, y, z) <= 0.0f)
            return y;
    return 0.0f;
}

int main() {
    for (float z = 1.5f; z > -1.5f; z -= 0.05f) {
        for (float x = -1.5f; x < 1.5f; x += 0.025f) {
            float v = f(x, 0.0f, z);
            if (v <= 0.0f) {
                float y0 = h(x, z);
                float ny = 0.01f;
                float nx = h(x + ny, z) - y0;
                float nz = h(x, z + ny) - y0;
                float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
                float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
                putchar(".:-=+*#%@"[(int)(d * 5.0f)]);
            }
            else
                putchar(' ');
        }
        putchar('\n');
    }
}

3n+1问题

/* 
   考虑如下的序列生成算法:从整数 n 开始,如果 n
   是偶数,把它除以 2;如果 n 是奇数,把它乘 3
   加1。用新得到的值重复上述步骤,直到 n = 1
   时停止。例如,n = 22 时该算法生成的序列是:

   22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1



   人们猜想(没有得到证明)对于任意整数
   n,该算法总能终止于 n = 1。这个猜想对于至少 1 000
   000内的整数都是正确的。



   对于给定的 n,该序列的元素(包括 1)个数被称为 n
   的循环节长度。在上述例子中,22 的循环节长度为
   16。输入两个数 i 和 j,你的任务是计算 i 到 j(包含 i 和 
   j)之间的整数中,循环节长度的最大值。

 */
#include<iostream>
using namespace std;
int e=0;
int cxk(int n)
{
	e++;
	if (n == 1)
	{
	//	cout<<1<<endl;
		return e;
	}
	if (n % 2 == 0)
	{
		//cout << n << endl;
		n /= 2;
	}
	else
	{
		//cout << n << endl;
		n = n * 3 + 1;
	}
	cxk(n);

}

int main()
{
//cout<<cxk(20);
	int i,j;
	cin>>i>>j;
	int b[j-i];
	for(int a=i,c=1;a<=j;a++)
	{
		b[c++]=cxk(a);
		}
	for(int d=1;d<=j-i;d++)
	cout<<b[d]<<" ";
	return 0;
	}

类的使用

/* 实现类和继承,具体要求如下:
   定义一个Person类,包括如下基本信息:姓名,性别,年龄。
   姓名要定义为字符类型指针变量,其他自定义。
   定义构造函数完成赋值。其中姓名要用new创建空间
   定义无参构造函数,变量设置为空或0
   定义析构函数,完成动态空间回收。
   定义带返回值的函数,获取年龄。
   定义带返回值的函数,获取性别。
   定义显示函数show,完成值的显示,要给出完整的提示信息。
   定义Student类,公有继承Person类,
   要求:增加两个变量,班级和学号。班级要定义为字符类型指针变量
   定义构造函数完成赋值,其中班级要用new创建动态空间。
   定义析构函数,完成动态空间回收。
   定义显示函数show,完成值的显示,要给出完整的提示信息。(保留父类内容,增加新内容) 
 */
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
  char *name;
  int age;
  string sex;
public:
    Person(char *N, int a, string s) {
    int len = strlen(N);
    if (len <= 0)
        name = 0;
    else {
      name = new char[len + 1];
        strcpy(name, N);
    } sex = s;
    age = a;
  }
  Person() {
  }
  ~Person() {
    if (name != 0)
      delete[]name;
  }
  int getAge() {
    return this->age;
  }
  string getSex() {
    return sex;
  }
  void show() {
    cout << "\n姓名:" << name << "\n年纪:" << age << "\n性别:" <<
      sex;
  }
};
class Student:public Person {
private:
  char *grade;
  string IDnumber;
public:
    Student(char *N, int a, string s, char *G, string ID):Person(N, a, s) {
    int len2 = strlen(G);
    if (len2 <= 0)
        grade = 0;
    else {
      grade = new char[len2 + 1];
        strcpy(grade, G);
    } IDnumber = ID;
  }
  ~Student() {
    if (grade != 0)
      delete[]grade;
  }
  void show() {
    Person::show();
    cout << "\n年级:" << grade << "\n学号:" << IDnumber;
  }
};
int main() {
  Person a("王丽", 21, "女");
  a.show();
  cout << "\n\n性别:" << a.getSex() << "\n" << "年龄:" << a.getAge() << endl;
  Student b("王丽", 21, "女", "17计算机6班", "511721010666");
  b.show();
  cout << endl;
  return 0;
}

三角形,菱形,九九乘法表

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"请输入行数n:";
    cin>>n;
    for(i=0;i<n;i++)//大循环,行数
    {
    	for(j=0;j<n-i;j++)//n-j是直角三角形
    	cout<<" "; 	
    	for(j=0;j<2*i+1;j++)
    	//小循环,输出空格*公式。
    	cout<<"*";
    	cout<<endl;
    	}
    	/*倒三角*/
    	   i=0,j=0;
    for(i=0;i<n+1;i++)
    {
    	for(j=0;j<i;j++)
    	cout<<" ";
    	for(j=0;j<(2*n+1)-2*i;j++)
    	cout<<"*";
    	cout<<endl;
    	}
  cout<<endl;
 
 i=0,j=0;
    for(i=0;i<n;i++)
    {
    	for(j=0;j<n-i;j++)//n-j是直角三角形
    	cout<<" ";
    	for(j=0;j<2*i+1;j++)
    	cout<<"*";
    	cout<<endl;
    	}
    	
    	cout<<endl;
  i=0,j=0;
    for(i=0;i<n+1;i++)
    {
    	for(j=0;j<i;j++)
    	cout<<" ";
    	for(j=0;j<(2*n+1)-2*i;j++)
    	cout<<"*";
    	cout<<endl;
    	}
    	cout<<endl;
  /*乘法表*/  	
    	int a,b;
for(a=1;a<=9;a++)//行数
{
	for(b=1;b<=a;b++)//列数
cout<<b<<"x"<<a<<"="<<a*b<<" " ;//输出
cout<<endl;
}
    return 0;
}
  • 与7无关的数
    #include<iostream>
    using namespace std;
    int main()
    {
    	int n;
    	cin>>n;
    	long long sum = 0;
    	for (int i = 0; i <= n; i++)
    	{
    
    		if (i % 7 != 0 && i % 10 != 7 && i / 10 != 7)
    		{
    			cout << i << endl;
    			sum += i * i;
    		}
    	}
    	cout << sum;
    	// cout<<7%7;
    	return 0;
    }
发布了21 篇原创文章 · 获赞 14 · 访问量 4107

猜你喜欢

转载自blog.csdn.net/Cxk___/article/details/103253161