立志10天学会C++基础应用—day02 代码清晰易懂 涉及数据结构算法的知识 写完了~我也麻了
??哈嘍,很高興又見面啦,一起加油一起學習,歡迎您的關注!https://blog.csdn.net/hanhanwanghaha學習路上有您的關注是我的榮幸,一起進步是我的動力,明天也一起加油啊!
以往鏈接
立志10天學會C++基礎應用—day01
文章目錄
- 以往鏈接
- 一、循環結構
- A01while循環.cpp
- A02while猜數字.cpp
- A03dowhile循環.cpp
- A04dowhile水仙花數.cpp
- A05for循環敲桌子案例.cpp
- 二、嵌套循環
- A06嵌套循環星星圖.cpp
- A06嵌套循環乘法口訣表.cpp
- 三、跳轉語句
- A07跳轉語句break.cpp
- A08跳轉語句continue.cpp
- A09跳轉語句goto.cpp
- 四、數組
- A10數組的定義.cpp
- A11八只狗狗比體重.cpp
- A12元素的逆置.cpp
- A13冒泡排序.cpp
- A14二維數組的定義方式.cpp
- A14二維數組的數組名稱.cpp
- A16二維數組的應用三位同學的總成績.cpp
- 五、函數
- A17函數的調用.cpp
- A17函數的值傳遞.cpp
- A19函數的常見樣式.cpp
- A20函數的分文件編寫.cpp
- 六、指針
- A21指針的定義和使用.cpp
- A22指針內存大小空指針野指針.cpp
- A23const修飾指針.cpp
一、循環結構
A01while循環.cpp
#include <iostream> using namespace std;int main() {int num = 0;num++;//()中填入循環條件//語法:while(循環條件){循環語句}while (num < 10) {num *= 2;cout << num << endl;}system("pause");return 0;}A02while猜數字.cpp
#include <iostream> using namespace std; #include<ctime>int main() {//添加隨機數種子,作用利用當前系統時間生成隨機數,防止每次隨機數都一樣‘srand((unsigned int)time(NULL));//1、系統生成0到99的隨機數int num = rand() % 100;//cout << num << endl;//2、玩家進行猜測cout << "請輸入你猜的數" << endl;int ans = 0;//3、判斷玩家的猜測//while死循環,等待中斷信號while (1) {cin >> ans;if (ans<num) {cout << "猜測過小,請你再猜一遍" << endl;}else if(ans>num) {cout << "猜測過大,請你再猜一遍" << endl;}else {cout << "恭喜您猜對了" << endl;break;//中斷信號}}//猜對 退出游戲//猜錯 提示猜的結果 過大或者過小 重新返回第二步system("pause");return 0; }A03dowhile循環.cpp
用這個寫了一個死循環,無聊到爆~今天的白日夢樂趣
A04dowhile水仙花數.cpp
#include <iostream> using namespace std;int main() {//查詢100到999中的所有水仙花數int num = 100;do {int a = 0;//個位int b = 0;//十位int c = 0;//百位a = num % 10;//提取個位的數b = num / 10 % 10;//提取十位的數c = num / 100 % 10;//提取百位的數if (a * a * a + b * b * b + c * c * c == num) {//如果是水仙花數,就打印cout << num << endl;}num++;} while (num < 1000);system("pause");return 0; }A05for循環敲桌子案例.cpp
案例描述:從1開始數到數字100,如果數字個位含有7,或者數字十位含有7,或者該數字是7的倍數,我們打印敲桌子,其余數字直接打印輸出。
#include <iostream> using namespace std;int main() {//語法:for(起始表達式:條件表達式:末尾表達式){循環語句}for (int i = 0;i <= 100;i++) {//數字個位含有7,或者數字十位含有7,或者該數字是7的倍數if (i / 7 == 0 || i / 10 ==7 || i % 7 == 0) {cout << "敲桌子" << endl;}else{cout << i << endl;}}system("pause");return 0;}二、嵌套循環
A06嵌套循環星星圖.cpp
打印一個星星圖
#include <iostream> using namespace std;int main() {//打印一個星星圖for (int i = 0;i < 10;i++) {//內層循環for (int j = 0;j < 10;j++) {cout << "*";}cout << endl;}system("pause");return 0; }A06嵌套循環乘法口訣表.cpp
#include <iostream> using namespace std;int main() {//乘法口訣表//打印行數for (int i = 1;i < 10;i++) {for (int j = 1;j <= i;j++) {cout << j << "*" << i << "=" << j * i <<" ";}cout << endl;}system("pause");return 0; }歡迎關注:https://blog.csdn.net/hanhanwanghaha
三、跳轉語句
A07跳轉語句break.cpp
#include <iostream> using namespace std;int main() {//break的幾種用法//1、出現在switch中cout << "請選擇您的成績等級" << endl;cout << "1、A" << endl;cout << "2、B" << endl;cout << "3、C" << endl;//創建結果的變量int grade = 0;//用戶輸入等級cin >> grade;switch (grade){case 1:cout << "您可以讀市重點" << endl;break;case 2:cout << "您可以讀區重點" << endl;break;case 3:cout << "您可以讀鄉鎮學校" << endl;break;default:break;}//2、出現在for循環中for (int i = 0; i < 10; i++){if (i == 7) {break;}cout << i << endl;}//3、出現在嵌套循環語句中for (int i = 0;i < 10;i++) {//內層循環for (int j = 0;j < 10;j++) {if (j == 5) {break;}cout << "*";}cout << endl;}system("pause");return 0;}A08跳轉語句continue.cpp
執行到continue那一行,就不會執行后面的了,然后循環前面的
#include <iostream> using namespace std;int main() {//continue語句for (int i = 0;i <= 100;i++) {//結果是奇數輸出,偶數不輸出if (i % 2 == 0) {continue;}cout << i << endl;}system("pause");return 0;}A09跳轉語句goto.cpp
#include <iostream> using namespace std;int main() {//語法:goto 標記//如果標記的名稱存在,執行到goto的語句時,會跳轉到標記的位置cout << "我是寶藏" << endl;cout << "女孩" << endl;goto baby;cout << "????????????" << endl;baby:cout << "的成長日記" << endl;cout << "小伙伴你好啊" << endl;system("pause");return 0;}四、數組
A10數組的定義.cpp
#include <iostream> using namespace std;int main() {//數組/*數組的三種定義類型1、數據類型 數組名[數組長度];2、數據類型 數組名[數組長度]–{ 值1,值2 ...};3、數據類型 數組名[ ]= { 值1,值2 ...};*///3、時間關系就演示第三個(常用)int arr[] = { 1,2,5,34,344,221,666,777,9998 };for (int i = 0;i < 2; i++ ) {cout << arr[i] << endl;}/*數組名稱的作用:1、可以通過數組名統計整個數組占用內存大小2、可以通過數組名查看數組首地址*///1、可以通過數組名統計整個數組占用內存大小cout << "整個數組所占空間為" << sizeof(arr) << endl;cout << "第一個數組所占空間為" << sizeof(arr[0]) << endl;cout << "數組的元素個數為" << sizeof(arr)/ sizeof(arr[0]) << endl;//2、可以通過數組名查看數組首地址cout << "數組首地址為" << (int)arr << endl;cout << "數組第一個地址為" << (int)&arr[0] << endl;cout << "數組第二個地址為" << (int)&arr[0] << endl;//注意:數組名是常量不可以賦值system("pause");return 0;}A11八只狗狗比體重.cpp
#include <iostream> using namespace std;int main() {//八個小狗狗比體重//1、創建8個狗狗的體重的數組int arr[] = { 200,99,98,110,20,50,55,77 };//2、從數組中找到最大值int max = 0;//先認定一個最大值為0for (int i = 0;i < 8;i++) {if (arr[i]>max) {max = arr[i];}}cout << "狗狗的體重最大值為" << max << endl;system("pause");return 0;}A12元素的逆置.cpp
此時此刻我看到這個代碼的心情是復雜的,因為我以前好像考到過這個代碼,仿佛遇見了前世的愛人那般熟悉。不過有個更簡單方法(我記得是利用離散數學中的求逆,我之前寫過這個代碼,懶得找了)
#include <iostream> using namespace std;int main() {//1、實現12345的逆置//1、創建數組int arr[] = { 1,2,3,4,5 };cout << "數組逆置前為" << endl;for (int i = 0;i < 5;i++) {cout << arr[i] << endl;}//2、實現逆置int start = 0;//初始位置為0//最后一個位置可以用整個數組的長度除以第一個元素位置的長度-1int end = sizeof(arr) / sizeof(arr[0]) - 1;//末尾位置while (start < end) {//實現元素互換int temp = arr[start];arr[start] = arr[end];arr[end] = temp;//下標更新start++;end--;}//3、打印逆置后的數組cout << "數組逆置后為" << endl;for (int i = 0;i < 5;i++) {cout << arr[i] << endl;}system("pause");return 0;}https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關注!
歡迎關注微信公眾號:寶藏女孩的成長日記
讓這個可愛的寶藏女孩在努力的道路上與你一起同行!
如有轉載,請注明出處(如不注明,盜者必究)
A13冒泡排序.cpp
冒泡排序python版本
如果不懂話建議看一下王道數據結構的講解,這個比較詳細,這涉及到數據結構的內容。
這里安利一個之前我看到的各種排序算法動畫演示過程
http://tools.jb51.net/static/api/paixu_ys/index.html
冒泡排序的具體過程
1.比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。
2.對每一對相鄰元素做同樣的工作,執行完畢后,找到第一個最大值。
3.重復以上的步驟,每次比較次數-1,直到不需要比較
A14二維數組的定義方式.cpp
1. 數據類型數組名[行數][列數];
2.數據類型數組名[行數][列數]= { {數據1,數據2},{數據3,數據4 } };
3.數據類型數組名[行數][列數]={數據1,數據2,數據3,數據4};
4.數據類型數組名[][列數]={數據1,數據2,數據3,數據4};
推薦用第二種
A14二維數組的數組名稱.cpp
#include <iostream> using namespace std;int main() {//二維數組名稱用途//1、可以查看占用內存空間大小double arr[2][3]{{1,2,3},{4,5,6}};cout << "該二維數組的內存空間大小為" <<sizeof(arr) << endl;cout << "該二維數組的第一行占用空間大小為" << sizeof(arr[0]) << endl;cout << "該二維數組的第一個元素占用空間大小為" << sizeof(arr[0][0]) << endl;cout << "二維數組的一共有多少個元素" << sizeof(arr) / sizeof(arr[0][0]) << endl;cout << "二維數組的行數為" << sizeof(arr) / sizeof(arr[0]) << endl;cout << "二維數組的列數為" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;//2、可以查看二維數組的首地址cout << "二維數組的首地址為" << (int)arr << endl;cout << "二維數組的第一行的首地址為" << (int)arr[0] << endl;cout << "二維數組的第二行的首地址為" << (int)arr[1] << endl;cout << "二維數組的第一個元素的首地址為" << (int)&arr[0][0] << endl;system("pause");return 0;}A16二維數組的應用三位同學的總成績.cpp
#include <iostream> using namespace std; #include <string>int main() {//二維數組的案例——三位同學的的三門課的總成績統計//1、創建二維數組int scores[3][3]{{100,99,99},{100,100,100},{16,89,100}};string names[3]{ "周杰倫","昆凌","蔡依林" };for (int i = 0;i < 3;i++) {int sum = 0;//統計分數總和變量for (int j = 0;j < 3;j++) {sum += scores[i][j];}cout << names[i] << "的總成績為" << sum << endl;}system("pause");return 0;}五、函數
函數說白了就是封裝一個功能
函數的定義
A17函數的調用.cpp
#include <iostream> using namespace std; #include <string>//隨便定義一個加法函數,num1和num2為形參 int add(int num1, int num2) {int sum = num1 + num2;return sum; };int main() {//在主函數中調用int a = 66;int b = 88;//函數調用方法:函數名稱(參數)//ab為實參//當調用函數時,實參賦給形參int c = add(a, b);cout << "最終結果為:" << c << endl;system("pause");return 0;}A17函數的值傳遞.cpp
#include <iostream> using namespace std; #include <string>//值傳遞//定義函數,實現兩個數字進行交換函數//如果函數不需要返回值,聲明的時候可以寫void void swap(int num1, int num2) {cout << "交換前" << endl;cout << "num1為" << num1 << endl;cout << "num2為" << num2 << endl;int temp = num1;num1 = num2;num2 = temp;cout << "交換后" << endl;cout << "num1為" << num1 << endl;cout << "num2為" << num2 << endl;}int main() {int a = 99;int b = 89;cout << "a:" << a << endl;cout << "b:" << a << endl;//當我們做值傳遞的時候,函數的形參發生改變,并不會影響實參swap(a, b);cout << "a:" << a << endl;cout << "b:" << b << endl;system("pause");return 0;}A19函數的常見樣式.cpp
常見的函數樣式有4種
1.無參無返
2.有參無返
3.無參有返
4.有參有返
A20函數的分文件編寫.cpp
作用:讓代碼結構更加清晰簡潔
函數分文件編寫─般有4個步驟
這種造型
A20函數的分文件編寫.cpp
#include <iostream> using namespace std; #include "swap.h"//函數的分文件編寫//實現兩個數字進行交換的函數 函數的聲明 //void swap(int num1, int num2); 函數的定義 //void swap(int num1, int num2) { // int temp = num1; // num1 = num2; // num2 = temp; // // // cout << "num1=" << num1 << endl; // cout << "num2=" << num2 << endl; //}int main() {int a = 99;int b = 88;swap(a,b);system("pause");return 0;}swap.h
#include <iostream> using namespace std;//函數的聲明 void swap(int num1, int num2);swap.cpp
#include "swap.h"//函數的定義 void swap(int num1, int num2) {int temp = num1;num1 = num2;num2 = temp;cout << "num1=" << num1 << endl;cout << "num2=" << num2 << endl; }六、指針
A21指針的定義和使用.cpp
#include <iostream> using namespace std;int main() {//1、定義指針int a = 10;//指針定義的語法:數據類型*指針變量名;int* p;//讓指針記錄變量a的地址p = &a;cout << "a的地址為" << &a << endl;cout << "指針p為" << p << endl;//2、使用指針//可以通過 解引用 的方式來找到指針指向的內存//指針前加*代表解引用,找到指針指向的內存中的數據*p = 888;cout << "a為" << a << endl;cout << "*p為" << *p << endl;system("pause");return 0;}A22指針內存大小空指針野指針.cpp
#include <iostream> using namespace std;int main() {//不管是什么數據類型,要求熟記://在32位操作系統下,指針是占4個字節空間大小,//在64位操作系統下,指針是占8個字節空間大小cout << "sizeof(int *=)" << sizeof(int *) << endl;cout << "sizeof(float *=)" << sizeof(float *) << endl;cout << "sizeof(double *=)" << sizeof(double *) << endl;cout << "sizeof(char *=)" << sizeof(char *) << endl;//空指針//指針變量p指向內存地址編號為0的空間int* p = NULL;//訪問空指針報錯//內存編號0 ~255為系統占用內存,不允許用戶訪問//cout<<*p<<endl;//野指針:指針變量指向非法的內存空間int* p = (int *)0x1100;//訪問野指針運行不出來cout << *p << endl;system("pause");return 0;}A23const修飾指針.cpp
const修飾指針有三種情況:
我自己是這么記的,誰在后面誰就可以變
#include <iostream> using namespace std;int main() {// 1、const修飾指針常量指針int a = 8;int b = 6;const int* p = &a;//指針指向的值不可以改,指針的指向可以改//* p = 20;錯誤p = &b;//正確// 2、const修飾常量指針常量//指針的指向不可以改,指針指向的值可以改int* const p2 = &a;*p2 = 88;//p2 = &b;錯誤//3、const修飾指針和常量const int* const p3 = &a;//指針的指向和指針指向的值都不可以改//*p3 =100;//錯誤//p3 = &b;//錯誤system("pause");return 0;}https://blog.csdn.net/hanhanwanghaha 寶藏女孩的成長日記 歡迎您的關注!
歡迎關注微信公眾號:寶藏女孩的成長日記
讓這個可愛的寶藏女孩在努力的道路上與你一起同行!
如有轉載,請注明出處(如不注明,盜者必究)
終于寫完了,完成任務,看武林外傳去!
總結
以上是生活随笔為你收集整理的立志10天学会C++基础应用—day02 代码清晰易懂 涉及数据结构算法的知识 写完了~我也麻了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RT-Thread中如何预编译一个.c文
- 下一篇: (python3.8)pygraphvi