1. 程式人生 > >NUOJ2.0入門題目部分(C++)

NUOJ2.0入門題目部分(C++)

練習平臺 : http://nyoj.top/

在分類中選擇入門題目

括號配對問題

#include<iostream>
#include<string>
using namespace std;
int main() {
	int N;
	string str;
	cin >> N;
	for (int n = 0; n < N; n++) {
		int i = 0;
		cin >> str;
		if (int(str.length()) % 2 == 1) {
			cout << "No" << endl;
			continue;
		}
		int sum = str.find("()");
		if (sum < 0) {
			sum = str.find("[]");
		}
		while (!(sum<0)) {
			string str1 = str.substr(0, sum);
			string str2 = str.substr(sum + 2);
			str = str1 + str2;                   //如果找到配對的括號就刪除它
			if (str.length() ==0) {
				i = 1;
				break;
			}
			sum = str.find("()");
			if (sum < 0) {
				sum = str.find("[]");
			}
		}
		if (i == 1)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
}

4. ASCII碼排序

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main() {
	string str;
	int N;
	cin >> N;
	for (int n = 0; n < N; n++) {
		cin >> str;
		sort(str.begin(),str.end());
		cout << str[0] << " " << str[1]<<" "<<str[2] << endl;
	}
}

5. Binary String Matching

#include<iostream>
#include<string>
using namespace std;
int main() {
	string str,str_son;
	int N;
	cin >> N;
	for (int n = 0; n < N; n++) {
		int sum = 0;
		cin >> str_son >> str;
		int i = str.find(str_son);
		while (!(i < 0)) {
			str = str.substr(i+1);
			i = str.find(str_son);
			sum++;
		}
		cout << sum << endl;

	}
}

11. 奇偶數分離

#include<iostream>
using namespace std;
void ji(int n) {
	if(n!=1)
	ji(n - 2);
	cout << n<<" ";
}
void ou(int n) {
	if(n!=2)
	ou(n - 2);
	cout << n <<" ";
}
int main() {
	int i;
	cin >> i;
	for (int j = 0; j < i; j++) {
		int n;
		cin >> n;
		ji(n - 1); cout << endl;
		ou(n); cout << endl << endl;  //注意題目要求的格式
	}
}

39. 水仙花數

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	while (n != 0) {
		int a = n / 100;
		int b = n / 10 % 10;
		int c = n % 10;
		if (a*a*a + b * b*b + c * c*c == n)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
		cin >> n;
	}
}

41. 三個數從小到大排序

#include<algorithm>
#include<iostream>
using namespace std;
int main() {
	int a[3];
	cin >> a[0] >> a[1] >> a[2];
	sort(a+0 , a + 3);             //從第一個到第三個
	cout << a[0] <<" "<< a[1] << " "<<a[2] <<endl;
}