Table of contents
Preface
Everyone has hair on their body Everyone’s code is stupid, and coincidentally, I have it too! As soon as I saw the CSDN activity "What is the stupidest code you have ever written?" 》, I immediately wanted to participate to tell everyone about some of my stupid codes and the shortcomings of these codes, and how we can avoid them. Surprise
text
Stupid code - 1
Everyone should be familiar with intervals! It's OK to use the prefix sum, but I...
#include <bits/stdc++.h>
using namespace std;
int a[100005]
int main()
{
int n;
cin>>n;
for(int i=1; i<=n; i++) cin>>a[i];
int sum=0;
int l,r;
cin>>l>>r;
for(int i=l; i<=r; i++) sum+=a[i];
cout<<sum;
return 0;
}
Violence works wonders!
Finally, I would like to mention a TLE... The most appropriate thing to do is to use the prefix sum (will be discussed later):
#include <iostream>
using namespace std;
int a[100005],n,q,l,r,sa[100005];
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
sa[i]=a[i]+sa[i-1];
}
cin>>l>>r;
cout<<sa[r]-sa[l-1]<<endl;
return 0;
}
Stupid Code - 2
A+B Problem is familiar to everyone, I should have written:
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
But what I wrote is:
#include <iostream>
using namespace std;
int main()
{
int 博主,牛掰;
cin>>博主>>牛掰;
cout<<博主+牛掰;
return 0;
}
(Note: Statements are very dangerous in C++)
Stupid Code - 3
The first time I wrote a binary tree + preorder traversal was the first time. When writing a binary tree, I did not use new to give the binary tree memory space, as follows:
#include <iostream>
using namespace std;
struct Node {
int value;
Node* left;
Node* right;
};
void qianxv(Node* root) {
if (root == nullptr) {
return;
}
cout << root->value << " ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
int main() {
Node* tree;
tree->value = 1;
tree->left->value = 3;
tree->left->left->value = 114;
tree->left->right->value = 114514;
cout << "前序遍历结果:" << endl;
preOrderTraversal(tree);
return 0;
}
喜提 E R R O R {\Huge \mathbf{ {\color{Red} ERROR}}} ERROR One piece!
正确 {\Huge \mathbf{ {\color{Red} 正确}}} correctly correctness:
#include <iostream>
using namespace std;
struct Node {
int value;
Node* left;
Node* right;
};
void qianxv(Node* root) {
if (root == nullptr) {
return;
}
cout << root->value << " ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
int main() {
Node* tree = new Node();
tree->value = 1;
tree->left = new Node();
tree->left->value = 3;
tree->left->left = new Node();
tree->left->left->value = 114;
tree->left->right = new Node();
tree->left->right->value = 114514;
cout << "前序遍历结果:" << endl;
preOrderTraversal(tree);
return 0;
}
Blogger’s feelings:Click me
remind!
Be sure to use the keyword new to open up space for the pointer!
Stupid Code - 4
Question:
∑ i = 1 N f ( i ) {\color{Red} \mathcal{
{\Huge \sum_{i=1}^{N} f(i)} } } i=1∑Nf(i)
Further definition: {\color{Red} f(N)=N approximate number} } }
f ( N ) = N approximate number {\Huge \mathcal{
f(N)=The number of divisors of N
I thought:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j*j <= i; j++) {
if (i % j == 0) {
sum++;
if (j != i / j) sum++;
}
}
}
cout << sum << endl;
return 0;
}
但 T L E {\Huge \mathbf{ {\color{Blue} TLE}}} TLE 了呜呜
The correct one should be:
#include <iostream>
using namespace std;
int main() {
int n,sum = 0;
cin >> n;
for (int i = 1; i <= n; i++) sum+=n/i;
cout << sum << endl;
return 0;
}
Be vigilant {\color{Red} \mathcal{ {\Huge Be vigilant} } } Be vigilant Blogger’s feelings:Click me
Summarize
This article talks about my stupid code, I hope you won’t imitate it!
No. 1379:
Don’t answer!
Don’t answer! !
Don’t answer! ! !
National Anti-Fraud Center: You!