100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > C语言经典编程实例(一)

C语言经典编程实例(一)

时间:2021-09-28 00:23:07

相关推荐

C语言经典编程实例(一)

文章目录

一、C语言基础 1.打印素数 2.打印乘法口诀表 3.判断闰年 4.两数交换 5.最大公约数GCD 6.数组内容交换 7.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 8.1~100 的所有整数中出现多少次数字9 9.打印菱形 10.水仙花数

使用C++语言解决C语言常见重点、要点问题

一、C语言基础
 1.打印素数

素数(质数):大于1的自然数中除了1和其本身之外不再有其他因数的数称其为质数,其余则称之为合数

//1.打印素数void PrintPrime(){int n1 = 0;int n2 = 0;cout << "Please input two numbers: ";cin >> n1 >> n2;int flag = 0;//①判断n是否是素数,看n能否被从2到n-1中的整数整除//当n很大时,效率低下for (int i = n1; i <= n2; i++){bool IsNoPrime = false;int data = i;for (int j = 2; j < data; j++){if ((data % j) == 0){IsNoPrime = true;break;}}if (!IsNoPrime){cout << data << ' ';if (flag++ == 10){cout << endl;flag = 0;}}}cout << endl << endl;//②有规律如下:当n能被2~n-1中的任意整数整除,则其因子中必定有一个小于等于√n,必定//有一个大于大于等于√n,那么便可缩小计算范围flag = 0;for (int i = n1; i <= n2; i++){bool IsNoPrime = false;int data = i;int k = (int)sqrt((double)data);//sqrt的参数类型是doublefor (int j = 2; j <= k; j++){if ((data % j) == 0){IsNoPrime = true;break;}}if (!IsNoPrime){cout << data << ' ';if (flag++ == 10){cout << endl;flag = 0;}}}cout << endl;}

 2.打印乘法口诀表

//2.打印乘法口诀表void PrintMultiplicationFormulatable(){//右上for (int i = 1; i <= 9; i++){for (int j = 1; j <= 9; j++){if (j < i)cout << " ";else{printf("%d*%d=%2d ", i, j, i * j);}}cout << endl;}cout << endl << endl;//左上for (int i = 1; i <= 9; i++){for (int j = i; j <= 9; j++){printf("%d*%d=%2d ", i, j, i * j);}cout << endl;}cout << endl << endl;//右下for (int i = 1; i <= 9; i++){for (int j = 1; j <= 9 - i; j++){cout << " ";}for (int j = 1; j <= i; j++){printf("%d*%d=%2d ", i, j, i * j);}cout << endl;}cout << endl << endl;//左下for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){printf("%d*%d=%2d ", i, j, i * j);}cout << endl;}cout << endl << endl;}

 3.判断闰年

判断任意年份是否为闰年,需要满足以下条件中的任意一个:

 ① 该年份能被 4 整除同时不能被 100 整除;

 ② 该年份能被400整除

//3.判断闰年void IsLeapYear(){int year = 0;cout << "Please input year:";cin >> year;assert(year > 0);if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){cout << "Is Leap Year!" << endl;}else{cout << "Is Not Leap Year!" << endl;}}

 4.两数交换

//4.两数交换void Swap(){int n1 = 0;int n2 = 0;cout << "Please input two numbers: ";cin >> n1 >> n2;cout << "n1: " << n1 << " " << "n2: " << n2 << endl;//中间变量法int tmp = n1;n1 = n2;n2 = tmp;cout << "n1: " << n1 << " " << "n2: " << n2 << endl;//加减法--当数字过大时存在局限性n1 += n2;n2 = n1 - n2;n1 = n1 - n2;cout << "n1: " << n1 << " " << "n2: " << n2 << endl;//异或法n1 = n1 ^ n2;n2 = n2 ^ n1;n1 = n1 ^ n2;cout << "n1: " << n1 << " " << "n2: " << n2 << endl;//位运算法--当数字过大时存在局限性(2字节存储范围:-32767-32767)n1 <<= 16;n1 += n2;n2 = n1 >> 16;n1 = n1 & 0xffff;cout << "n1: " << n1 << " " << "n2: " << n2 << endl;}

 5.最大公约数GCD

如果有一个自然数a能被自然数b整除,则称a为b的倍数,b为a的约数。几个自然数公有的约数,叫做这几个自然数的公约数。公约数中最大的一个公约数,称为这几个自然数的最大公约数

//5.最大公约数GCDvoid FindGCD(){unsigned int n1 = 0;unsigned int n2 = 0;cout << "Please input two numbers: ";cin >> n1 >> n2;unsigned int max = n1;unsigned int min = n2;if (n1 < n2){max = n2;min = n1;}//穷举法--效率低下vector<int> num;for (int i = 1;i <= min; i++){if ((n1 % i == 0) && (n2 % i == 0)){num.push_back(i);}}cout << "The GCD Is: " << num[num.size() - 1] << endl;//按照从大(两个整数中较小的数)到小(到最小的整数1)的顺序求出第一个能同时整除//两个整数的自然数for (int i = min; i > 0; i--){if ((n1 % i == 0) && (n2 % i == 0)){cout << "The GCD Is: " << i << endl;break;}}}

 6.数组内容交换

//6.数组内容交换//根据上一题既可以进行处理//交换函数void SwapNum(int* pa, int* pb){*pa ^= *pb;*pb ^= *pa;*pa ^= *pb;}void SwapElements(){vector<int> arr1, arr2;cout << "Please Input Arr1 Elements:";int num = 0;while (cin >> num){arr1.push_back(num);if (arr1.size() > N - 1)break;cout << "Please Input Arr1 Elements:";}cout << endl << endl;cout << "Please Input Arr2 Elements:";while (cin >> num){arr2.push_back(num);if (arr2.size() > N - 1)break;cout << "Please Input Arr2 Elements:";}cout << endl << endl;cout << "The Arr1 Is: ";for (int i = 0; i < arr1.size(); i++){cout << arr1[i] << ' ';}cout << endl << endl;cout << "The Arr2 Is:";for (int i = 0; i < arr2.size(); i++){cout << arr2[i] << ' ';}cout << endl << endl;for (int i = 0; i < arr1.size(); i++){SwapNum(&arr1[i], &arr2[i]);}cout << endl << endl;cout << "The Arr1 Is: ";for (int i = 0; i < arr1.size(); i++){cout << arr1[i] << ' ';}cout << endl << endl;cout << "The Arr2 Is: ";for (int i = 0; i < arr2.size(); i++){cout << arr2[i] << ' ';}cout << endl << endl;}

 7.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

//7.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值void SumofFraction(){double sum = 0.0;for (int i = 1; i <= 100; i++){sum += pow(-1, i + 1) / i;}cout << "The Sum of '1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100' Is: " << sum << endl;}

 8.1~100 的所有整数中出现多少次数字9

将每个整数中的数字取出来进行判断

//8.1~100 的所有整数中出现多少次数字9void NineNumber(){int cou = 0;int i = 0;for (i = 1; i <= 100; i++){int tmp = 1;int nu = i;while (nu > 0){tmp = nu % 10;if ((tmp / 9) == 1)cou++;nu = nu / 10;}}cout << "The number of 9 is: " << cou << endl;}

 9.打印菱形

//9.打印菱形void PrintRhombic(){//①分为上下两部分int line = 0;//行数 == 列数cout << "Please input a Odd: ";cin >> line;int spacenumber = line / 2;//空格数int starnumber = 1;//星号数for (int i = 0; i < line; i++){//上半部分if (i <= line / 2){for (int j = 0; j < spacenumber; j++){cout << ' ';}for (int j = 0; j < starnumber; j++){cout << '*';}cout << endl;if (i == line / 2){continue;}spacenumber--;starnumber += 2;}else//下半部分{spacenumber++;starnumber -= 2;for (int j = 0; j < spacenumber; j++){cout << ' ';}for (int j = 0; j < starnumber; j++){cout << '*';}cout << endl;}}//②按照行列来考虑,判断某一行某一列的元素line==colnmn//对于上半部分(包括中间一行),当前行与当前列满足如下关系输出星号i,j从1开始//j>=(column+1)/2-(i + 1)(column+1)/2-(i + 1)为第i行最左边的星号//j<=(column+1)/2+(i + 1) (column+1)/2+(i + 1)为第i行最右边的星号//对于下半部分,当前行与当前列满足如下关系输出星号//j>=(column+1)/2-(line-i)(column+1)/2-(line-i)为第i行最左边的星号//j<=(column+1)/2+(line-i) (column+1)/2+(line-i)为第i行最右边的星号int colnmn = line;//列==行for (int i = 1; i <= line; i++){//上半部分if (i <= (line / 2 + 1)){for (int j = 1; j <= colnmn; j++){if (((colnmn + 1) / 2) - (i - 1) <= j && j <= ((colnmn + 1) / 2) + (i - 1)){cout << '*';}else{cout << ' ';}}cout << endl;}else//下半部分{for (int j = 1; j <= colnmn; j++){if (((colnmn + 1) / 2) - (line - i) <= j && j <= ((colnmn + 1) / 2) + (line - i)){cout << '*';}else{cout << ' ';}}cout << endl;}}}

 10.水仙花数

“水仙花数”是指一个三位数其各位数字的立方和等于该数本身,例如153是“水仙花数”,因为:153 = 13 + 53 + 33,所以判断一个数是否为“水仙花数”,最重要的是要把给出的三位数的个位、十位、百位分别拆分,并求其立方和(设为s),若s与给出的三位数相等, 三位数为“水仙花数”,反之,则不是。

//10.求出100~999之间的所有“水仙花数”并输出void FindNarcissisticnumber(){int bit = 0;int top = 0;int hundred = 0;cout << "Result Is: ";for (int i = 153; i < 1000; i++){int sum = 0;int n = i;bit = n % 10;n = n / 10;top = n % 10;hundred = n / 10;sum = pow(bit, 3) + pow(top, 3) + pow(hundred, 3);if (sum == i){cout << i << ' ';}}cout << endl;}

 main函数及其头文件

#include <iostream>#include <assert.h>#include <vector>#define N 5using namespace::std;int main(){1.打印素数//PrintPrime();2.打印乘法口诀表//PrintMultiplicationFormulatable();3.判断闰年//IsLeapYear();4.两数交换//Swap();5.最大公约数GCD//FindGCD();6.数组内容交换//SwapElements();7.分数求和//SumofFraction();8.数九问题//NineNumber();9.打印菱形//PrintRhombic();10.水仙花数FindNarcissisticnumber();system("pause");return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。