大一上 | 【C++】代码整理

大一上 | 【C++】代码整理

仅记录一下大一上写下的“垃圾”代码

1. 二进制转十进制

#include<iostream>
using namespace std;
int main()
{
	int dec = 0;
	char ch;
	cout << "binary = ";
	do
	{
		// 从输入流中读取一个字符赋给 ch
		cin.get(ch);
	} while (ch != '0' && ch != '1');
	do
	{
		int x = ch - '0';
		dec += x;
		cin.get(ch);
		if (ch == '0' || ch == '1')
		{
			dec *= 2;
		}
	} while (ch == '0' || ch == '1');
	cout << "decimal = " << dec;
	return 0;
}

 

2. 十进制转二进制

#include<iostream>
using namespace std;
int main()
{
	int x, s[16] = { 0, };
	cout << "decimal = "; 
	cin >> x;
	for (int i = 15; i >= 0; i--)
	{
		s[i] = x % 2;
		x /= 2;
	}
	cout << "binary = ";
	for (int i = 0; i <= 15; i++)
	{
		cout << s[i];
		if ((i + 1) % 8 == 0)
		{
			cout << " ";
		}
	}
	return 0;
}

 

3. XYZ+YZZ=532的解

#include<iostream>
using namespace std;
int main()
{
	int X, Y, Z;
	cout << "XYZ+YZZ=532的解为" << endl;
	for (X = 0; X != 9; X++)
		for (Y = 0; Y != 9; Y++)
			for (Z = 0; Z != 9; Z++)
				if (532 == (Y + X) * 100 + (Y + Z) * 10 + Z * 2)
				{
					cout << "X=" << X << endl;
					cout << "Y=" << Y << endl;
					cout << "Z=" << Z << endl;
				}
	return 0;
}

 

4. 输出 2-20内的素数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	bool b;
	for (int x = 2; x <= 20; x++)
	{
		b = true;
		for (int j = 2; j <= sqrt(1.0 * x); j++)
		{
			if (x % j == 0)
			{
				b = false;
			}
		}
		if (b)
			cout << x << " ";
	}
	return 0;
}

 

5. 求圆周率Π (小数点后六位)

这里用到的公式是: π 4 ≈ 1 1 − 1 3 + 1 5 − 1 7 ⋯ \frac{\pi}{4}\approx \frac{1}{1}-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}\cdots 4π1131+5171

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	double old_sum = 0.0;
	double sum = 1.0, s = 1.0;
	int n = 1;
	while (abs(sum-old_sum)>1e-6)
	{
		old_sum = sum;
		s = s * (-1);
		n = n + 2;
		sum += s / n;
	}
	double pi = 4 * sum;
	printf("%f", pi);
	return 0;
}

 

6. 公鸡母鸡小鸡问题

公鸡5元一只,母鸡3元一只,小鸡1/3元一只,给100元,则公鸡,母鸡,小鸡各买多少只?

#include<iostream>
using namespace std;
int main()
{
	short int x, y, z;
	cout << "cock\t" << "hen\t" << "chick" << endl;
	for (x = 0; x < 20; x++)
	{
		for (y = 0; y < 33; y++)
		{
			z = 100 - x - y;
			if (5.0 * x + 3.0 * y + z / 3.0 == 100)
				cout << x << '\t' << y << '\t' << z << endl;
		}
	}
	return 0;
}

 

7. 10进制转8进制

#include<iostream>
using namespace std;
int main()
{
	long S;
	int n, m = 0, T[100];
	cout << "pleast importer your num" << endl;
	cout << "Decimal:";
	cin >> S;
	while (S != 0)
	{
		n = S % 8;
		S = S / 8;
		T[++m] = n;
	}
	int k = m;
	cout << "Octonary:";
	while (k >= 1)
	{
		cout << T[k];
		k--;
	}
	return 0;
}

 

8. 十进制转二进制

#include<iostream>
using namespace std;
int main()
{
	long S;
	int n, m=0, T[100];
	cout << "Please input your number" << endl;
	cout << "Decimal:";
	cin >> S; 
	while (S != 0)
	{
		n = S % 2;
		S = S / 2;
		T[++m] = n;
	}
	cout << "Binary:";
	for (int k = m; k>=1; k--)
		cout << T[k];
	return 0;
}

 

9. 等差数列

#include<iostream>
using namespace std;
int main()
{
	long an, S;
	int a1, d, n, m;
	cout << "首项:" << endl;
	cin >> a1;
	cout << "公差: " << endl;
	cin >> d;
	cout << "项数: " << endl;
	cin >> n;
	an = a1 + (n - 1) * d;
	cout << "所以an=" << an << endl;
	S = 0; m = 1;
	while (m <= n)
	{
		an = a1 + (m - 1) * d;
		S = S + an;
		m++;
	}
	cout << "前n项等于:" << S << endl;
	return 0;
}

 

10. 最大公约数

#include<iostream>
using namespace std;
int main()
{
	int m, n;
	cout << "input 2 numbers(the first number is the bigger one)" << endl;
	cout << "?	"; cin >> m;
	cout << "?	"; cin >> n;
	int temp = n;
	cout << m << " and " << n << " maximal common divisor is ";
	while (temp)
	{
		temp = m % n;
		m = n;
		n = temp;
	}
	cout << m;
	return 0;
}

 

11. 斐波那契数列

#include<iostream>
using namespace std;
int main()
{
	long long int a0 = 0, a1 = 1, a2;
	short int n;
	cout << "n = "; cin >> n;
	cout << a0 << '\t' << a1 << '\t';
	for (int i = 3; i <= n; i++)
	{
		a2 = a0 + a1;
		cout << a2 << '\t';
		a0 = a1;
		a1 = a2;
		if (i % 10 == 0) cout << endl;
	}
	return 0;
}

 

12. 打印圣诞树

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int n; char ch;
	cout << "How many lines ?\t"; cin >> n;
	cout << "What character ?\t"; cin >> ch;
	for (int i = 0; i < n; i++)
	{
		cout << setw((2 * n + 1) / 2 - i) << ch;
		for (int j = 1; j < (2 * i + 1); j++)
			cout << ch;
		cout << endl;
	}
	for (int k = 0; k < n / 2; k++)
		cout << setw(n - 1) << ch << ch << ch << endl;
	return 0;
}
// Sample Run
// How many lines ?        6
// What character ?        *
//      *
//     ***
//    *****
//   *******
//  *********
// ***********
//     ***
//     ***
//     ***

 

13. 求100内的素数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int m, k, i, n = 0;
	for (m = 2; m <= 100; m++)
	{
		k = int(sqrt(double(m)));
		i = 2;
		while (m % i && i <= k)
			i++;
		if (i > k)
		{
			cout << m << '\t';
			n++;
			if (n % 5 == 0) cout << endl;
		}
	}
	return 0;
}

 

14. 牛顿法迭代求根

/*
	f(x) = x^3 + x^2 + 10x - 20
	f'(x) = 3x^2 + 2x + 10
	牛顿迭代公式:
		x(n+1) = x(n) - f(xn) / f'(xn)
*/
#include<iostream>
#include<cmath>
using namespace std;
double f1(double x);
double f2(double x);
int main()
{
	double x, x0, epson;
	cout << "input test root, x0 = ";
	cin >> x0;
	cout << "input precision, epson = ";
	cin >> epson;
	do
	{
		x = x0;
		x0 = x - f1(x) / f2(x);
	} while (fabs(x0 - x)>epson);
	cout << "the root is:" << x0;
	return 0;
}

double f1(double x)
{
	double y = pow(x, 3) + pow(x, 2) + 10 * x - 20;
	return y;
}
double f2(double x)
{
	double y = 3 * pow(x, 2) + 2 * x + 10;
	return y;
}

 

15. 计算圆的周长和面积 / 球的面积和体积

#include<iostream>
using namespace std;

const double PI = 3.14159;
typedef double funType(double);
funType circlePerimeter, circleArea, ballArea, ballVolume;
double callFun(funType* qr, double r);

int main()
{
	double r;
	cout << "please input your number of radius: "; cin >> r;
	cout << endl;

	cout << "the circlePerimeter is " << callFun(circlePerimeter, r) << endl << endl;

	cout << "the circleArea is " << callFun(circleArea, r) << endl << endl;

	cout << "the ballArea is " << callFun(ballArea, r) << endl << endl;

	cout << "the ballVolume is " << callFun(ballVolume, r) << endl << endl;
	return 0;
}

double circlePerimeter(double r)
{
	return 2 * PI * r;
}

double circleArea(double r)
{
	return PI * r * r;
}

double ballArea(double r)
{
	return 4 * PI * r * r;
}

double ballVolume(double r)
{
	return 4.0 / 3.0 * PI * r * r * r;
}

double callFun(funType* qr, double r)
{
	return qr(r);
}

 

16. 链表的基本操作

  1. define.h
    #include<iostream>
    using namespace std;
    struct node
    {
    	int data;
    	node* next;
    };
    
    typedef struct node* List;
    
    void construct(List& L, int a[], int n);
    
    void print(List L);
    
    void destroy(List& L);
    
  2. func.cpp
    #include"define.h"
    
    void print(List L)
    {
    	struct node* p = L;
    	while (p)
    	{
    		cout << p->data << endl;
    		p = p->next;
    	}
    }
    
    void construct(List &L, int a[], int n)
    {
    	if (L || n < 1)
    		return;
    
    	struct node* p;
    	struct node* rear = L;
    
    	p = (struct node*)malloc(sizeof(struct node));
    	p->data = a[0];		
    	p->next = NULL;
    	L = p;
    	rear = L;
    
    	for (int i = 1; i < n; i++)
    	{
    		p = (struct node*)malloc(sizeof(struct node));
    		p->data = a[i];
    		p->next = NULL;
    		rear->next = p;
    		rear = rear->next;
    	}
    	//return true;
    }
    
    void destroy(List &L)
    {
    	struct node* p = L;
    	while (L)
    	{
    		L = L->next;
    		free(p);
    		p = L;
    	}
    }
    
  3. main.cpp
    #include"func.cpp"
    
    int main()
    {
    	int a[10], i;
    	for (i = 0; i < 10; i++)
    	{
    		a[i] = i;
    	}
    	List L = NULL;;
    	construct(L, a, 10);
    	print(L);
    	cout << "---------------------------------------" << endl;
    	destroy(L);
    	print(L);
    	return 0;
    }
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值