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
總結
- 上一篇: RS(1)--10分钟了解什么是推荐系统
- 下一篇: 非类型模板参数(参考《C++ Templ