/ *
* @作成者:LeeG
* @ date:2020-12-10
* /
1.C言語とC ++の違い
©プロセス指向言語:プロセス指向プログラミングとは、問題を解決するためのステップを分析し、これらのステップを段階的に実装し、使用するときに1つずつ呼び出すことです。
(C ++)オブジェクト指向言語:オブジェクト指向プログラミングは、問題をさまざまなオブジェクトに分解することです。オブジェクトを作成する目的は、ステップを完了することではなく、問題解決ステップ全体での何かの動作を記述することです。
2. C ++ヘッダーファイルと一般的なヘッダーファイルの概要
#include <iostream>
using namespace std;
int main(){
//please write your code!
return 0;
}
#include <iostream>
#include <cmath> //数学公式 例如绝对值abs()、幂函数pow()...
#include <string> //C++字符串
#include <string.h> //C语言库,对应C++是cstring
#include <algorithm> //常用函数,max()、min()、abs()、swap()、sort()...
#include <bits/stdc++.h> //万能头文件
using namespace std;
int main(){
//please write your code!
return 0;
}
その他のヘッダーファイルとC ++標準テンプレートライブラリSTLについては、以下のリンクを参照してください。
3. C ++の入力と出力
#include <iostream>
using namespace std;
int main(){
int a;
char b;
float c;
cin>>a>>b>>c;
cout<<"1:"<<a<<b<<c<<endl;
string str;
cin>>str;
cout<<"2:"<<str<<endl;
getline(cin, str);
cout<<"3:"<<str<<endl;
//思考1 上述的cin>>str和getline(cin, str); 有什么区别?
//思考2 如果要输入 19:05 怎么输入?
//思考3 如果题目要求输入一串数字,以回车结束输入,怎么输入?
return 0;
}
思考3は非常に重要であり、質問を書くときによく遭遇します。!!
3の要約を考えて、記事を参照してください:
4.コードフォーマットの仕様
なぜこれが何度も強調されるのですか?コードを記述してバグがある場合、クリーンで整理されたコードを使用すると、コードをすばやく見つけることができます。また、心理的にもはるかに快適になります。それだけでなく、将来あなたがあなたのコードをレビューするとき、または他の人があなたのコードを読むとき、読みやすさを改善してください〜
#include <bits/stdc++.h>
#include <vector>
using namespace std;
struct Graph{
int edge;
int weight;
Graph(int e, int w){
edge = e;
weight = w;
}
};
int main(){
int n, m, sx, sy;
cin>>n>>m>>sx>>sy;
int cs[n]; //记录救援队数目
int judge[n]; //记录该城市是否达到过
int dotlist[n]; //记录权值
int pre[n]; //记录行走路径
int path[n]; //记录最短路径条数
memset(path, 0, sizeof(path));
memset(pre, 0, sizeof(pre));
memset(dotlist, 0, sizeof(dotlist));
memset(judge, 0, sizeof(judge));
int res[n];
fill(res, res+n, 501);
for(int i = 0; i < n; i++){
cin>>cs[i];
}
vector<Graph> s[n];
for(int i = 0; i < m; i++){
int a, b, c;
cin>>a>>b>>c;
s[a].push_back(Graph(b, c));
s[b].push_back(Graph(a, c));
}
res[sx] = 0;
dotlist[sx] = cs[sx];
path[sx] = 1;
int flag = 0; //记录目前城市的下标
for(int i = 0; i < n; i++){
int min = 502;
for(int j = 0; j < n; j++){
if(!judge[j] && res[j] < min){
min = res[j];
flag = j;
}
}
int num = res[flag];
judge[flag] = 1;
for(int j = 0; j < s[flag].size(); j++){
int e = s[flag][j].edge;
int w = s[flag][j].weight;
if(!judge[e] && num + w < res[e]){
res[e] = num + w;
dotlist[e] = dotlist[flag] + cs[e];
pre[e] = flag;
path[e] = path[flag];
}else if(num + w == res[e]){
path[e] += path[flag];
if(dotlist[flag] + cs[e] > dotlist[e]){
dotlist[e] = dotlist[flag] + cs[e];
pre[e] = flag;
}
}
}
}
memset(res, 0, sizeof(res));
int r = sy, k = 0;
while(r != sx){
res[k++] = pre[r];
r = pre[r];
}
cout<<path[sy]<<' '<<dotlist[sy]<<endl;
for(int i = k-1; i >= 0; i--){
cout<<res[i]<<' ';
}
cout<<sy;
return 0;
}
5. C ++の質問を書く過程で一般的に使用される要約
-
関数memset()およびfill()の使用
-
文字と数値の変換stringstream
-
一般的な文字列操作length()、substr()など。
-
ソート関数sort()および(一時的に受け入れられない)構造ソートの使用
/* 小李老师因为有事情要忙,交给小王一个任务,这个任务就是帮他把几个学生排好序 排序规则按照年级降序排序,如果年级相同则按照姓名升序排序,如果姓名相同则按照年龄降序排序。 输入 第一行给出学生总人数N, 接下来N行给出每个学生信息,格式:年级 姓名 年龄 输出 按照排序规则输出学生信息 输入样例 1 2 2 wps 20 2 lsn 8 输出样例 1 lsn 8 wps 20 */
-
問題解決の過程で遭遇しやすい落とし穴!