美文网首页
PAT刷题感悟

PAT刷题感悟

作者: obsession_me | 来源:发表于2018-06-10 21:55 被阅读0次

PAT (Basic Level) Practice (中文)

1001

我特么就多写了个while(1)就通不过了。
这个能少还是得少啊。

#include <stdio.h>

int main(){
  int count,a;
  scanf("%d", &a);
  count = 0;
  while (1){
    count++;
    if (a%2!=0){
      a = (a*3+1)/2;
    }else{
      a /= 2;
    }
    if (a ==1){
      break;
    }
  }
  printf("%d", count);
  return 0;
}

这样在最后一步,时间上是过不去的,因此必须去掉while (1)的条件
这样写可以正确通过:

#include <stdio.h>

int main(){
  int count,a;
  scanf("%d", &a);
  count = 0;
  while (a!=1){
    count++;
    if (a%2!=0){
      a = (a*3+1)/2;
    }else{
      a /= 2;
    }
  }
  printf("%d", count);
  return 0;
}

1002

这道题对于我来说最大的坑无非就在于C语言操作数组特别是字符串数组非常的不熟悉。这里总结下几个遇到坑爹的地方:

  1. 怎么使数字(int)转换成字符串(char *),这里使用sprintf()来解决这个问题。
  2. 便是定义那个字符串数组了,那个数组的话需要定义称为二维的数组,为char a[][5]这种形式的,这里的话,对数组操作不熟悉是一个最大的短板。
#include <stdio.h>
#include <string.h>

int main(){
    int sum, len;
    char a[100];
    scanf("%s", &a);
    // get the sum
    len = strlen(a);
    sum = 0;
    for (int i=0; i<len;++i){
        sum += a[i] - '0';
    }
    char zhongwen[][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
    // transform sum to string
    char temp[5];
    sprintf(temp, "%d", sum);
    // printf("the sum is %s", temp);
    len = strlen(temp);
    int index = 0;
    for (int j=0; j<len; ++j){
        index = temp[j] - '0';
        if (j == len-1){
            printf("%s", zhongwen[index]);
        }else{printf("%s ", zhongwen[index]);}
    }
}

值得注意的是,这道题的要求是400ms以内,而这个程序在2ms内就已经完成了,所以这道题和上面那道要求2ms完成的不是一个侧重点。这个需要多加思索。

1004

#include <stdio.h>
#include <string.h>

typedef struct student{
  char name[10];
  char student_num[10];
  int grade;
}student;

int main(){
  // input the n student
  int n;
  scanf("%d", &n);
  student instance[n];  // array
  // input the detail
  char name[10];
  char student_num[10];
  int grade;
  for (int i=0; i<n; ++i){
    scanf("%s %s %d", &name, &student_num, &grade);
    strcpy(instance[i].name, name);
    strcpy(instance[i].student_num, student_num);
    instance[i].grade = grade;
  }
  // find the highest and lowest
  student best = instance[0];
  student worst = best;
  for (int j=1; j<n; ++j){
    if (instance[j].grade > best.grade){
      best = instance[j];
    }else if(instance[j].grade < worst.grade){
      worst = instance[j];
    }
  }
  printf("%s %s\n%s %s", best.name, best.student_num, worst.name, worst.student_num);
  return 0;
}

这道题开始出现的问题在于我在对字符串赋值的时候使用了=,然后出现了报错,因此,需要使用string.h中的strcpy()函数。

PAT (Advanced Level)

1001

#include <stdio.h>
#include <string.h>

int main(){
  long x,y;
  scanf("%d %d", &x, &y);
  // already get the two input num;
  long sum = x+y;
  char temp[7];
  sprintf(temp, "%d", sum);
  int len = strlen(temp);
  for (int i=0; i<len; ++i){
      printf("%c", temp[i]);
      if (temp[i] == '-') continue;
      if ((i+1)%3==len%3 && i != len-1) printf(",");  // 这段代码写的很有意思,可以多考虑一下
  }
  return 0;
}

相关文章

  • PAT刷题感悟

    PAT (Basic Level) Practice (中文) 1001 我特么就多写了个while(1)就通不过...

  • 简明刷题指南

    1. 刷题网站 HDUOJ Welcome To PKU JudgeOnline PAT Codeforces A...

  • PAT刷题技巧

    PAT 是什么 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一...

  • PAT刷题榜帮助

    PAT题目集 所有代码采用C++编译器通过(现在又加入了Python 2.x) 本人没有系统学习过C++,只有C的...

  • PAT乙级刷题总结

    开始先说最重要心得体会: 写代码前,先在纸上写写画画,写下伪码,理清思路,别上来就敲代码,效率极低还易出现bug。...

  • PAT初级刷题思路汇总

    1036? 输入时忽略某字符: cin.ignore() 或者直接用scanf 1021 个位数统计 用一个计数器...

  • 刷题感悟

    第17章,49道题目错了16道,只听了一遍录播,直播后书没看没复习。靠的是这一个月以来反复的接触,加强做题的理解。...

  • PAT A1001 A+B Format (20)

    PAT A1001 A+B Format 原题链接PAT甲级题目目录(简书)PAT甲级题目目录(CSDN)在CSD...

  • 刷题日记 | PAT (Basic Level) Practic

    从今天开始刷PAT,每天两道(至少<_<)??coding原则:自顶向下->逐步细化->模块化设计->结构化编码 ...

  • PAT 甲级 刷题日记|A 1122 Hamiltonian

    单词积累 vertex 顶点 Hamilton cycle problem 哈密顿问题 题目 The "Hamil...

网友评论

      本文标题:PAT刷题感悟

      本文链接:https://www.haomeiwen.com/subject/mbvqeftx.html