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

本文记录了楼主大一学习C/C++时的基础练习,包括水仙花数、乘法表、排列组合等。代码基于Visual Studio 2017,部分代码可能较为初级,主要用于个人学习历程的回顾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

实验平台为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>
using namespace std;
int main()
{
	int c,d,x,sum;
	for (int a = 100; a <= 999; a = a + 1)
	{
		x = a; sum = 0;
		for (int b = 1; b <= 3; b = b + 1) {
			c = x % 10;  x = x / 10;
			d = c * c * c; sum = sum + d;
		}
		if (sum == a)
			cout << sum << " ";
	}
	system("pause");
}
  1. 乘法表
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	for (int x=1; x <= n; x = x + 1)
	{
		cout << x;
		for (int y = 1; y <= x; y = y + 1)
		{
			cout << ' ' << x * y;
		}
		cout << endl;
	}
	system("pause");
}
  1. 四个数字的排列组合
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int sum=0;
	for (int baiwei = 1; baiwei <= 4; baiwei = baiwei + 1)
	{
		for (int shiwei = 1; shiwei <= 4; shiwei = shiwei + 1)
		{
			for (int gewei = 1; gewei <= 4; gewei = gewei + 1)
			{
				if (baiwei != shiwei && baiwei != gewei && shiwei != gewei)
					sum = sum + 1;
			}
		}
	}
	cout << sum;
	system("pause");
}
  1. 一个数组最小元素及其下标
#include "pch.h"
#include <iostream>
using namespace std;
int imin(int a[], int count)
{
	int min = a[0]; int x = 0;
	for (int i = 1; i < count; i = i + 1)
	{
		if (a[i] < min) { min = a[i];  }
	}
	return min;
}

int main()
{
	int count;
	int a[100];
	cin >> count;
	for (int i = 0; i < count; i = i + 1)
	{
		cin >> a[i];
	}
	int min= imin(a, count);
	cout << min << endl;
	system("pause");
}
  1. 求完数
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int a = 1,b[1000];
	for (a; a <= 1000; a = a + 1)
	{
		int d = 0,sum=0;
		for (int c = 2; c < a; c = c + 1)
		{
			if (a%c == 0)
			{
				b[d] = c;
				sum = sum + c;
				d = d + 1;
			}
		}
		if (a == sum+1&&a!=1)
		{
			cout << a << " = 1";
			for (int e=0;e<d; e = e + 1)
			{
				cout << " + " << b[e];
			}
			cout << endl;
		}
	}
	system("pause");
}
  1. 摘桃子 与 吃问题 反向推导
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int x = 1;
	for (int a = 1; a <= 9; a = a + 1)
	{
		x = (x + 1) * 2;
	}
	cout << x<<endl;
	system("pause");
}
  1. 求n!(函数调用法)
#include "pch.h"
#include <iostream>
using namespace std;
int fac(int n)
{
	int sum = 1;
	for (int x = 1; x <= n; x = x + 1)
	{
		sum = sum * x;
	}
	return sum;
}
int main()
{
	int a, b, c,sum;
	cin >> a >> b >> c;
	sum = fac(a) + fac(b) + fac(c);
	cout << sum << endl;
	system("pause");
}
  1. 递归法(整数转换成字符串)
#include "pch.h"
#include <iostream>  

// stdio.h就是指“standard input&output"
//意思就是说标准输入输出头文件!所以用到标准输入输出函数时,就要调用这个头文件!

//#include <stdio.h> 

//printf() 可以输出多种数据类型(%d %s %f...)  putchar() 一般只用来输出字符

/* 最后总结,C++的iostream库和C中的stdio库中
分别的cout/cin和printf/scanf相比有哪些优势呢?
首先是类型处理更加安全,更加智能,我们无须应对int、float中的%d、%f,而且扩展性极强,
对于新定义的类,printf想要输入输出一个自定义的类的成员是天方夜谭的,
而iostream中使用的位运算符都是可重载的,
并且可以将清空缓冲区的自由交给了用户(在printf中的输出是没有缓冲区的),
而且流风格的写法也更加自然和简洁。
*/

using namespace std;
void convert(int n)
{
	int zhengxing;
	char zifuchuan;
	if ((zhengxing = n / 10) != 0)
	{
		convert(zhengxing);
	}
	zifuchuan = n % 10 + 48;  //+'0' 整型转字符串  -'0' 字符串转整型   或者用ascii码  +48  或者 -48
	
	cout <<" "<< zifuchuan;
}

int main()
{
	int n;
	cin >> n;
	if (n < 0) { cout << '-'; n = -n; }
	convert(n);
	system("pause");
}
#include "pch.h"
#include <cstdio>
#include <iostream>
using namespace std;
void convert(int n)
{
	int zhengxing;
	if ((zhengxing = n / 10) != 0)
	{
		convert(zhengxing);
	}
	putchar(n % 10 + 48);
}
int main()
{
	int n;
	scanf_s("%d", &n);  //cin>>n
	if (n < 0) { printf ("-");  n = -n; }  //cout<<"-"
	convert(n);

	system("pause");
}
  1. 打印至少10行的杨辉三角
#include "pch.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	long long int a[100][100];     //太大要放在外面
	long long int hang,i,j;
	cin >> hang;

	for (i = 0; i < hang; i = i + 1)
	{
		a[i][0] = 1;
		a[i][i] = 1;
	}


	for (i = 2; i < hang; i = i + 1)
	{
		for (j = 1; j < i; j = j + 1)
		{
			a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
		}
	}


	for (i = 0; i < hang; i = i + 1)
	{
		for (j = 0; j <= i; j = j + 1)
		{
			cout << setw(8) << a[i][j];
		}
		cout << endl;
	}

	system("pause");
}
  1. c++ 红书作业-插入排序
#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
	int a[11] = { 10,20,30,40,50,60,70,80,90,100 };
	cout << "初始数据"<<endl;
	int b=0, x,t;
	for(;b<10;b=b+1)
	{
		cout << a[b] << " ";
	}
	cout << endl;
	cout << "请输入一个要插入的数字";
	cin >> x;
	a[10] = x;
	
	for (b; b > 0; b = b - 1)
	{
		if (a[b] < a[b - 1])
		{
			t = a[b];
			a[b] = a[b-1];
			a[b-1] = t;
		}
	}
	for (int n = 0; n <= 10; n = n + 1)
		cout << a[n] << " ";

	system("pause");
}
  1. 给出年月日 求该日是该年第几天
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int year, month, day,sum=0;
	int a[2][12] = { 31,29,31,30,31,30,31,31,30,31,30,31,
	31,28,31,30,31,30,31,31,30,31,30,31 };
	cin >> year >> month >> day;
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		for (; month > 1; month = month - 1)
		{
			sum = sum + a[0][month - 2];
		}
		sum = sum + day;
	}
	else
	{
		for (; month > 1; month = month - 1)
		{
			sum = sum + a[1][month - 2];
		}
		sum = sum + day;
	}
	cout << sum;
	system("pause");
}

36.打印沙漏

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
	int n;
	cout << "请输入一个数"<<endl;
	cin >> n;
	int sum = 1,kongyu,a=1;
	for (a; sum <= n; a = a + 1)
	{
		sum = sum + 2 * (2 * a + 1);
	}
	sum = sum - 2 * (2 * (a-1) + 1);
	kongyu = n - sum;     //输出多余数字
	cout << "多余数是" << kongyu << endl;

	int b,c=1,d;
	a = a - 2;
	for (a; a > 0; a = a - 1)
	{
		d = 2 * c - 1;
		for (d;d>0;d=d-1)
		{
			cout << " ";
		}
		c = c + 1;
		b = 2 * a + 1;
		for (b; b > 0; b = b - 1)
		{
			cout << "*"<<" ";
		}
		cout << endl;
	}

	d = 2 * c - 1;
	b = 2 * a + 1;
	for (d; d > 0; d = d - 1) { cout << " "; }
	cout << "*";
	cout << endl;

	for (c; c > 1; c = c - 1)
	{
		d = 2 * (c - 1) - 1;
		for (d; d > 0; d = d - 1)
		{
			cout << " ";
		}
		a = a + 1;
		b = 2 * a + 1;
		for (b; b > 0; b = b - 1)
		{
			cout << "*" << " ";
		}
		cout << endl;
	}

	system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值