C++入门

DIARY 1

目录

一、个人信息
二、闰年判断
三、反转数字
四、公约(倍)数
五、素数
六、杨辉三角前n行
七、统计单词数
八、判断回文质数
九、栈的基本功能
十、两数交换

一、个人信息

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	string xh, xm, zy, xb;
	xh = "102201119";
	xm = "陈宇尧";
	zy = "计算机类";
	xb = "男";
	cout << "个人信息: " << endl;
	cout << "姓名: " << xm << endl;
	cout << "性别: " << xb << endl;
	cout << "学号: " << xh << endl;
	cout << "专业: " << zy << endl;







	system("pause");
	return 0;
}

运行结果:
个人信息:
姓名: 陈宇尧
性别: 男
学号: 102201119
专业: 计算机类

二、闰年判断

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	int year;
	cout << "请输入年份" << endl;
	cin >> year;
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		cout << "此年是闰年" << endl;

	}
	else
	{
		cout << "此年不是闰年" << endl;

	}




	system("pause");
	return 0;
}

运行结果:
eg:1531
请输入年份
1531
此年不是闰年

三、反转数字

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	long long num, i;
	long long j = 0;
	cout << "请输入要反转的数字: " << endl;
	cin >> num;
	for (num; num != 0; num /= 10)
	{
		i = num % 10;
		j = j * 10 + i;

	}
	cout << "反转后的数字是: " << j << endl;


	system("pause");
	return 0;
}

运行结果:
eg:32113
请输入要反转的数字:
32113
反转后的数字是: 31123

四、公约(倍)数

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(void)
{
	int i, j, k, m,n=0,p=0;
	cout << "请输入两个整数: " << endl;
	cin >> i;
	cin >> j;
	for ( k =min(i,j); k >=1; k--)
	{
		
	
		if (i % k == 0 && j % k == 0)
		{
			break;
		}
		
	}
	cout << "最大公因数为: " << endl;
	cout << k << endl;
	for ( m = max(i,j); m <= i * j; m++)
	{


		if (m %i == 0 && m %j == 0)
		{
			break;
		}

	}
	cout << "最小公倍数为: " << endl;
	cout << m << endl;






	system("pause");
	return 0;
}

运行结果:
eg:42 63
请输入两个整数:
42
63
最大公因数为:
21
最小公倍数为:
126

五、素数

#include<iostream>
#include<string>

using namespace std;
int main(void)
{
	
	int n = 1000;
	cout << "2 ";
	for (int i = 3; i < n; i += 2)
	{
		int j;
		for (j = 3; j < i; j += 2)
		{
			if (i % j == 0)
			{
				break;
			}
		}
		if (i == j)
		{
			cout << i << " " /*<< "\t"*/;
		}

	}
	




	system("pause");
	return 0;
}

运行结果:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

六、杨辉三角前n行

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int zuhe(int a, int b)
{
	int num1 = 1, num2 = 1;
	for (int i = a; i > a-b; i--)
	{
		num1 = num1 * i;
	}
	if (b == 0||a==0)
	{
		return 1;
	}
	else
	{
		for (int j = b; j > 0; j--)
		{
			num2 = num2 * j;
			
		}
		return num1 / num2;
	}
	
	
}

int main(void)
{
	int n = 0;
	cout << "请输入行数" << endl;
	cin >> n;
	cout << "杨辉三角前n行为: " << endl;
	int x = 1;
	for (int j = 1; j <= n; j++)
	{
		cout << setw(3 * (n - j) + 1) << x;
		for (int i = 1; i < j; i++)
		{
			x = zuhe(j - 1, i);
			cout << setw(6) << x;
		}
		cout << endl;
	}
	





	system("pause");
	return 0;
}

运行结果:
请输入行数
9
杨辉三角前n行为:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
ps:代码结果是居中的…

七、统计单词数

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
string change(string w)
{
	for (int i = 0; i < w.size(); i++)
	{
		if (w[i] >= 'a')
		{
			w[i] -= 32;
		}
	}
	return w;
}

int main(void)
{
	int num1 = 0,num2=0;
	string w1,w2;
	cout << "请输入要统计数量的单词" << endl;
	getline(cin, w1);
	cout << "请输入目标短文" << endl;
	getline(cin, w2);
	w1 = change(w1);
	w2 = change(w2);
	w1 = ' ' + w1 + ' ';
	w2 = ' ' + w2 + ' ';
	for (int i = 0; i < w2.size(); i++)
	{
		num1 = w2.find(w1, num1);
		
		if (num1 != string::npos)
		{
			num2++;
		}
		else
		{
			break;
		}
		num1++;
	}
	if (num2 == 0)
	{
		cout << "未找到该单词" << endl;
	}
	else
	{
		cout << "该单词出现的次数是: ";
		cout<<num2<<endl;
	}
	system("pause");
	return 0;
		
}

运行结果:
eg:
rises
The sun also Rises
请输入要统计数量的单词
rises
请输入目标短文
The sun also rises
该单词出现的次数是: 1

八、判断回文质数

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
	int a = 0;
	cout << "输入一个正整数" << endl;
	cin >> a;
	int i;
	for (i = 2; i <= a; i++)
	{
		if (a % i == 0)
		{
			break;
		}
	}
	if (a == i)
	{
		int x = 0, y = 0, z = 0;
		x = a;
		for (x; x != 0; x /= 10)
		{
			y = x % 10;
			z = z * 10 + y;
		}
		if (z == a)
		{
			cout << "该数是回文质数" << endl;
		}
		else
		{
			cout << "该数不是回文质数" << endl;
		}
	}
	else 
	{
		cout << "该数不是回文质数" << endl;
	}





	system("pause");
	return 0;
}





eg:
151
输入一个正整数
151
该数是回文质数

九、栈的基本功能

#include<iostream>
#include<string>
/*
	链式结构:一种不连续内存的数组
*/
using namespace std;
struct Node//一个结构体
{
	int data;//存放的数据
	struct Node* next;//指向下一个节点的指针
};
//创建结点
struct Node* createNode(int data)
{
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}
struct stack
{
	struct Node* stacktop;//栈顶标记
	int size;//栈中元素个数
};
//创建栈,就是创建一个struct stack的变量
struct stack* createstack()
{
	//创建过程就是初始化过程
	struct stack* mystack = (struct stack*)malloc(sizeof(struct stack));
	mystack->stacktop = NULL;
	mystack->size = 0;
	return mystack;
}
//出入栈
//入栈
void push(struct stack* mystack, int data)
{
	//插入的这个结点创建出来
	struct Node* newNode = createNode(data);
	//入栈操作就是链表表头插入
	newNode->next = mystack->stacktop;
	mystack->stacktop = newNode;
	mystack->size++;
}
//获取栈顶元素
int top(struct stack* mystack)
{
	//防御编程
	if (mystack->size == 0)
	{
		cout << "栈为NULL,无法获取栈顶元素" << endl;
		system("pause");
		return mystack->size;
	}
	else
	{
		return mystack->stacktop->data;
	}
}
//出栈
void pop(struct stack* mystack)
{
	if (mystack->size == 0)
	{
		cout << "栈为NULL,无法出栈" << endl;
		system("pause");
	}
	else
	{
		struct Node* nextNode = mystack->stacktop->next;
	    free(mystack->stacktop);
		mystack->stacktop = nextNode;
		mystack->size--;
	}
}
//万金油函数
int empty(struct stack* mystack)
{
	if (mystack->size == 0)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
int main()
{
	struct stack* mystack = createstack();
	push(mystack, 1);
	push(mystack, 2);
	push(mystack, 3);
	while (empty(mystack))
	{
		cout << "\t" << top(mystack);
		pop(mystack);
	}
	cout << endl;
	system("pause");
	return 0;
}

运行结果:
3 2 1

十、两数交换

#include<iostream>
#include<string>
//封装一个函数,利用冒泡排序,实现对整型数组的升序排序
using namespace std;
//冒泡排序函数
//指针
void bub(int* arr, int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
//引用
void swapinc(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}
void printarr(int* arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "arr" << "[" << i << "]= " << arr[i] << endl;
	}
}
int main(void)
{
	试验实参转换
	//int a = 4;
	//int b = 3;
	//bub(&a, &b);
	//cout << "a= " << a<< endl << "b= " << b << endl;
	//1、先创建数组
	int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };

	//数组长度
	int len = sizeof(arr) / sizeof(arr[0]);
	//2、创建函数,实现冒泡排序
	//bub(arr, len);//指针
	for (int i = 0; i < len - 1; i++)//引用
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				swapinc(arr[j], arr[j + 1]);
			}
		}
	}


	//3、打印排序后的数组
	printarr(arr, len);

	system("pause");
	return 0;
}

运行结果:
eg:
arr[0]= 1
arr[1]= 2
arr[2]= 3
arr[3]= 4
arr[4]= 5
arr[5]= 6
arr[6]= 7
arr[7]= 8
arr[8]= 9
arr[9]= 10

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值