C++入门
DIARY 1
目錄
一、個(gè)人信息
二、閏年判斷
三、反轉(zhuǎn)數(shù)字
四、公約(倍)數(shù)
五、素?cái)?shù)
六、楊輝三角前n行
七、統(tǒng)計(jì)單詞數(shù)
八、判斷回文質(zhì)數(shù)
九、棧的基本功能
十、兩數(shù)交換
一、個(gè)人信息
#include<iostream> #include<string> using namespace std; int main(void) {string xh, xm, zy, xb;xh = "102201119";xm = "陳宇堯";zy = "計(jì)算機(jī)類";xb = "男";cout << "個(gè)人信息: " << endl;cout << "姓名: " << xm << endl;cout << "性別: " << xb << endl;cout << "學(xué)號: " << xh << endl;cout << "專業(yè): " << zy << endl;system("pause");return 0; }運(yùn)行結(jié)果:
個(gè)人信息:
姓名: 陳宇堯
性別: 男
學(xué)號: 102201119
專業(yè): 計(jì)算機(jī)類
二、閏年判斷
#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; }運(yùn)行結(jié)果:
eg:1531
請輸入年份
1531
此年不是閏年
三、反轉(zhuǎn)數(shù)字
#include<iostream> #include<string> using namespace std; int main(void) {long long num, i;long long j = 0;cout << "請輸入要反轉(zhuǎn)的數(shù)字: " << endl;cin >> num;for (num; num != 0; num /= 10){i = num % 10;j = j * 10 + i;}cout << "反轉(zhuǎn)后的數(shù)字是: " << j << endl;system("pause");return 0; }運(yùn)行結(jié)果:
eg:32113
請輸入要反轉(zhuǎn)的數(shù)字:
32113
反轉(zhuǎn)后的數(shù)字是: 31123
四、公約(倍)數(shù)
#include<iostream> #include<string> #include<algorithm> using namespace std; int main(void) {int i, j, k, m,n=0,p=0;cout << "請輸入兩個(gè)整數(shù): " << endl;cin >> i;cin >> j;for ( k =min(i,j); k >=1; k--){if (i % k == 0 && j % k == 0){break;}}cout << "最大公因數(shù)為: " << endl;cout << k << endl;for ( m = max(i,j); m <= i * j; m++){if (m %i == 0 && m %j == 0){break;}}cout << "最小公倍數(shù)為: " << endl;cout << m << endl;system("pause");return 0; }運(yùn)行結(jié)果:
eg:42 63
請輸入兩個(gè)整數(shù):
42
63
最大公因數(shù)為:
21
最小公倍數(shù)為:
126
五、素?cái)?shù)
#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; }運(yùn)行結(jié)果:
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 << "請輸入行數(shù)" << 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; }運(yùn)行結(jié)果:
請輸入行數(shù)
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:代碼結(jié)果是居中的…
七、統(tǒng)計(jì)單詞數(shù)
#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 << "請輸入要統(tǒng)計(jì)數(shù)量的單詞" << endl;getline(cin, w1);cout << "請輸入目標(biāo)短文" << 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 << "該單詞出現(xiàn)的次數(shù)是: ";cout<<num2<<endl;}system("pause");return 0;}運(yùn)行結(jié)果:
eg:
rises
The sun also Rises
請輸入要統(tǒng)計(jì)數(shù)量的單詞
rises
請輸入目標(biāo)短文
The sun also rises
該單詞出現(xiàn)的次數(shù)是: 1
八、判斷回文質(zhì)數(shù)
#include<iostream> #include<string> using namespace std; int main(void) {int a = 0;cout << "輸入一個(gè)正整數(shù)" << 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 << "該數(shù)是回文質(zhì)數(shù)" << endl;}else{cout << "該數(shù)不是回文質(zhì)數(shù)" << endl;}}else {cout << "該數(shù)不是回文質(zhì)數(shù)" << endl;}system("pause");return 0; }eg:
151
輸入一個(gè)正整數(shù)
151
該數(shù)是回文質(zhì)數(shù)
九、棧的基本功能
#include<iostream> #include<string> /*鏈?zhǔn)浇Y(jié)構(gòu):一種不連續(xù)內(nèi)存的數(shù)組 */ using namespace std; struct Node//一個(gè)結(jié)構(gòu)體 {int data;//存放的數(shù)據(jù)struct Node* next;//指向下一個(gè)節(jié)點(diǎn)的指針 }; //創(chuàng)建結(jié)點(diǎn) 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;//棧頂標(biāo)記int size;//棧中元素個(gè)數(shù) }; //創(chuàng)建棧,就是創(chuàng)建一個(gè)struct stack的變量 struct stack* createstack() {//創(chuàng)建過程就是初始化過程struct stack* mystack = (struct stack*)malloc(sizeof(struct stack));mystack->stacktop = NULL;mystack->size = 0;return mystack; } //出入棧 //入棧 void push(struct stack* mystack, int data) {//插入的這個(gè)結(jié)點(diǎn)創(chuàng)建出來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--;} } //萬金油函數(shù) 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; }運(yùn)行結(jié)果:
3 2 1
十、兩數(shù)交換
#include<iostream> #include<string> //封裝一個(gè)函數(shù),利用冒泡排序,實(shí)現(xiàn)對整型數(shù)組的升序排序 using namespace std; //冒泡排序函數(shù) //指針 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) {試驗(yàn)實(shí)參轉(zhuǎn)換//int a = 4;//int b = 3;//bub(&a, &b);//cout << "a= " << a<< endl << "b= " << b << endl;//1、先創(chuàng)建數(shù)組int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };//數(shù)組長度int len = sizeof(arr) / sizeof(arr[0]);//2、創(chuàng)建函數(shù),實(shí)現(xiàn)冒泡排序//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、打印排序后的數(shù)組printarr(arr, len);system("pause");return 0; }運(yùn)行結(jié)果:
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
總結(jié)
- 上一篇: RS(1)--10分钟了解什么是推荐系统
- 下一篇: s3c2440移植MQTT