算法笔记--简单模拟
【PAT B1001】害死人不偿命的3n+1猜想
#include<stdio.h> int main(){ int step = 0; int n; printf("输入n的值:\t"); scanf("%d", &n); while(n != 1){ if(n % 2 == 0){ n = n / 2; step++; }else{ n = (3*n + 1) / 2; step++; } } printf("%d",step); return 0; }
【PAT B1032】挖掘机技术哪家强
#include<stdio.h> int main(){ const int max = 100010; int num; int school[max] = {0}; int sid, score; scanf("%d", &num); for(int i=0; i < num; i++){ scanf("%d %d", &sid, &score); school[sid] = school[sid] + score; } int maxscore = -1, k = -1; for(int i=0; i < num; i++){ if(school[i] > maxscore){ maxscore = school[i]; k = i; } } printf("%d %d", k, maxscore); return 0; }