大一以来C/C++基础学习入门练习记录(4)

写在前面的话
这个是楼主大一入门时的课程作业代码,很多当时也写得较为粗糙,
后来逐渐掌握后也没进一步优化的想法和必要,此文纯粹记录,参考作用不大。

实验平台为Visual-Studio-2017
前面的 #include "pch.h"
文件如下:

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#ifndef PCH_H
#define PCH_H
// TODO: 添加要在此处预编译的标头
#endif //PCH_H
  1. 阈值下的圆周率
#include "pch.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double a;
	double sum = 1.0, m = 1.0, n = 1.0,i,j;
	i = 0.0;  j = n;
	cin >> a;
	while ((m/n)>=a)
	{
		i = i + 1;
		j = j + 2;
		m = m * i;
		n = n * j;
		sum = sum + (m / n);
	}
	
	cout <<fixed<<setprecision(20)<< 2 * sum << endl;
	system("pause");
}
  1. 龟兔赛跑
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int T;
	int m=0, n=0,time=0;  //m 兔子距离 n乌龟距离 time 过去的时间
	cin >> T;
	while (time != T)
	{
		m = m + 9;
		n = n + 3;
		time = time + 1;
		if ((time % 10 == 0 && (m > n)))
		{
			if (time < (T - 30))
			{
				n = n + 90;
				time = time + 30;
			}
			else
			{
				n = n + 3 * (T - time);
				break;    //这个break非常重要 否则会重复前三步计算
			}
		}
	}
	if (m > n) { cout <<"^_^" << " " << m; }
	else if (m < n) { cout << "@_@"<< " " << n; }
	else { cout <<"-_-" << " " << m; }
	system("pause");
}
  1. 简单计算器
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int m, n,result;
	char ch;
	cin >> m >> ch >> n;
	switch (ch)
	{
	case '+': {result = m + n; cout << result << endl; break; }
	case '-': {result = m - n; cout << result << endl; break; }
	case '*': {result = m * n; cout << result << endl; break; }
	case '/': {result = m / n; cout << result << endl; break; }
	case '%': {result = m % n; cout << result << endl; break; }
	default:
		cout << "ERROR" << endl;
		break;
	}
	system("pause");
}
  1. 数列求和 进阶版
#include "pch.h"
#include <iostream>
using namespace std;
int num[100000000];
int main()
{
	int A, N;
	int t, jinwei, X;
	cin >> A >> N;
	X = N; jinwei = 0;
	if (A == 0 || N == 0) { cout << "0"; }
	else
	{
		for (; N > 0; N = N - 1)
		{
			t = A * N + jinwei;
			jinwei = t / 10;
			num[N] = t % 10;
		}
		if (jinwei > 0) { num[N + 1] = t; }
		for (; N < X; N = N + 1)
		{
			cout << num[N + 1];
		}
	}
	cout << endl;
	system("pause");
}
  1. 删除字符串
#include "pch.h"
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s1, s2;
	getline(cin, s1);            //行输入
	getline(cin, s2);

	while (s1.find(s2) < s1.length())        //判断S1中是否还存在S2
	{
		s1 = s1.erase(s1.find(s2), s2.length());        //将S1中的S2删除掉
	}
	cout << s1 << endl;

	system("pause");
	//cin.get();
	//cin.get();
}
  1. 皮球的反复恒跳
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	double m, n, sum = 0;
	cin >> m >> n;
	while (n > 0)
	{
		sum = sum + m;
		m = m / 2;
		n = n - 1;
	}
	cout << "sum = " << sum;
	system("pause");
}
  1. swap 数组交换[最大与最小交换]
#include "pch.h"
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void swap(int *a, int *b)
{
	int t;
	t = *a;
	*a = *b;
	*b = t;
}
int main() {
	vector<int>s1 = { 5,3,40,2,1 };

	int x = 0;
	while (x<=4)
	{
		cout << s1[x] << " ";
		x = x + 1;
	}

	cout << endl << endl;
	system("pause");
	cout << endl << endl;

	vector<int>::iterator biggest;
	biggest = max_element(s1.begin(), s1.end());
	 // cout << distance(s1.begin(), biggest) << " ";
	// cout << *biggest << endl;
	vector<int>::iterator min;
	min = min_element(s1.begin(), s1.end());
	// cout << distance(s1.begin(), min) << " ";
	// cout << *min << endl;
	swap(&s1[distance(s1.begin(), biggest)], &s1[distance(s1.begin(), min)]);
	x = 0;
	while (x <= 4)
	{
		cout << s1[x] << " ";
		x = x + 1;
	}
	cout << endl << endl;
	system("pause");
}
  1. 拆数使乘积最大(题眼为:3)
#include "pch.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()  //3N,3N+1,3N+2
{
	long long int N, M,x,y;
	cin >> N;
	cout << endl;
	x = N % 3;
	y = N / 3;
	//if (N == 1) { M = 1; }
		switch (x)
		{
		case 1:M = 4 * pow(3, y - 1); break;
		case 2:M = 2 * pow(3, y);  break;
		case 0:M = pow(3, y); break;
		}
	cout << M << endl;
	system("pause");
}
  1. 数学黑洞
#include "pch.h"
#include <iostream>
using namespace std;

void imax(int a[])
{
	int x = 0,y,t,p;
	while (x<4)
	{
		y = x; p = x;
		while (a[x + 1] > a[x] && y >= 0)
		{
			t = a[x];
			a[x] = a[x + 1];
			a[x + 1] = t;
			y = y - 1;
			x = x - 1;
		}
		x = p+1;
	}

}

int main()
{
	int max, min, magic,a[4],n=0;
	cout << "请输入一个4位的自然数"<<endl;
	cin >> magic;

	while (magic != 6174&&n<15)
    {
		a[3] = magic % 10;
		a[2] = magic / 10 % 10;
		a[1] = magic / 100 % 10;
		a[0] = magic / 1000;

		imax(a);

		max = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
		min = 1000 * a[3] + 100 * a[2] + 10 * a[1] + a[0];
		magic = max - min;
		cout << max << " - " << min << " = " << magic;
		cout << endl;

		n = n + 1;
	}
	cout << "一共经过 " << n << " 步";
	cout << endl;
	system("pause");
}
  1. 递推法-小蜜蜂[ACM]
#include "pch.h"
#include <iostream>
using namespace std;
long long int a[100];
int main()
{
	int x, y, N,t=3;
	cin >> N;
	
	a[0] = 0;
	a[1] = 1;
	a[2] = 2;

	while (t <= 100)
	{
		a[t] = a[t - 1] + a[t - 2];
		t++;
	}

	while (N>0)
	{
		
		cin >> x >> y;
		if (y - x < 0) { cout << "error" << endl << endl; }
		else
		{
			cout << a[y - x] << endl << endl;
		}
		N = N - 1;
	}
	system("pause");
}

题解公式

F(1)=0
F(2)=1
n>=3ʱ
F(n)=f(n-1)+f(n-2);


#include <iostream>
using namespace std;
long long int a[100];
int main()
{
	int x, y, N,t=3;
	cin >> N;
	
	a[0] = 0;
	a[1] = 1;
	a[2] = 2;

	while (t <= 100)
	{
		a[t] = a[t - 1] + a[t - 2];
		t++;
	}

	while (N>0)
	{
		
		cin >> x >> y;
		if (y - x < 0) { cout << "error" << endl << endl; }
		else
		{
			cout << a[y - x] << endl << endl;
		}

		N = N - 1;
	}

	system("pause");
}
  1. 面向对象实践 圆与单价
#include "pch.h"
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
const float FencePrice = 35;
const float ConcretePrice = 20;
using namespace std;

class Circle
{
public:

	Circle(float r);  //构造函数

	float Circumference() const;
	float Area() const;

private:
	float radius;
};  //定义类及其数据和方法   构造函数初始化数据成员radius

Circle::Circle(float r)
{
	radius = r;
}  //半径  构造函数

float Circle::Circumference() const
{
	return    2 * M_PI*radius;
} //周长

float Circle::Area() const
{
	return M_PI * radius*radius;
} // 面积

/*Circle::~Circle()
{
}  析构函数的写法*/

int main()
{
	float radius;
	float FenceCost, ConcreteCost;

	cout << "Enter the radius of the pool :";
	cin >> radius;

	Circle Pool(radius);
	Circle PoolRim(radius + 3);      //声明Circle 对象

	FenceCost = PoolRim.Circumference()*FencePrice;
	cout << "Fencing Cost is $" << FenceCost << endl;

	ConcreteCost = (PoolRim.Area() - Pool.Area())*ConcretePrice;
	cout << "Concrete Cost is $" << ConcreteCost << endl;
	system("pause");
}

48.结构体与成绩输入管理

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

class ExamInfo
{
public:
	ExamInfo(string name,char grade):
		name(name),mode(GRADE),grade(grade){}
	ExamInfo(string name, bool pass) :
		name(name), mode(PASS), pass(pass) {}
	ExamInfo(string name, int percent) :
		name(name), mode(PERCENTAGE), percent(percent) {}
	void show();
	//重载
private:
	string name;
	enum 
	{
		GRADE,
		PASS,
		PERCENTAGE
	} mode;
	union 
	{
		char grade;
		bool pass;
		int percent;
	};
};


void ExamInfo::show()
{
	cout << name << ":";
	switch (mode)
	{
	case GRADE:cout << grade; break;
	case PASS:cout << (pass ? "PASS" : "FAIL"); break;
	case PERCENTAGE:cout << percent; break;
	}
	cout << endl << endl;
}



int main()
{
	ExamInfo course1("English", 'A');
	ExamInfo course2("Calculus", true);
	ExamInfo course3("C++ Programming", 100);
	course1.show();
	course2.show();
	course3.show();
	system("pause");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值