• 2022天梯赛练习集(2022.9-2022.10)


    使用函数判断完全平方数 

    没有加(int)过不了 

    1. int IsSquare(int n){
    2. if((int)sqrt(n) * sqrt(n) != n) return 0;
    3. else return 1;
    4. }

    使用函数求余弦函数的近似值 

    1. double funcos(double e, double x){
    2. double sum = 1, item = 1;
    3. for(int i = 0; fabs(item) >= e; i += 2){
    4. item = (-1) * item * x * x / ((i + 1) * (i + 2));
    5. sum += item;
    6. }
    7. return sum;
    8. }

    6-13 使用函数输出水仙花数

    1. int narcissistic(int number){
    2. int n = number;
    3. int item = 0, sum = 0, cnt = 0;
    4. while(n){
    5. n /= 10;
    6. cnt ++;
    7. }
    8. item = number;
    9. while(item){
    10. int lastnumber = item % 10;
    11. sum += pow(lastnumber, cnt);
    12. item /= 10;
    13. }
    14. if(sum == number) return 1;
    15. return 0;
    16. }
    17. void PrintN(int m, int n){
    18. for(int i = m + 1; i < n; i ++ ){
    19. if(narcissistic(i)) printf("%d\n", i);
    20. }
    21. }

    6-18 使用函数输出指定范围内的完数 

    在一些高级语言当中,为了能够完成更好的逻辑判断,因此就有了bool类型,bool类型的变量值只有true和false两种。

    而在C语言中,一般认为0为假,非0为真。
    这是因为c99之前,c90是没有bool类型的的。但是c99引入了_Bool类型(_Bool就是一个类型,不过在新增头文件stdbool.h中,被重新用宏写成了 bool,为了保证C/C++兼容性)。
    目前为止大部分C语言书籍采用的标准还是c90标准,因此我们很少用bool类型。
     

    factorsum函数中如果是

    1. int factorsum( int number ){
    2. int sum = 1;
    3. for(int i = 2; i < number; i ++ ){
    4. if(number % i == 0) sum += i;
    5. }
    6. return sum;
    7. }

    过不了 最大范围测试点

    1. int factorsum( int number ){
    2. int sum = 0;
    3. for(int i = 1; i < number; i ++ ){
    4. if(number % i == 0) sum += i;
    5. }
    6. return sum;
    7. }
    8. void PrintPN( int m, int n ){
    9. int flag = 0;
    10. for(int i = m; i <= n; i ++ ){
    11. if(factorsum(i) == i){
    12. flag = 1;
    13. printf("%d = 1", i);
    14. for(int j = 2; j < i; j ++ ){
    15. if(i % j == 0) printf(" + %d", j);
    16. }
    17. printf("\n");
    18. }
    19. }
    20. if(!flag) printf("No perfect number");
    21. }

    6-19 使用函数输出指定范围内的Fibonacci数

    一开始的代码一直出现 [PTA报错]warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result 的问题

    下面的ac代码反向使用cnt,和我平时的用法感觉妙了点(?

    1. int fib( int n ){
    2. if(n == 1 || n == 2) return 1;
    3. return fib(n - 1) + fib(n - 2);
    4. }
    5. void PrintFN( int m, int n ){
    6. int cnt = 1, flag = 0;
    7. for(int i = 1; fib(i) <= n; i ++ ){
    8. if(fib(i) >= m && fib(i) <= n){
    9. if(cnt){
    10. printf("%d", fib(i));
    11. cnt = 0;
    12. }
    13. else printf(" %d", fib(i));
    14. flag = 1;
    15. }
    16. }
    17. if(!flag) printf("No Fibonacci number");
    18. }

    6-30 删除字符

    1. void delchar( char *str, char c ){
    2. int j = 0;
    3. for(int i = 0; *(str + i) !='\0'; i ++ ){
    4. if(*(str + i) != c){
    5. *(str + j) = *(str + i);
    6. j ++;
    7. }
    8. }
    9. *(str + j) = '\0';
    10. }

    6-31 字符串的连接 

    1. char *str_cat( char *s, char *t ){
    2. int len_s = strlen(s);
    3. int len_t = strlen(t);
    4. for(int i = 0; i < len_t; i ++ ){
    5. *(s + len_s + i) = *(t + i);
    6. }
    7. return &s[0];
    8. }

    6-46 指定位置输出字符串 

    1. char *match( char *s, char ch1, char ch2 ){
    2. char *p;
    3. int i;
    4. for(i = 0; i < strlen(s); i ++ ){
    5. if(s[i] == ch1) break;
    6. }
    7. p = &s[i];
    8. for(int j = i; j < strlen(s); j ++ ){
    9. printf("%c", s[j]);
    10. if(s[j] == ch2) break;
    11. }
    12. printf("\n");
    13. return p;
    14. }

    6-47 查找子串 

    1. char *search( char *s, char *t ){
    2. int flag = 0;
    3. for(int i = 0; s[i] != '\0'; i ++ ){
    4. for(int j = 0; t[j] != '\0'; j ++ ){
    5. flag = 0;
    6. if(s[i + j] != t[j]) break;
    7. else flag = 1;
    8. }
    9. if(flag) return &s[i];
    10. }
    11. return NULL;
    12. }


    6-49 建立学生信息链表

    1. void input(){
    2. struct stud_node *p;
    3. p = (struct stud_node *)malloc(sizeof(struct stud_node));
    4. scanf("%d", &p->num);
    5. while(p->num != 0){
    6. scanf("%s %d", p->name, &p->score);
    7. if(head == NULL){
    8. head = p;
    9. head->next = NULL;
    10. }
    11. if(tail != NULL) tail->next = p;
    12. tail = p;
    13. tail->next = NULL;
    14. p = (struct stud_node *)malloc(sizeof(struct stud_node));
    15. scanf("%d", &p->num);
    16. }
    17. }

    6-50 学生成绩链表处理 

    1. struct stud_node *createlist(){
    2. struct stud_node *head, *tail, *s;
    3. head = (struct stud_node *)malloc(sizeof(struct stud_node));
    4. head->next = NULL;
    5. tail = head;
    6. s = (struct stud_node *)malloc(sizeof(struct stud_node));
    7. while(1){
    8. s=(struct stud_node*)malloc(sizeof(struct stud_node));
    9. scanf("%d",&s->num);
    10. if(s->num==0)break;
    11. scanf("%s",s->name);
    12. scanf("%d",&s->score);
    13. s->next=NULL;
    14. tail->next=s;
    15. tail=s;
    16. }
    17. return head;
    18. }
    19. struct stud_node *deletelist( struct stud_node *head, int min_score ){
    20. struct stud_node *p, *n;
    21. p = head->next;
    22. while(p != NULL){
    23. if(p->score < min_score){
    24. n = head;
    25. while(n->next != p) n = n->next;
    26. n->next = p->next;
    27. free(p);
    28. }
    29. p = p->next;
    30. }
    31. return head->next;
    32. }

    6-51 逆序数据建立链表 

    1. struct ListNode *createlist(){
    2. struct ListNode *head, *tail, *p;
    3. head = tail = NULL;
    4. int num;
    5. while(scanf("%d", &num) && num != -1){
    6. p = (struct ListNode *)malloc(sizeof(struct ListNode));
    7. p->data = num;
    8. p->next = head;
    9. head = p;
    10. }
    11. return head;
    12. }

    6-52 链表拼接 

    1. struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
    2. struct ListNode *head;
    3. head = (struct ListNode *)malloc(sizeof(struct ListNode));
    4. struct ListNode *p = head, *p1 = list1, *p2 = list2;
    5. while(p1 && p2){
    6. if(p1->data < p2->data){
    7. p->next = p1;
    8. p1 = p1->next;
    9. p = p->next;
    10. }
    11. else{
    12. p->next = p2;
    13. p2 = p2->next;
    14. p = p->next;
    15. }
    16. }
    17. p->next = p1 ? p1 : p2;
    18. return head->next;
    19. }

    6-53 奇数值结点链表 

    1. struct ListNode *readlist(){
    2. struct ListNode *head = NULL;
    3. struct ListNode *current, *prev;
    4. int n;
    5. while(scanf("%d", &n) == 1 && n != -1){
    6. current = (struct ListNode *)malloc(sizeof(struct ListNode));
    7. if(head == NULL) head = current;
    8. else prev->next = current;
    9. current->next = NULL;
    10. current->data = n;
    11. prev = current;
    12. }
    13. return head;
    14. }
    15. struct ListNode *getodd( struct ListNode **L ){
    16. struct ListNode *t = *L;
    17. struct ListNode *head_even = NULL, *head_odd = NULL;
    18. struct ListNode *current_even, *current_odd, *prev_even, *prev_odd;
    19. while(t){
    20. if((t->data % 2) == 0){
    21. current_even = (struct ListNode *)malloc(sizeof(struct ListNode));
    22. if(head_even == NULL) head_even = current_even;
    23. else prev_even->next = current_even;
    24. current_even->next = NULL;
    25. current_even->data = t->data;
    26. prev_even = current_even;
    27. }
    28. else{
    29. current_odd = (struct ListNode *)malloc(sizeof(struct ListNode));
    30. if(head_odd == NULL) head_odd = current_odd;
    31. else prev_odd->next = current_odd;
    32. current_odd->next = NULL;
    33. current_odd->data = t->data;
    34. prev_odd = current_odd;
    35. }
    36. t = t->next;
    37. }
    38. *L = head_even;
    39. return head_odd;
    40. }

    6-54 单链表结点删除 

    hhhhh,仿照上面的自己敲出来了红红火火恍恍惚惚哈哈哈(已疯 

    1. struct ListNode *readlist(){
    2. struct ListNode *head = NULL;
    3. struct ListNode *prev, *current;
    4. int n;
    5. while(scanf("%d", &n) == 1 && n != -1){
    6. current = (struct ListNode *)malloc(sizeof(struct ListNode));
    7. if(head == NULL) head = current;
    8. else prev->next = current;
    9. current->next = NULL;
    10. current->data = n;
    11. prev = current;
    12. }
    13. return head;
    14. }
    15. struct ListNode *deletem( struct ListNode *L, int m ){
    16. struct ListNode *t = L;
    17. struct ListNode *head = NULL;
    18. struct ListNode *current, *prev;
    19. while(t){
    20. if(t->data != m){
    21. current = (struct ListNode *)malloc(sizeof(struct ListNode));
    22. if(head == NULL) head = current;
    23. else prev->next = current;
    24. current->next = NULL;
    25. current->data = t->data;
    26. prev = current;
    27. }
    28. t = t->next;
    29. }
    30. return head;
    31. }

    6-55 链表逆置 

    1. struct ListNode *reverse( struct ListNode *head ){
    2. struct ListNode *Head, *p, *q;
    3. p = head;
    4. Head = NULL;
    5. while(p){
    6. q = p->next;
    7. p->next = Head;
    8. Head = p;
    9. p = q;
    10. }
    11. return Head;
    12. }

    6-56 统计专业人数 

    1. int countcs( struct ListNode *head ){
    2. int cnt = 0;
    3. if(head == NULL) return cnt;
    4. while(head){
    5. char code[8];
    6. strcpy(code, head->code);
    7. if(code[1] == '0' && code[2] == '2') cnt ++;
    8. head = head->next;
    9. }
    10. return cnt;
    11. }

     6-57 删除单链表偶数节点

    这道题就是前面单链表结点删除直接复制粘贴改几个就ac了( 

    1. struct ListNode *createlist(){
    2. struct ListNode *head = NULL;
    3. struct ListNode *prev, *current;
    4. int n;
    5. while(scanf("%d", &n) == 1 && n != -1){
    6. current = (struct ListNode *)malloc(sizeof(struct ListNode));
    7. if(head == NULL) head = current;
    8. else prev->next = current;
    9. current->next = NULL;
    10. current->data = n;
    11. prev = current;
    12. }
    13. return head;
    14. }
    15. struct ListNode *deleteeven( struct ListNode *head ){
    16. struct ListNode *t = head;
    17. struct ListNode *Head = NULL;
    18. struct ListNode *current, *prev;
    19. while(t){
    20. if(t->data % 2 != 0){
    21. current = (struct ListNode *)malloc(sizeof(struct ListNode));
    22. if(Head == NULL) Head = current;
    23. else prev->next = current;
    24. current->next = NULL;
    25. current->data = t->data;
    26. prev = current;
    27. }
    28. t = t->next;
    29. }
    30. return Head;
    31. }

     7-45 高速公路超速处罚

    打%号 %%

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 40;
    6. int main(){
    7. int v, lv;
    8. cin >> v >> lv;
    9. double x = 100.0 * (v - lv) / lv;
    10. if(x < 10) cout << "OK";
    11. else if(x < 50) printf("Exceed %.lf%%. Ticket 200\n", x);
    12. else printf("Exceed %.lf%%. License Revoked\n", x);
    13. return 0;
    14. }

     7-54 猜数字游戏

    if else 过不了,要用if if if,因为后面还有一个循环外的条件不能都过??就是感觉这题还是不太会wwwww

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 40;
    6. int main(){
    7. int sn, n;
    8. cin >> sn >> n;
    9. int cnt = 0;
    10. for(int i = 1; i <= n; i ++ ){
    11. int a;
    12. cin >> a;
    13. cnt ++ ;
    14. if(a == sn && cnt == 1){
    15. cout << "Bingo!" << endl;
    16. break;
    17. }
    18. else if(a == sn && cnt <= 3 && cnt >= 1){
    19. cout << "Lucky you!" << endl;
    20. break;
    21. }
    22. else{
    23. if(a < 0){
    24. cout << "Game over" << endl;
    25. break;
    26. }
    27. if(a > sn){
    28. cout << "Too big" << endl;
    29. }
    30. if(a < sn){
    31. cout << "Too small" << endl;
    32. }
    33. if(a == sn){
    34. cout << "Good Guess!" << endl;
    35. break;
    36. }
    37. }
    38. if(a != sn && cnt == n){
    39. cout << "Game over" << endl;
    40. }
    41. }
    42. return 0;
    43. }

    7-56 高空坠球

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 40;
    6. int main(){
    7. double H, n;
    8. cin >> H >> n;
    9. if(n == 0){
    10. printf("0.0 0.0");
    11. return 0;
    12. }
    13. double sum = 0, h = H;
    14. for(int i = 1; i <= n; i ++ ){
    15. sum += h;
    16. h /= 2;
    17. sum += h;
    18. }
    19. printf("%.1lf %.1lf", sum - h, h);
    20. return 0;
    21. }

    7-57 求分数序列前N项和 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 40;
    6. int main(){
    7. int n;
    8. cin >> n;
    9. double sum = 0;
    10. double z = 2.0, m = 1.0;
    11. for(int i = 1; i <= n; i ++ ){
    12. sum += z / m;
    13. double t = z;
    14. z += m;
    15. m = t;
    16. }
    17. printf("%.2lf", sum);
    18. return 0;
    19. }

    7-58 求e的近似值 

    自定的fun函数int型过不了,要double

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 40;
    6. double fun(int n){
    7. double ans = 1;
    8. for(int i = 1; i <= n; i ++ ){
    9. ans *= i;
    10. }
    11. return ans;
    12. }
    13. int main(){
    14. int n;
    15. cin >> n;
    16. double sum = 1;
    17. for(int i = 1; i <= n; i ++ ){
    18. sum += 1.0 / fun(i);
    19. }
    20. printf("%.8lf", sum);
    21. return 0;
    22. }

    7-61 求幂级数展开的部分和 

    a啊啊啊啊啊·,还是没做出来,超时了一个点QAQ 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. //const int N = 40;
    6. int main(){
    7. double x;
    8. cin >> x;
    9. double sum = 1.0, ans = 1.0;
    10. for(int i = 1;; i ++ ){
    11. ans = (ans * x) / i;
    12. sum += ans;
    13. if(ans < 1e-5) break;
    14. }
    15. printf("%.4lf", sum);
    16. return 0;
    17. }

    7-71 交换最小值和最大值 

    交换位置的时候amax要在amin前面,不然 最小值出现最后 的测试点不能过

    分析一下,如果最小值在最后,最大值在最前面的情况下:

    eg:8 2 5 4 1

    ac的代码来分析,先找到的是和amax相等的a[0], 交换后就变成了 1 2 5 4 8

    继续循环,然后找到了和amin相等的a[0], 交换后变成了 1 2 5 4 8

    拿 最小值出现最后 的测试点不能过的代码来说:

    eg:8 2 5 4 1

    先找到的是和amax相等的a[0], 交换后变成了1 2 5 4 8

    但是这时候只能继续循环开始 i = 1 了,可是此时最小值在 i = 0 , 所以无法交换最小值到最后的位置 

    1. //最小值出现在最后的测试点过不了
    2. for(int i = 0; i < n; i ++ ){
    3. if(amin == a[i]){
    4. a[i] = a[0] ^ a[i];
    5. a[0] = a[0] ^ a[i];
    6. a[i] = a[0] ^ a[i];
    7. }
    8. if(amax == a[i]){
    9. a[n - 1] = a[n - 1] ^ a[i];
    10. a[i] = a[n - 1] ^ a[i];
    11. a[n - 1] = a[n - 1] ^ a[i];
    12. }
    13. }

     AC代码

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 10;
    6. int a[N];
    7. int main(){
    8. int n;
    9. cin >> n;
    10. for(int i = 0; i < n; i ++ ) cin >> a[i];
    11. int amin = a[0], amax = a[0];
    12. for(int i = 1; i < n; i ++ ){
    13. amin = min(amin, a[i]);
    14. amax = max(amax, a[i]);
    15. }
    16. for(int i = 0; i < n; i ++ ){
    17. if(amax == a[i]){
    18. int t = a[n - 1];
    19. a[n - 1] = amax;
    20. a[i] = t;
    21. }
    22. if(amin == a[i]){
    23. int t = a[0];
    24. a[0] = amin;
    25. a[i] = t;
    26. }
    27. }
    28. for(int i = 0; i < n; i ++ ) cout << a[i] << " ";
    29. return 0;
    30. }

    7-75 找出不是两个数组共有的元素 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 25;
    6. int a[N], b[N], c[N];
    7. int main(){
    8. int n, m, i, j;
    9. int k = 0;
    10. cin >> n;
    11. for(i = 0; i < n; i ++ ) cin >> a[i];
    12. cin >> m;
    13. for(j = 0; j < m; j ++ ) cin >> b[j];
    14. for(i = 0; i < n; i ++ ){
    15. for(j = 0; j < m; j ++ ){
    16. if(a[i] == b[j]) break;
    17. }
    18. if(j == m) c[k ++ ] = a[i];
    19. }
    20. for(i = 0; i < m; i ++ ){
    21. for(j = 0; j < n; j ++ ){
    22. if(a[j] == b[i]) break;
    23. }
    24. if(j == n) c[k ++ ] = b[i];
    25. }
    26. cout << c[0];
    27. for(i = 1; i < k; i ++ ){
    28. for(j = 0; j < i; j ++ ){
    29. if(c[i] == c[j]) break;
    30. }
    31. if(j == i) cout << " " << c[i];
    32. }
    33. return 0;
    34. }

    7-85 螺旋方阵

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 15;
    6. int a[N][N];
    7. int main(){
    8. int n;
    9. cin >> n;
    10. int row = 0, col = n - 1, num = 1;
    11. while(row <= col){
    12. for(int i = row; i <= col; i ++ ){
    13. a[row][i] = num ++ ;
    14. }
    15. for(int i = row + 1; i <= col; i ++ ){
    16. a[i][col] = num ++ ;
    17. }
    18. for(int i = col - 1; i >= row; i -- ){
    19. a[col][i] = num ++ ;
    20. }
    21. for(int i = col - 1; i >= row + 1; i -- ){
    22. a[i][row] = num ++ ;
    23. }
    24. row ++ , col -- ;
    25. }
    26. for(int i = 0; i < n; i ++ ){
    27. for(int j = 0; j < n; j ++ ){
    28. printf("%3d", a[i][j]);
    29. }
    30. puts("");
    31. }
    32. return 0;
    33. }

    7-88 统计字符出现次数 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 350;
    6. int a[N];
    7. int main(){
    8. char c;
    9. c = getchar();
    10. while(c != '\n'){
    11. a[c] ++ ;
    12. c = getchar();
    13. }
    14. c = getchar();
    15. cout << a[c];
    16. return 0;
    17. }

    7-92 字符串转换成十进制整数

     感觉就是有没有做过和看看ASCII的问题

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. typedef long long LL;
    5. const int N = 1e5 + 10;
    6. LL fun(char *str){
    7. LL x = 0;
    8. for(int i = 0; i < strlen(str); i ++ ){
    9. if(str[i] >= '0' && str[i] <= '9')
    10. x = x * 16 + str[i] - '0';
    11. else if(str[i] >= 'A' && str[i] <= 'F')
    12. x = x * 16 + str[i] - 'A' + 10;
    13. }
    14. return x;
    15. }
    16. int main(){
    17. char s[N], str[N];;
    18. cin >> s;
    19. int flag = 0, mul = 0, cnt = 0;
    20. for(int i = 0; i < strlen(s); i ++ ){
    21. s[i] = toupper(s[i]);
    22. if(flag == 0){
    23. if(s[i] == '-'){
    24. flag = 1;
    25. mul = 1;
    26. }
    27. else if((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'A' && s[i] <= 'F')){
    28. flag = 1;
    29. str[cnt ++ ] = s[i];
    30. }
    31. }
    32. else{
    33. if((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'A' && s[i] <= 'F')){
    34. str[cnt ++] = s[i];
    35. }
    36. }
    37. }
    38. LL ans = fun(str);
    39. if(mul == 1) ans *= -1;
    40. cout << ans << endl;
    41. return 0;
    42. }

    7-103 查找书籍

    一开始忘记map还有自动根据key值排序的功能了 

    1. #include
    2. #define INF 0x3f3f3f3f
    3. #define x first
    4. #define y second
    5. using namespace std;
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. map<double, string> mp;
    9. int main(){
    10. int n;
    11. cin >> n;
    12. getchar();
    13. while(n -- ){
    14. string name;
    15. double price;
    16. getline(cin, name);
    17. cin >> price;
    18. getchar();
    19. mp[price] = name;
    20. }
    21. cout << fixed << setprecision(2) << (--mp.end())->x << ", " << (--mp.end())->y << endl;
    22. cout << fixed << setprecision(2) << mp.begin()->x << ", " << mp.begin()->y << endl;
    23. return 0;
    24. }

    7-105 平面向量加法 

    一开始nt了一下,进位考虑应该考虑保留最后一位的后一位 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. int main(){
    9. double x1, x2, y1, y2;
    10. cin >> x1 >> y1 >> x2 >> y2;
    11. double x = x1 + x2;
    12. double y = y1 + y2;
    13. if(fabs(x) < 0.05) x = 0.0;
    14. if(fabs(y) < 0.05) y = 0.0;
    15. printf("(%.1lf, %.1lf)", x, y);
    16. return 0;
    17. }

     7-108 英文单词排序

    哥们觉得vector pair auto 用法需要记一下 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. typedef pair<int, int> pr;
    9. vector v;
    10. int cnt;
    11. string t[N];
    12. int main(){
    13. string s;
    14. while(cin >> s && s != "#"){
    15. v.push_back({s.size(), cnt});
    16. t[cnt ++ ] = s;
    17. }
    18. sort(v.begin(), v.end());
    19. for(auto i : v){
    20. cout << t[i.y] << " ";
    21. }
    22. return 0;
    23. }

    7-109 藏头诗

    这道题上次做还是int行,改成UTF-8把我干蒙了,触及到只是盲区了

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. int main(){
    9. string s;
    10. for(int i = 0; i < 4; i ++ ){
    11. getline(cin, s);
    12. cout << s[0] << s[1] << s[2];
    13. }
    14. return 0;
    15. }

    7-110 自动售货机 

     一开始没注意,被以前的思维带偏了,我为什么要想他一样的物品剩多少匹配多少的问题,NND

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 20;
    8. int a[N];
    9. int sum, total;
    10. int main(){
    11. int n;
    12. while(cin >> n, n != -1){
    13. total += n;
    14. }
    15. while(cin >> n, n != -1){
    16. a[n] ++ ; //编号为n的总数量
    17. if(n > 0 && n < 4) sum ++ ;
    18. else if(n > 3 && n < 6) sum += 2;
    19. else if(n > 5 && n < 9) sum += 3;
    20. else sum += 4;
    21. }
    22. if(total < sum) cout << "Insufficient money" << endl;
    23. else{
    24. printf("Total:%dyuan,change:%dyuan\n", total, total - sum);
    25. for(int i = 0; i < 11; i ++ ){
    26. if(a[i]){
    27. switch(i){
    28. case 1:printf("Table-water:%d;", a[i]);break;
    29. case 2:printf("Table-water:%d;", a[i]);break;
    30. case 3:printf("Table-water:%d;", a[i]);break;
    31. case 4:printf("Coca-Cola:%d;", a[i]);break;
    32. case 5:printf("Milk:%d;", a[i]);break;
    33. case 6:printf("Beer:%d;", a[i]);break;
    34. case 7:printf("Orange-Juice:%d;", a[i]);break;
    35. case 8:printf("Sprite:%d;", a[i]);break;
    36. case 9:printf("Oolong-Tea:%d;", a[i]);break;
    37. case 10:printf("Green-Tea:%d;", a[i]);break;
    38. }
    39. }
    40. }
    41. }
    42. return 0;
    43. }

    7-111 停车场管理 

    怪,这题真的怪,代码好像可以卡bug 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 11;
    8. int place[N], tim[N], wait[N];
    9. int waiti = -1;
    10. int main(){
    11. int n;
    12. cin >> n;
    13. while(1){
    14. char x;
    15. int carnum, cartime;
    16. cin >> x >> carnum >> cartime;
    17. if(x == 'E') break;
    18. else if(x == 'A'){
    19. int flag = 0;
    20. for(int i = 1; i <= n; i ++ ){
    21. if(place[i] == 0){
    22. flag = i;
    23. break;
    24. }
    25. }
    26. if(flag){
    27. place[flag] = carnum;
    28. cout << "car#" << carnum << " in parking space #" << flag << endl;
    29. }
    30. else{
    31. wait[++ waiti] = carnum;
    32. cout << "car#" << carnum << " waiting" << endl;
    33. }
    34. }
    35. else if(x == 'D'){
    36. int flag = 0;
    37. for(int i = 0; i <= n; i ++ ){
    38. if(place[i] == carnum){
    39. flag = i;
    40. break;
    41. }
    42. }
    43. if(!flag) cout << "the car not in park" << endl;
    44. else{
    45. cout << "car#" << carnum << " out,parking time " << tim[flag] << endl;
    46. place[flag] = 0;
    47. tim[flag] = 0;
    48. int t = 0;
    49. for(int i = flag; i <= n; i ++ ){
    50. place[i] = place[i + 1];
    51. tim[i] = tim[i + 1];
    52. if(i == n){
    53. place[i] = 0;
    54. tim[i] = 0;
    55. }
    56. t = i;
    57. }
    58. if(waiti != -1){
    59. place[t] = wait[0];
    60. for(int i = 0; i < waiti; i ++ ){
    61. wait[i] = wait[i + 1];
    62. }
    63. waiti -- ;
    64. cout << "car#" << place[t] << " in parking space #" << t << endl;
    65. }
    66. }
    67. }
    68. for(int i = 1; i <= n; i ++ ){
    69. if(place[i]) tim[i] ++ ;
    70. }
    71. }
    72. return 0;
    73. }

    7-112 值班安排 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 20;
    8. struct st{
    9. int obj1;
    10. int obj2;
    11. int num;
    12. int ret;
    13. }st[N];
    14. int judge(int *d, int n){
    15. int ret = 1;
    16. for(int i = 0; i < n; i ++ ){
    17. if(st[i].ret){
    18. if(d[st[i].obj1] != st[i].num - 1){
    19. ret = 0;
    20. break;
    21. }
    22. }
    23. else if(d[st[i].obj1] + st[i].num != d[st[i].obj2]){
    24. ret = 0;
    25. break;
    26. }
    27. }
    28. return ret;
    29. }
    30. int main(){
    31. int n;
    32. cin >> n;
    33. getchar();
    34. string s;
    35. for(int i = 0; i < n; i ++ ){
    36. getline(cin, s);
    37. st[i].obj1 = s[0] - 'A';
    38. if(s[1] == '='){
    39. st[i].ret = 1;
    40. st[i].num = s[2] - '0';
    41. }
    42. else{
    43. st[i].ret = 0;
    44. st[i].obj2 = s[2] - 'A';
    45. st[i].num = s[3] - '0';
    46. if(s[1] == '<') st[i].num *= -1;
    47. }
    48. }
    49. int day[7];
    50. int a, b, c, d, e, f, g;
    51. for(a = 0; a < 7; a ++ ){
    52. for(b = 0; b < 7; b ++ ){
    53. for(c = 0; c < 7; c ++ ){
    54. for(d = 0; d < 7; d ++ ){
    55. for(e = 0; e < 7; e ++ ){
    56. for(f = 0; f < 7; f ++ ){
    57. for(g = 0; g < 7; g ++ ){
    58. day[0] = a;
    59. day[1] = b;
    60. day[2] = c;
    61. day[3] = d;
    62. day[4] = e;
    63. day[5] = f;
    64. day[6] = g;
    65. if(judge(day, n)) goto E;
    66. }
    67. }
    68. }
    69. }
    70. }
    71. }
    72. }
    73. E:
    74. if(judge(day, n)){
    75. char Day[7];
    76. for(int i = 0; i < 7; i ++ ){
    77. Day[day[i]] = i + 'A';
    78. }
    79. Day[7] = '\0';
    80. puts(Day);
    81. }
    82. return 0;
    83. }

    7-113 完美的代价

    Impossible有两个条件:

    1.当n为奇数,如果有两个及以上不能配对的,说明不符合回文串条件

    2.当n为偶数,如果相同数字出现奇数次 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 11;
    8. int main(){
    9. int n;
    10. string s;
    11. cin >> n >> s;
    12. int ed = n - 1;
    13. int step = 0;
    14. bool flag = false;
    15. for(int i = 0; i < ed; i ++ ){
    16. for(int j = ed; j >= i; j -- ){
    17. if(i == j){
    18. if(n % 2 == 0 || flag == true){
    19. cout << "Impossible" << endl;
    20. return 0;
    21. }
    22. flag = true;
    23. step += n / 2 - i;//当前位置到中间位置需要的步数
    24. }
    25. else if(s[i] == s[j]){
    26. for(int k = j; k < ed; k ++ ){
    27. swap(s[k], s[k + 1]);
    28. step ++ ;
    29. }
    30. ed -- ;
    31. break;//重点
    32. }
    33. }
    34. }
    35. cout << step << endl;
    36. return 0;
    37. }

    L2-1 盲盒包装流水线 

    一开始只有22分,卡在了开的不够大,紫砂 ,现在st开的数据我不知道具体怎么算的 QAQ,感觉·就看样例的话就是N=可能的最大数据 / S本题样例5 就不会溢出了(

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. stack<int> st[50010];
    9. queue<int> q;
    10. int a[N];
    11. int main(){
    12. int n, s, k;
    13. cin >> n >> s;
    14. for(int i = 1; i <= n; i ++ ){
    15. int x;
    16. cin >> x;
    17. q.push(x);
    18. }
    19. int col = n / s;
    20. for(int i = 0; i < col; i ++ ){
    21. for(int j = 0; j < s; j ++ ){
    22. int x;
    23. cin >> x;
    24. st[i].push(x);
    25. }
    26. for(int j = 0; j < s; j ++ ){
    27. int x = q.front();
    28. q.pop();
    29. int y = st[i].top();
    30. st[i].pop();
    31. a[x] = y;
    32. }
    33. }
    34. cin >> k;
    35. while(k -- ){
    36. int x;
    37. cin >> x;
    38. if(a[x] == 0) cout << "Wrong Number" << endl;
    39. else cout << a[x] << endl;
    40. }
    41. return 0;
    42. }

    L2-2 点赞狂魔 

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 110;
    8. struct person{
    9. string name;
    10. set tag;
    11. double ave;
    12. }p[N];
    13. bool cmp(person x, person y){
    14. if(x.tag.size() == y.tag.size()) return x.ave < y.ave;
    15. return x.tag.size() > y.tag.size();
    16. }
    17. int main(){
    18. int n;
    19. cin >> n;
    20. for(int i = 0; i < n; i ++ ){
    21. string s;
    22. cin >> s;
    23. p[i].name = s;
    24. int k;
    25. cin >> k;
    26. for(int j = 0; j < k; j ++ ){//woc用while循环过不了
    27. LL x;
    28. cin >> x;
    29. p[i].tag.insert(x);
    30. }
    31. p[i].ave = 1.0 * k / p[i].tag.size();
    32. }
    33. sort(p, p + n, cmp);
    34. if(n == 0) cout << "- - -" << endl;
    35. else if(n == 1) cout << p[0].name << " - -" << endl;
    36. else if(n == 2) cout << p[0].name << " " << p[1].name << " -" << endl;
    37. else cout << p[0].name << " " << p[1].name << " " << p[2].name << endl;
    38. return 0;
    39. }

    L2-3 浪漫侧影

    1. #include
    2. using namespace std;
    3. #define INF 0x3f3f3f3f
    4. #define x first
    5. #define y second
    6. typedef long long LL;
    7. const int N = 1e5 + 10;
    8. int midt[25], postt[25], tree[N], level;
    9. void createTree(int *postt, int *midt, int n, int idx){
    10. if(!n) return ;
    11. int k = 0;
    12. while(postt[n - 1] != midt[k]) k ++ ;
    13. tree[idx] = midt[k];
    14. level = max(level, idx);
    15. createTree(postt, midt, k, idx * 2);
    16. createTree(postt + k, midt + k + 1, n - k - 1, idx * 2 + 1);
    17. }
    18. int main(){
    19. int n;
    20. cin >> n;
    21. for(int i = 0; i < n; i ++ ) cin >> midt[i];
    22. for(int i = 0; i < n; i ++ ) cin >> postt[i];
    23. createTree(postt, midt, n, 1);
    24. for(int i = 1; i <= 22; i ++ ){
    25. if(level < pow(2, i)){
    26. level = i;
    27. break;
    28. }
    29. }
    30. cout << "R: ";
    31. int cnt = 0, idx = 1;
    32. while(cnt < level){
    33. if(tree[idx]) cout << tree[idx];
    34. if(tree[idx * 2 + 1]) idx = idx * 2 + 1;
    35. else if(tree[idx * 2]) idx *= 2;
    36. else{
    37. idx *= 2;
    38. while(!tree[idx]) idx -- ;
    39. }
    40. cnt ++ ;
    41. if(cnt != level) cout << ' ';
    42. else cout << endl;
    43. }
    44. cout << "L: ";
    45. cnt = 0, idx = 1;
    46. while(cnt < level){
    47. if(tree[idx]) cout << tree[idx];
    48. if(tree[idx * 2]) idx *= 2;
    49. else if(tree[idx * 2 + 1]) idx = idx * 2 + 1;
    50. else{
    51. idx *= 2;
    52. while(!tree[idx]) idx ++ ;
    53. }
    54. cnt ++ ;
    55. if(cnt != level) cout << ' ';
    56. else cout << endl;
    57. }
    58. return 0;
    59. }

  • 相关阅读:
    MyBatis Plus详细教程
    基于SpringBoot的校园交友网站
    QT 面试题汇总[通俗易懂]
    zabbix中监控数据,报错返回给钉钉查看
    什么是微服务?
    浦东人大常委会副主任刘宇青一行莅临零数科技指导工作
    【C语言】预处理超级详细解析
    psutils连接虚拟机
    【装箱问题】基于Shuffled Complex Evolution (SCE) 算法解决装箱问题 (BPP)附matlab代码
    2022年高教社杯建模国赛论文写作指导
  • 原文地址:https://blog.csdn.net/weixin_63914060/article/details/126932992