每日算法(二)算法笔记例题

学习《算法笔记》,做一下里面的例题(看完题后写的,非官方答案)

PAT B1001 害死人不偿命的(3n+1) 猜想


#include
#include
using namespace std;

int main() {
	int n ;
	scanf("%d",&n);
	int step = 0;
	while(n!=1) {
		if(n%2) {
			//3n  在c++中一定要写成3*n 
			n = (3*n+1)/2;
		}else {
			n = n/2;
		} 
	step++;
	}
	printf("%d", step);
	return 0;
} 
PAT B1032 挖掘机技术哪家强


#include
#include
using namespace std;
//#define MAX 100000  虽然这样也可以但是还是推荐使用cosnt 去定义一个常量 
const int MAX = 10000; 
int school_score[MAX] = {};
int main() {
	int size,num,score;
	scanf("%d",&size);
	int max_num = 1;
	for(int i = 0; i < size; i++) {
		//scanf 遇到空格或者换行 结束一次的输入 
		scanf("%d%d",&num,&score);
		school_score[num] += score;
		if(num > max_num ) {
			max_num = num;
		}
	}
	int max_score_index = 0;
	for(int j = 1; j <= max_num;j++) {
		if(school_score[j] > school_score[max_score_index]) {
			max_score_index  = j;
		}
	} 
	printf("%d %d",max_score_index,school_score[max_score_index]); 
	return 0;
} 
codeup 1934 找X


#include
using namespace std;
int main() {
	int size;
	scanf("%d",&size);
	int nums[size] = {};
	for(int i=0;i
PAT B1036 跟奥巴马一起编程


#include
#include

int main() {
	int nums,rows;
	char feature;
	scanf("%d %c",&nums,&feature);
	rows = round(nums/2);
	for(int i=0; i





你可能感兴趣的:(C++,算法)