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

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

实验平台为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 <Windows.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv)
{

	cout << "请输入一个奇数"<<endl;
	int a,b,c,d=0;
	cin >> a;
	if (a % 2 != 0)
	{

		for (b = 0; b < a/2+1 ; b = b + 1)
		{
			for (c = b; c < a/2; c = c + 1)
			{
				cout << "  ";
			}
			for (c = 0; c<b*2+1; c = c + 1)
			{
				cout << "▲";
			}
			cout << endl;
		}
	};
	d = a / 2 ;
	for (d; d > 0; d = d - 1)
	{
		cout << "  ";
	}
	cout << char(31)<< endl;
	d = a / 2;
	for (d; d > 0; d = d - 1)
	{
		cout << "  ";
	}
	cout << char(31);
	while (true) {
		system("color 1");
		Sleep(100);
		system("color 2");
		Sleep(100);
		system("color 3");
		Sleep(100);
		system("color 4");
		Sleep(100);
		system("color 5");
		Sleep(100);
		system("color 6");
		Sleep(100);
		system("color 7");
		Sleep(100);
	}
	return 0;
}
  1. 输出整数位数及各位数求和
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int a,b=0,c=0,sum=0;
	cin >> a;
	for (a; a > 0;)
	{
		b = a % 10;
		sum = sum + b;;
		c = c + 1;
		a = a / 10;
	}
	cout << c << " " << sum;
	system("pause");
}
  1. 数位数(求该数有几位)
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int a,b=0;
	cin >> a;
	if (a == 0) b = 1;
	for (a; a > 0;)
	{
		a = a / 10;
		b = b + 1;
	}
	cout << b;
	system("pause");
}
  1. 计算分段函数
#include "pch.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double x, result;
	cin >> x;
	if (x == 0) result = 0.0;
	else result = 1 / x;
	cout << "f(" << showpoint << fixed << setprecision(1) << x << ")"
		<< " = " << result;
	system("pause");
}
  1. !和sum(求和)
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int n,sum=0,a=1;
	cin >> n;
	for (a; a <= n; a = a + 1)
	{
		sum = sum + a;
	}
	cout << sum;
	system("pause");
}
  1. 计算阶乘和 12以内
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int n, sum = 0,b=1,a=1;
	cin >> n;
	for (a;a<=n;a=a+1)
	{
		b = b * a;
		sum = sum + b;
	}
	cout << sum;
	system("pause");
}
  1. 素数判断
#include "pch.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int N,a;//b被除计数
	cin >> N;
	for (N; N > 0; N = N - 1)
	{
		cin >> a;
		int x = 0,b=2;//用作判断结束与否依据
		if (a == 1) cout << "No"<<endl; 
		else {
			for (b; b <= sqrt(a); b = b + 1)
			{
				if (a%b == 0)
				{
					x = 1;
					break;
				}
			}
			if (x == 0)
				cout << "Yes" << endl;
			else cout << "No" << endl;
		}
	}
	system("pause");
}
  1. 求最大值及其下标
#include "pch.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int n, a[11], b = 0, c = 0;
	cin >> n;
	for (b; b < n; b = b + 1)
	{
		cin >> a[b];
	}
	b = 0;
	for (b; b < n; b = b + 1)
	{
		if (a[0] < a[b])
		{
			a[0] = a[b];
			c = b;
		}
	}
	cout << a[0] << " " << c;
	system("pause");
}
  1. N个数求和(分数)
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int fenzi[100], fenmu[100], n, x = 0;
	char a;
	cin >> n;
	for (x; x < n; x = x + 1)
	{
		cin >> fenzi[x] >> a >> fenmu[x];
	}
	int fenzisum = fenzi[0], fenmusum = fenmu[0];//初始化
	for (int y = 1; y < n; y = y + 1)
	{
		fenzisum = fenzisum * fenmu[y] + fenzi[y] * fenmusum;
		fenmusum = fenmusum * fenmu[y];
	}//分子分母通分
	if (fenzisum == 0) { cout << 0; system("pause"); return 0; }
	int A = fenzisum, B = fenmusum, X;
	while (B != 0)
	{
		X = A % B;
		A = B;
		B = X;
	}//求最大公约数
	fenzisum = fenzisum / A;
	fenmusum = fenmusum / A;//约简出来了
	if (fenzisum < 0) { fenzisum = (-1)*fenzisum; cout << "-"; }
	if (fenzisum < fenmusum) { cout << fenzisum << '/' << fenmusum; }
    if (fenzisum == fenmusum) { cout << 1; }//加else 表面平行关系
	if(fenzisum>fenmusum)  //或者最后别直接加else 详细说明 表示平行关系
	{
		cout << fenzisum / fenmusum;
		if (fenzisum%fenmusum != 0)//暂时未理解(如果分子不为分母倍数)
			cout <<" "<< fenzisum % fenmusum << '/' << fenmusum;
	}

	system("pause");
}
  1. 大数阶乘和及取大模(非最优方法)
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	long long int n,sum,mo;
	while (cin >> n)
	{
		sum = 1;  mo = 0;
		for (int a = 1; a <= n; a = a + 1)
		{
			sum = (sum * a) % 1000000007;
			mo = (mo + sum) % 1000000007;
		}
		cout << mo << endl;
	}
	system("pause");
}
  1. 最大公约数 最小公倍数
#include "pch.h"
#include <iostream>
using namespace std;
int gcd(int a, int b)//2个数的最大公约数
{
	if (a < b) { swap(a, b); }//始终令a>b
	if (b == 0) { return a; }
	else { return gcd(b, a%b); }
}
int lcm(int a, int b)//两个数的最小公倍数
{
	return a * b / gcd(a, b);
}
int main()
{
	int x, y;
	cin >> x >> y;
	cout << gcd(x, y) << " " << lcm(x, y);
	system("pause");
}
  1. 统计输入字符数并分类
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int n, zimu = 0, shuzi = 0, kongge = 0, qita = 0;
	while (n = getchar())//[while ((c=getchar())!='\n')] 读取一个字符回车时结束
	{
		if (n == '\n') break;
	    if (n >= 'a'&&n <= 'z' || n >= 'A'&&n <= 'Z') zimu = zimu + 1;
	    else if (n >= '0'&&n <= '9') shuzi = shuzi + 1;//加了else变互斥关系    
		else if (n ==' ') kongge = kongge + 1;
		else qita = qita + 1;
	}
	cout << zimu << ' ' << shuzi << ' ' << kongge << ' ' << qita << endl;
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值