【杭电oj】练习自用

Problem 2000

很搞笑,我居然这个代码还能写错,真的是太久没写了,手生的要死。

#include<iostream>
#include<vector>
using namespace std;

void swap(char& a, char& b) {
	char temp;
	temp = a;
	a = b;
	b = temp;
}

int main() {
	char a,b,c;
	while (cin >> a>>b>>c) {
		if (b < a) {
			swap(a, b);
		}
		if (c< b) {
			swap(b, c);
		}
		if (b < a) {
			swap(a, b);
		}
		cout << a <<" " << b << " " << c << endl;
	}
	return 0;
}

Problem 2001

这个题目值得记住的是,double的输出的精度问题,需要三个东西:①头文件iomanip ②setfixed ③setprecision(n)

#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;

double getDistance(double &a, double& b, double& c, double& d ){
	double dis1 = a -c;
	double dis2 = b - d;
	return sqrt(dis1 * dis1 + dis2 * dis2);
}

int main() {
	double a,b,c,d;
	while (cin >> a>>b>>c>>d) {
		cout <<fixed<<setprecision(2) << getDistance(a, b, c, d) << endl;
	}
	return 0;
}

Problem 2002

这题比较关键的内容是不能用整数的除法去计算,要计算4.0/3.0

#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;


const double PI = 3.1415927;

int main() {
	double r;
	while (cin >>r) {
		cout <<fixed<<setprecision(3) << 4.0/3.0 *PI *r*r*r << endl;
	}
	return 0;
}

Problem 2004

好简单的题,大学读了四年还是在做这种题,好累呀,生活好可怕。

#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;

int main() {
	int grade;
	while (cin >>grade) {
		if (grade>=90 && grade<=100){
			cout << "A" << endl;
		}
		else if (grade >= 80 && grade <= 89) {
			cout << "B" << endl;
		}else if (grade >= 70 && grade <= 79) {
			cout << "C" << endl;
		}else if (grade >= 60 && grade <= 69) {
			cout << "D" << endl;
		}else if (grade >= 0 && grade <= 59) {
			cout << "E" << endl;
		}
		else {
			cout << "Score is error!" << endl;
		}
		
	}
	return 0;
}

Problem 2005

这题比较重要的是,C++从格式化的输入里面得到想要的值,需要用到sscanf(date.c_str,"%d/%d/%d’,&a,&b,&c);
还有就是闰年的计算方法。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	string date;
	int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int year, month, day;
	int sum=0;
	while (cin >>date) {
		sscanf_s(date.c_str(), "%d/%d/%d", &year, &month, &day);
		for (int i = 0; i < month-1; i++) {
			sum = sum + days[i];
		}
		sum = sum + day;
		if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {
			if (month > 2) {
				sum++;
			}
		}
		cout << sum << endl;
		sum = 0;
	}
	return 0;
}

Problem 2006

太简单了,服了。。。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	int num;
	int sum = 1;
	int temp;
	while (cin >>num) {
		for (int i = 0; i < num; i++) {
			cin >> temp;
			if (temp % 2 == 1) {
				sum = sum * temp;
			}
		}
		cout << sum << endl;
		sum = 1;
	}
	return 0;
}

Problem 2007

有点坑,因为没说给的两个数字的大小关系,我默认就是按照大小顺序给的,无语。。。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	int begin,end;
	int sum_e = 0,sum_o=0;
	while (cin >>begin>>end) {
		if (begin > end) {
			int temp;
			temp = begin;
			begin = end;
			end = temp;
		}
		for (int i = begin; i < end+1; i++) {
			if (i % 2 == 0) {
				sum_e = sum_e + pow(i,2);
			}
			else {
				sum_o = sum_o + pow(i, 3);
			}
		}
		cout << sum_e<<" "<<sum_o << endl;
		sum_e = 0;
		sum_o = 0;
	}
	return 0;
}

Problem 2008

值得注意的是,个数是int,但是统计的数值不能用int

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	int num;
	int sum_z = 0,sum_f=0,sum_0=0;
	double temp;
	while (cin >>num && num!=0) {
		for (int i = 0; i <num ; i++) {
			cin >> temp;
			if (temp > 0) {
				sum_z++;
			}
			else if (temp < 0) {
				sum_f++;
			}
			else {
				sum_0++;
			}
		}
		cout << sum_f<<" "<<sum_0 <<  " " << sum_z << endl;
		sum_z = 0;
		sum_f = 0;
		sum_0 = 0;
	}
	return 0;
}

Problem 2009

这个题目值得注意的是,int和double的区别,要重新赋值,算的才准确。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	int num,count;
	double sum = 0,temp;
	while (cin >>num >>count) {
		temp = num;
		for (int i = 0; i <count ; i++) {
			sum = sum + temp;
			temp = sqrt(temp);
		}
		cout <<fixed<<setprecision(2)<< sum << endl;
		sum = 0;
	}
	return 0;
}

Problem 2010

水仙花数还是很好玩的,比较有意思的是,我又忘记了重置了。。。。好玩好玩,就喜欢做这种不费脑子的简单题。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	int begin,end;
	int x, y, z,flag=0,cnt=0;
	while (cin >>begin >>end) {
		for (int i = begin; i <end+1 ; i++) {
			x = i % 10;
			y = (i / 10) % 10;
			z = i / 100;
			if (i == x * x * x + y * y * y + z * z * z ) {
				cnt++;
				if (flag == 0) {
					cout << i;
					flag = 1;
				}
				else {
					cout <<" "<< i;
				}
			}
		}
		if (cnt == 0) {
			cout << "no"<<endl;
		}
		else {
			cout << endl;
		}

		flag = 0;
		cnt = 0;
	}
	return 0;
}

Problem 2011

值得注意的是,当flag被定义为int类型的时候,flag/2是等于0的,必须要定义为double类型才能达到我想要的效果。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
using namespace std;

int main() {
	int num;
	int temp;
	double sum = 0;
	double flag = 1;
	//int x, y, z, flag = 0, cnt = 0;
	while (cin >> num) {
		for (int i = 0; i < num; i++) {
			cin >> temp;
			for (int j = 0; j < temp; j++) {
				sum = sum + flag / (j+1);
				flag = -flag;
			}
			cout << fixed << setprecision(2) << sum << endl;
			sum = 0;
			flag = 1;
		}
	}
	return 0;
}

Problem 2015

前面的题目写了没保存真的好想死,另外,这么一个简单的题,我活活在这耗了两个小时,一个废柴程序猿想哭哭不出,想笑不能笑。。。。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n, m;
	int cnt, sum=0;
	int first ;
	int flag;
	while (cin >> n >> m) {
		first = 2;
		flag = 0;
		sum = 0;
		cnt = m;
		for (int i = 0; i < n; i++) {
			if (cnt != 0 ) {
				sum = sum + first;
				first = first + 2;
				cnt--;
			}
			if (i == n - 1 && cnt != 0) {
				if (flag == 0) {
					cout << sum / m;
					flag = 1;
				}
				else {
					cout << " " << sum / (n % m);
				}
			}
			if(cnt==0 ) {
				if (flag == 0) {
					cout << sum / m;
					flag = 1;
				}
				else {
					cout << " " << sum / m;
				}
				sum = 0;
				cnt = m;
			}
			
			
		}
		cout << endl;
	}
	return 0;
}

看不下去了,这么简单一个代码被我写的这么啰嗦。。。。让豆包给我优化了一下。。。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n, m;
	int CurrentCount, CurrentSum=0;
	int first ;
	int flag;
	int isFirst=1;
	while (cin >> n >> m) {
		first = 2;
		flag = 0;
		CurrentSum = 0;
		CurrentCount = 0;
		for (int i = 0; i < n; i++) {
			//先计算加法再说
			CurrentSum = CurrentSum + first;
			first = first + 2;
			CurrentCount++;
			if (CurrentCount == m || i == n - 1) {
				int average = static_cast<double>(CurrentSum) / CurrentCount;
				CurrentCount = 0;
				CurrentSum = 0;
				if (!isFirst) {
					cout << " ";
				}
				isFirst = 0;
				cout << fixed << setprecision(0) << average;
			}


		}
		isFirst = 1;
		cout << endl;
	}
	return 0;
}

Problem 2016

这题对vector的使用感觉有点意思,重新了解了一下迭代器的用法,就理解成指针就好了,这么多函数,感觉C++方便了之后,编程起来挺像python一样舒服的。
学习一下std库里面的swap函数,挺简单的,不用自己去编程了。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n;
	vector<int> nums;
	while (cin >> n) {
		int temp;
		for (int i = 0; i < n; i++) {
			cin >> temp;
			nums.push_back(temp);
		}
		auto maxIt = min_element(nums.begin(), nums.end());
		swap(nums[0], *maxIt);
		for (int i=0; i < n; i++) {
			if (i == 0) {
				cout << nums[i];
			}
			else {
				cout << " " << nums[i];
			}
		}
		cout << endl;
		nums.clear();
	}
	return 0;
}

Problem 2017

这题比较有意思的是,我学会了遍历string中的char,又回到了四年前努力学python的感觉。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n;
	//int flag;
	//vector<int> strs;
	while (cin >> n) {
		for (int i = 0; i < n; i++) {
			int numCount = 0;
			string temp;
			cin >> temp;
			for (char c : temp) {
				if (c >= '0' && c <= '9') {
					numCount++;
				}
			}
			cout << numCount << endl;
			numCount = 0;
		}
	}
	return 0;
}

Problem 2018

能想到用vector去处理元素,我感觉我还是蛮聪明的,这题写完之后还是有点模模糊糊。然后让豆包给我分析了一下,在第n年的时候,就不用加上母牛新生这种情况了。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n;
	while (cin >> n && n!=0) {
		vector<int> cows={1,0,0,0};
		for (int i = 0; i < n; i++) {
			//每年都有新的小牛长成母牛
			cows[0] = cows[0] + cows[3];
			cows[3] = cows[2];
			cows[2] = cows[1];
			//每年所有的母牛都会生一个小牛
			cows[1] = cows[0];
		}
		cout << cows[0]+cows[2]+cows[3] << endl;
	}
	return 0;
}

在这里插入图片描述

Problem 2019

需要学习vector的迭代器遍历vector<int>::iterator和插入nums.insert(it+1,m);

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n,m;
	while (cin >> n >>m && !(n==0 && m==0)) {
		vector<int> nums;
		int temp;
		for (int i = 0; i < n; i++) {
			cin >> temp;
			nums.push_back(temp);
		}
		for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++) {
			if (*it < m && *(it+1) >= m) {
				nums.insert(it+1,m);
				break;
			}
		}
		for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++) {
			if (it == nums.begin()) {
				cout << *it;
			}
			else {
				cout << " " << *it;
			}
		}
		nums.clear();
		cout << endl;
	}
	return 0;
}

Problem 2020

C++ 真的好方便,有很多函数和方法不需要自己实现,比如这个std库里面的sort函数,就是要自己写一个numbda匿名函数来定义比较规则,让我来背一背[](int a,int b){ return abs(a)>abs(b)}

在这里插入图片描述

#include<iostream>
#include<vector>
#include<iomanip>
//#include <string>
//#include<algorithm>
using namespace std;

int main() {
	int n;
	while (cin >> n && n!=0 ) {
		int temp;
		vector<int> nums;
		for (int i = 0; i < n; i++) {
			cin >> temp;
			nums.push_back(temp);
		}
		//冒泡排序
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n-i-1; j++) {
				if (abs(nums[j]) < abs(nums[j + 1])) {
					swap(nums[j], nums[j + 1]);
				}
			}
		}
		for (int i = 0; i < n; i++) {
			if (i == 0) {
				cout << nums[i];
			}
			else {
				cout << " "<<nums[i];
			}
		}
		cout << endl;
		nums.clear();
	}
	return 0;
}

Problem 2021

这种写出来就被accept的感觉太好了,好喜欢这种感觉。

#include<iostream>
#include<vector>
#include<iomanip>
//#include <string>
//#include<algorithm>
using namespace std;

int main() {
	int n;
	while (cin >> n && n!=0 ) {
		int temp;
		vector<int> nums={0,0,0,0,0,0};
		for (int i = 0; i < n; i++) {
			cin >> temp;
			nums[0] = nums[0] + temp / 100;
			temp = temp % 100;
			nums[1] = nums[1] + temp /50;
			temp = temp % 50;
			nums[2] = nums[2] + temp / 10;
			temp = temp % 10;
			nums[3] = nums[3] + temp / 5;
			temp = temp % 5;
			nums[4] = nums[4] + temp / 2;
			nums[5] = nums[5] + temp % 2;
		}
		cout << nums[0] + nums[1] + nums[2] + nums[3] + nums[4] + nums[5] << endl;
		for (size_t i = 0; i < nums.size(); ++i) {
			nums[i] = 0;
		}
	}
	return 0;
}

Problem 2022

这题其实就是想考察二维数组,但是题出的太简单了,我都没有用上二维的vector,vector<vector<int>> nums(3,vector<int>(5,0))

#include<iostream>
#include<vector>
#include<iomanip>
//#include <string>
//#include<algorithm>
using namespace std;

int main() {
	int n, m;
	while (cin >> n >> m ){
		int row=0, col=0,temp=0,CurrentNum=0;
		//vector<vector<int>> nums(n,vector<int>(m,0));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> temp;
				if (abs(temp) > abs(CurrentNum)) {
					row = i;
					col = j;
					CurrentNum = temp;
				}
				//nums[i][j] = temp;
			}
		}
		cout << row+1 <<" "<< col+1 <<" " << CurrentNum << endl;
		
	}
	return 0;
}

Problem 2023

这题是真的挺有难度的,我 主要学到了二维vector的初始化,还有就是按行和列去计算二维矩阵,只需要交换一下外层和内层的for循环。

#include<iostream>
#include<vector>
#include<iomanip>
//#include <string>
//#include<algorithm>
using namespace std;

int main() {
	int n, m;
	while (cin >> n >> m ){
		vector<vector<double>> courses(m,vector<double>(n));
		double temp;
		//总共有m门课程
		//先初始化,再输入填充
		for (int student = 0; student < n; student++) {
			for (int course = 0; course < m; course++) {
				cin >> temp;
				courses[course][student] = temp;
			}
		}

		//计算每个学生的平均成绩(按列来计算输出)
		int flag=1;
		double sum = 0;
		for (int student = 0; student < n; student++) {
			for (int course = 0; course < m; course++) {
				//按列来加
				sum = sum + courses[course][student];
			}
			if (flag != 1) {
				cout << " ";
			}
			cout <<fixed<<setprecision(2)<< sum / m;
			sum = 0;
			flag = 0;
		}
		cout << endl;
		vector<double> aveCode;
		//计算每门课的平均成绩
		double sum_c = 0;
		flag = 1;
		for (int course = 0; course < m; course++) {
			for (int student = 0; student < n; student++) {
				//按行来加
				sum_c = sum_c + courses[course][student];
			}
			if (flag != 1) {
				cout << " ";
			}
			cout << fixed << setprecision(2) << sum_c / n;
			aveCode.push_back(sum_c / n);
			sum_c = 0;
			flag = 0;
		}
		cout << endl;

		int stuNums = 0;
		//计算每个学生的分数和平均成绩的关系
		for (int student = 0; student < n; student++) {
			int flag = 1;
			for (int course = 0; course < m; course++) {
				//按行来加
				if (courses[course][student] < aveCode[course]) {
					flag = 0;
					break;
				}
			}
			if (flag == 1) {
				stuNums++;
			}
		}
		cout << stuNums << endl<<endl;
	}
	return 0;
}

Problem 2024

这题有点给我耍心机,一般字符串输入会被空格给阻断,这里首先要吃一个换行符getchar() ,然后就是用C++ 输入行的公式getline(cin,str)。其他的内容倒是不难,这种编程题,编起来有种越编越爽的感觉。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
//#include<algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	getchar();
	// 清除输入缓冲区中的换行符
	//cin.ignore(numeric_limits<streamsize>::max(), '\n');
	string str;
	for (int i = 0; i < n; i++) {
		
		getline(cin, str);
		//判断首字母是否
		int flag = 1;
		for (int i = 0; i < str.length();i++) {
			if (i == 0 && !(str[i] == '_' || (str[i] >= 'a' && str[i] <= 'z')|| (str[i] >= 'A' && str[i] <= 'Z'))) {
				cout<<"no"<<endl;
				flag = 0;
				break;
			}
			if (i != 0) {
				if (!((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '_')) {
					cout << "no" << endl;
					flag = 0;
					break;
				}
			}
		}
		if (flag == 1) {
			cout << "yes" << endl;
		}
	}
	return 0;
}

Problem 2025

这题思路要灵活,多次插入找位置麻烦,所以可以准备一个新的str去装,然后就很简单了,不要用insert插入。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
//#include<algorithm>
using namespace std;

int main() {
	string str;
	while (getline(cin,str)) {
		//先找到最大的字符串
		char max = str[0];
		for (size_t i = 0; i < str.length(); i++) {
			if (str[i] > max) {
				max = str[i];
			}
		}
		string res = "";
		for (size_t i = 0; i < str.length(); i++) {
			res += str[i];
			if (str[i] == max) {
				res += "(max)";
			}
		}
		cout << res << endl;
	}
	
	
	return 0;
}

Problem 2026

强化了一下getline的使用方法,而且对于大小写的转换,C语言里面直接用assic码来加减32,小写变大写,减32,C++里面有专门的函数,islower和toupper

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
//#include<algorithm>
using namespace std;

int main() {
	string str;
	while (getline(cin,str)) {
		//先找到最大的字符串
		if (islower(str[0])) {
			str[0] = toupper(str[0]);
		}
		for (int i = 0; i < str.length(); i++) {
			if (str[i] == ' '&& islower(str[i+1])) {
				str[i + 1] = toupper(str[i + 1]);
			}
		}
		cout << str << endl;
	}
	return 0;
}

Problem 2027

看到这么多个if-else就觉得很好笑,梦回实习的代码。比较简洁的代码是用双层for循环来解决 ,固定aeiou的位置和nums的个数的位置。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
//#include<algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	getchar();
	string str;
	for (int i = 0; i < n; i++) {
		int nums[] = { 0,0,0,0,0 };
		getline(cin, str);
		for (int j = 0; j < str.length(); j++) {
			if (str[j] == 'a') {
				nums[0]++;
			}
			else if (str[j] == 'e') {
				nums[1]++;
			}else if (str[j] == 'i') {
				nums[2]++;
			}else if (str[j] == 'o') {
				nums[3]++;
			}else if (str[j] == 'u') {
				nums[4]++;
			}
		}
		for (int j = 0; j < 5; j++) {
			if (j==0) {
				cout << "a:" << nums[0] << endl;
			}
			else if (j == 1) {
				cout << "e:" << nums[1] << endl;
			}
			else if (j == 2) {
				cout << "i:" << nums[2] << endl;
			}
			else if (j == 3) {
				cout << "o:" << nums[3] << endl;
			}
			else {
				cout << "u:" << nums[4] << endl;
			}
		}
		cout << endl;
	}
	return 0;
}

附上豆包给的代码:

#include <iostream>
#include <string>

int main() {
    int n;
    std::cin >> n;
    std::cin.ignore();

    char vowels[] = {'a', 'e', 'i', 'o', 'u'};

    for (int i = 0; i < n; ++i) {
        std::string str;
        std::getline(std::cin, str);

        int nums[5] = {0};
        for (char c : str) {
            for (int j = 0; j < 5; ++j) {
                if (c == vowels[j]) {
                    nums[j]++;
                    break;
                }
            }
        }

        for (int j = 0; j < 5; ++j) {
            std::cout << vowels[j] << ":" << nums[j] << std::endl;
        }
        std::cout << std::endl;
    }

    return 0;
}    

Problem 2028 最大公约数和最小公倍数问题

① 辗转相除法求最大公倍数,先求余数,b给a,余数给b(为什么呢?因为余数一定是比a和b都小的,最开始a比b小也没事,while循环一次就纠正过来了。)
②求最小公倍数的时候,是用a*b / gcd(a,b),最好是先除后乘,否则除法会越界。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int gcd(int a, int b) {
	while (b != 0) {
		int temp = a % b;
		a = b;
		b = temp;
	}
	return a;
}

int lcm(int a, int b) {
	return a  / gcd(a, b) * b;
}

int main() {
	int n;
	int a, b;
	while (cin >> n) {
		cin >> a >> b;
		int res = lcm(a, b);
		for (int i = 0; i < n - 2; i++) {
			cin >> a;
			res = lcm(res, a);
		}
		cout << res << endl;
	}
	return 0;
}

Problem 2029 回文串判断 字符串reverse

在python里面学回文串的时候就觉得reverse函数很好用,那现在cpp里面也有,在aigrothm库里面,记一下用法,reverse(str.begin(),str.end())

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n;
	string str,str_res;
	while (cin>>n) {
		getchar();
		for (int i = 0; i < n; i++) {
			getline(cin, str);
			/*str_res = str;
			reverse(str_res.begin(), str_res.end());
			if (str.compare(str_res) == 0) {
				cout << "yes" << endl;
			}
			else {
				cout << "no" << endl;
			}*/
			auto Ite_Beg = str.begin();
			auto Ite_End = str.end()-1;
			int flag = 1;
			while (!(Ite_Beg == Ite_End || Ite_Beg == Ite_End + 1)) {
				if (*Ite_Beg == *Ite_End) {
					Ite_Beg++;
					Ite_End--;
				}
				else {
					flag = 0;
					cout << "no" << endl;
					break;
				}
			}
			if (flag == 1) {
				cout << "yes" << endl;
			}
		}
	}
	return 0;
}

Problem 2030 汉字的assic码的特点 小于0

还记得大一学python的时候,老师就讲过汉字的编码,好怀念从前,好怀念我的青春。

#include<iostream>
#include<vector>
#include<iomanip>
#include <string>
#include<algorithm>
using namespace std;

int main() {
	int n;
	string str,str_res;
	while (cin>>n) {
		int num = 0;
		getchar();
		for (int i = 0; i < n; i++) {
			getline(cin, str);
			for (int i = 0; i < str.length(); i++) {
				if (str[i] < 0) {
					num++;
				}
			}
			cout << num / 2 << endl;
			num = 0;
		}
	}
	return 0;
}

Problem 1232 并查集

本来以为这对我来说,是要放弃的东西,是跨不过去的坎,但是没想到大概花了一个小时,还是把这个bug给解决掉了。

#include<iostream>
#include<vector>
using namespace std;

int find(int a, vector<int>& par) {
	if (par[a] == 0) return a;
	return par[a]=find(par[a],par);
}

void uon(int a, int b,vector<int> &par) {
	//让b作为a的老爸,而不是让b的老爸作为a的老爸
	//当根相同时,不用管,当根不同的时候,合并根
	if (find(a, par) != find(b, par)) {
		par[find(a, par)] = find(b, par);
	}
}



int main() {
	int town, road;
	while ((cin >> town) && town != 0) {
		cin >> road;
		vector<int> parent(town+1,0);
		int town1, town2;
		for (int i = 0; i < road; i++) {
			cin >> town1 >> town2;
			uon(town1, town2, parent);
		}
		//找到vecter中有几个-1
		int count = 0;
		for (int i = 1; i < town+1; i++) {
			if (parent[i] == 0) {
				count++;
			}
		}
		cout << count - 1 << endl;
	}
}

Problem 1032 杨辉三角

写代码好开心,还不错,感觉。

#include<iostream>
#include<vector>
using namespace std;

int main() {
	int n=1;
	while (cin >> n) {
		vector<vector<int>> triangle(n, vector<int>(n, 0));
		if (n >= 1) {
			triangle[0][0] = 1;
		}
		if (n >= 2) {
			triangle[1][0] = 1;
			triangle[1][1] = 1;
		}
		for (int i = 2; i < n; i++) {
			triangle[i][0] = 1;
			triangle[i][i] = 1;
			for (int j = 1; j < i; j++) {
				triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < i + 1; j++) {
				if (j != 0) {
					cout << " ";
				}
				cout << triangle[i][j];
			}
			cout << endl;
		}
		cout << endl;
	}
}

Problem 2040 亲和数

两个月没写代码了,感觉手还没有很生,挺好的。最近感觉自己焦虑症又变严重了,写代码好,写代码挺好的,写代码很容易进入到心流状态,不会走神。写代码真好,能干活,真好。

#include<iostream>
#include<vector>
using namespace std;

int main() {
	int n;
	int num_a, num_b;
	cin >> n;
	while (n!=0) {
		n--;
		cin >> num_a >> num_b;
		int sum_a=1, sum_b=1;
		for (int i = 2; i < sqrt(num_b); i++) {
			if (num_b % i == 0) {
				sum_a = sum_a + i+num_b/i;
			}
		}
		for (int i = 2; i < sqrt(num_a); i++) {
			if (num_a % i == 0) {
				sum_b = sum_b + i+num_a/i;
			}
		}
		if (sum_a == num_a && sum_b == num_b) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}
}

Problem 2042

简单的编程题

#include<iostream>
#include<vector>
using namespace std;

int main() {
	int n;
	int num;
	cin >> n;
	while (n!=0) {
		n--;
		cin >> num;
		int sum = 3;
		while (num != 0) {
			sum = (sum - 1) * 2;
			num--;
		}
		cout << sum << endl;
	}
}

Problem 2054 A==B? 有点坑的题

感觉比较好玩的点是这个题可以用在前端的代码里面,提高代码的健壮性,还挺好玩的,有点坑的话,我觉得这个题应该说明一下,在示例里面就给出来5.000和5这样的例子。

#include<iostream>
#include<vector>
#include<string>
using namespace std;

string remove_zero(string str) {
	int pos;
	pos = str.find('.');
	if (pos != string::npos) {
		int i = str.length()-1;
		while (i != pos) {
			if (str[i] == '0') {
				str.replace(i, 1, "");
			}
			else {
				break;
			}
			i--;
		}
		if (i == pos) {
			str.replace(i, 1, "");
		}
	}
	return str;
}

int main() {
	string m,n;
	while (cin>>m>>n) {
		if (remove_zero(m) == remove_zero(n)) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}
}

Problem 2025 An easy problem

写这种简单题就是在满足人的虚荣心,因为你会觉得很顺畅,自己很喜欢这成功的感觉。

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main() {
	int n;
	cin >> n;
	while (n!=0) {
		n--;
		char c;
		int sum=0;
		cin >> c >> sum;
		if (c <= 'Z' && c >= 'A') {
			sum = sum + c - 'A' + 1;
		}
		else {
			sum = sum -( c - 'a' + 1);
		}
		cout << sum << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值