debug錯誤彙總

2020.5.28

1.賦值語句中=寫成==。maxheight=height;

2.遍歷容器vector的範圍。for(int i =0;i<temp.size();i++) 循環判斷條件不可以用<=.

3.set.end()返回的是set的大小,而不是set最後一個元素。

2020.5.29

1.

結構體可以整體直接賦值,F=temp。不可以只賦值一些,後面還需用更新後的值進行比較運算。

2020.5.30

1.輸入格式爲:第一行一個數字n,跟着n行字符串。這樣的情況輸入時要注意數字後的換行符,要用getchar()。

2020.6.7

1.

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
using namespace std;
bool cmp(char a, char b) {
	return a > b;
}
int main() {
	string s;
	cin >> s;
	s.insert(0, 4 - s.length(), '0');
	do {
		string a = s, b = s;
		sort(a.begin(), a.end(), cmp);
		sort(b.begin(), b.end());
		int ans = stoi(a) - stoi(b);
		s = to_string(ans);
		s.insert(0, 4 - s.length(), '0');
		cout << a << " - " << b << " = " << s << endl;
	} while (s != "6174"&&s != "0000");
	system("pause");
	return 0;
}

在這份代碼中,dowhile循環裏的s前不可以再定義string,否則會導致a,b一直等於循環體外輸入的s,造成死循環,運行超時。

2020.6.10

判斷素數:需要判定n能否被2,3,...,(int)sqrt(n)中的一個整除,即可判定n是否爲素數。

bool isprime(int a) {
	for (int i = 2;i*i <= a;i++)
		if(a%i==0)
		return false;
	return true;
}

這是正確的寫法,注意從2開始而不是0,結束條件包含i*i=a,不要寫成<。

2020.6.15

void init() {
	int i = 0, t = 0;
	while (t <= n) {
		fac.push_back(t);
		t = power(++i);
	}
}

初始化函數用到了全局變量n,因此在主函數中要先輸入n,纔可以調用init()函數。

cin >> n >> k >> p;
	init();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章