PAT-Basic-1095

题目

PAT 准考证号由 4 部分组成:

  • 第 1 位是级别,即 T 代表顶级;A 代表甲级;B 代表乙级;

  • 第 2~4 位是考场编号,范围从 101 到 999;

  • 第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位;

  • 最后 11~13 位是考生编号,范围从 000 到 999。

现给定一系列考生的准考证号和他们的成绩,请你按照要求输出各种统计信息。

输入格式:

输入首先在一行中给出两个正整数 N(≤104)和 M(≤100),分别为考生人数和统计要求的个数。

接下来 N 行,每行给出一个考生的准考证号和其分数(在区间 [0,100] 内的整数),其间以空格分隔。

阅读更多

PAT-Basic-1082

题目

本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟。我们假设靶心在原点(0,0)。

输入格式:

输入在第一行中给出一个正整数 N(≤ 10 000)。随后 N 行,每行按下列格式给出:

1
ID x y

其中 ID 是运动员的编号(由 4 位数字组成);xy 是其打出的弹洞的平面坐标(x,y),均为整数,且 0 ≤ |x|, |y| ≤ 100。题目保证每个运动员的编号不重复,且每人只打 1 枪。

阅读更多

PTA-Advance-1002

PROBLEM

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N1 aN1 N2 aN2 … NK aNK

where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK<⋯<N2<N1≤1000.

阅读更多

Kotlin学习笔记——BroadCast

1
#define 小毛驴 xml

收发广播

使用场景:Fragment想要向外传递信息

  1. 在Fragment中发送广播
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    class BlankFragment : Fragment() {
    var ctx:Context? = null
    var mPosition:Int = 0
    var mInageId:Int = 0
    var mDesc:String = ""
    var title:String = ""

    val colorNames = listOf<String>("红色","黄色","绿色","青色","蓝色")
    val colors = intArrayOf(Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE)
    var mSeq:Int = 0
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    ctx = activity
    if (arguments != null) {
    mPosition = arguments!!.getInt("position", 0)
    mInageId = arguments!!.getInt("image_id", 0)
    mDesc = arguments!!.getString("desc")
    title = arguments!!.getString("title")
    }
    val view = inflater.inflate(R.layout.show_info, container, false)

    view.findViewById<ImageView>(R.id.imageView).setImageResource(mInageId)
    view.findViewById<TextView>(R.id.textView).text = mDesc
    view.findViewById<Button>(R.id.se).setOnClickListener {
    ctx!!.selector("选择颜色", colorNames) {
    mSeq = it

    val intent = Intent(BlankFragment.EVENT)
    intent.putExtra("seq", it)
    intent.putExtra("color", colors[it])
    ctx!!.sendBroadcast(intent)//发送广播
    }
    }
    return view
    }

    companion object {
    const val EVENT:String = "changeColor"//const,编译期常量
    fun newInstance(position:Int, image_id:Int, desc:String, title:String) : BlankFragment {

    val fragment = BlankFragment()
    val bundle = Bundle()
    bundle.putInt("position", position)
    bundle.putInt("image_id", image_id)
    bundle.putString("desc", desc)
    bundle.putString("title", title)
    fragment.arguments = bundle
    return fragment
    }
    }
    }
  2. 在要接收广播的页面注册receiver
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    class MainActivity : FragmentActivity(){
    private var BGChangeRecever:myBgChangeRecever? = null
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var vp:ViewPager = findViewById(R.id.vp)
    var title: PagerTabStrip = findViewById(R.id.title)

    val pics = arrayOf(R.mipmap.basic, R.mipmap.close, R.mipmap.debug, R.mipmap.edit)
    val list:MutableList<itemInfo> = mutableListOf()
    for (i in pics.indices) {
    list.add(itemInfo((i+1).toString(), pics[i], ((i+1)*(i+1)).toString()))
    }
    vp.adapter = infoPagerAdapter(supportFragmentManager, list)
    vp.currentItem = 0
    }

    public override fun onStart() {
    super.onStart()
    BGChangeRecever = myBgChangeRecever()

    val filiter = IntentFilter(BlankFragment.EVENT)//广播过滤器,过滤掉参数以外的广播
    registerReceiver(BGChangeRecever,filiter)//开始时注册接收器

    }

    public override fun onStop() {
    unregisterReceiver(BGChangeRecever)//结束前注销接收器
    super.onStop()
    }

    private inner class myBgChangeRecever : BroadcastReceiver() {//广播接收器
    override fun onReceive(context: Context?, intent: Intent?) {//接收广播后执行的操作
    if (intent != null) {
    val color = intent.getIntExtra("color", Color.GREEN)
    textView2.setTextColor(color)
    }
    }

    }
    }

接收系统广播

静态注册

阅读更多

Kotlin学习笔记——Button

实现短按长按的方法

调用函数

方法 参数 参数解释 返回值 备注
setOnClickListener lambda表达式 lambda的参数为发生点击动作的View,返回值Unit Unit 相当于override fun onClickListener(v:View)
setOnLongClickListener lambda表达式 lambda的参数为发生点击动作的View,返回值Boolean(true表示这个事件已经消耗完了,false表示事件继续传递,会触发一次短按事件) Unit 相当于override fun onLongClick(v:View):Boolean

例子

1
2
3
4
5
6
7
btn.setOnClickListener {
toast("click")
}
btn.setOnLongClickListener {
toast("Long Click")
true
}

使用内部类

阅读更多

Kotlin学习笔记——Fragment

1
#define 小毛驴 xml

使用方法

Fragment与ViewPager搭配,实现翻页,实现每页多个控件

  1. 写好每个item的小毛驴文件和数据传送类
  2. 继承Fragment类,自定义一个fragment
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    class BlankFragment : Fragment() {
    var ctx:Context? = null
    var mPosition:Int = 0
    var mInageId:Int = 0
    var mDesc:String = ""
    var title:String = ""

    val colorNames = listOf<String>("红色","黄色","绿色","青色","蓝色")
    val colors = intArrayOf(Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE)
    var mSeq:Int = 0
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    ctx = activity
    if (arguments != null) {
    mPosition = arguments!!.getInt("position", 0)
    mInageId = arguments!!.getInt("image_id", 0)
    mDesc = arguments!!.getString("desc")
    title = arguments!!.getString("title")
    }//获取数据
    val view = inflater.inflate(R.layout.show_info, container, false)

    view.findViewById<ImageView>(R.id.imageView).setImageResource(mInageId)
    view.findViewById<TextView>(R.id.textView).text = mDesc
    //显示数据
    return view
    }

    companion object {
    fun newInstance(position:Int, image_id:Int, desc:String, title:String) : BlankFragment {//调用这个函数,创建新的fragment

    val fragment = BlankFragment()
    val bundle = Bundle()
    bundle.putInt("position", position)
    bundle.putInt("image_id", image_id)
    bundle.putString("desc", desc)
    bundle.putString("title", title)
    fragment.arguments = bundle
    return fragment
    }
    }
    }
  3. ViewPager的适配器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class infoPagerAdapter(val fragManger: FragmentManager, val itemList:MutableList<itemInfo>) : FragmentStatePagerAdapter(fragManger) {
    override fun getCount(): Int = itemList.size
    override fun getItem(p0: Int): Fragment {
    val item = itemList[p0]
    return BlankFragment.newInstance(p0, item.pic, item.desc, item.name)
    }

    override fun getPageTitle(position: Int): CharSequence? {
    return itemList[position].name
    }
    }
    4.给ViewPager添加适配器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    class MainActivity : FragmentActivity(){
    //这个时候,继承的是FragmentActivity
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var vp:ViewPager = findViewById(R.id.vp)
    var title: PagerTabStrip = findViewById(R.id.title)
    val list:MutableList<itemInfo> = mutableListOf()
    //省略中间给list赋值的过程
    vp.adapter = infoPagerAdapter(supportFragmentManager, list)
    vp.currentItem = 0
    }
    }

Kotlin学习笔记——GridView

Kotlin学习笔记——GridView

1
#define 小毛驴 xml

使用方法

  1. 设计好界面
  2. 新建一个小毛驴文件,这个小毛驴文件是GridView中,每一个Item的界面布局文件
  3. (可选)编写一个数据类,用来保存每个item中的数据,用data class可以很方便
  4. 编写一个继承BaseAdapter适配器的类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    class GridAdapter(private val context: Context, private val strList:MutableList<myItems>, private val background:Int) : BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var view = convertView
    val holder:ViewHolder
    if (convertView == null) {
    view = LayoutInflater.from(context).inflate(R.layout.item, null)
    //我猜这个函数的作用是指定这个类所对应的小毛驴文件
    holder = ViewHolder()
    holder.myLayout = view.findViewById<LinearLayout>(R.id.all)
    holder.desc = view.findViewById<TextView>(R.id.textView)
    holder.pic = view.findViewById<ImageView>(R.id.imageView)
    view.tag = holder
    } else {
    holder = (view?.tag) as ViewHolder
    }
    //以上是固定格式
    val myItem = strList[position]
    //传进来的数据数组,适配器根据数组大小反复调用这个函数构造ViewList
    //position是当前位置,对应数组下标
    //holder.myLayout.setBackgroundColor(background)
    holder.desc.text = myItem.desc
    holder.pic.setImageResource(myItem.image)
    //以上是自定义每个控件的显示内容
    return view!!
    }

    override fun getItem(position: Int): Any = strList[position]
    override fun getItemId(position: Int): Long = position.toLong()
    override fun getCount(): Int = strList.size

    inner class ViewHolder {
    lateinit var myLayout:LinearLayout
    lateinit var desc: TextView
    lateinit var pic: ImageView
    }
    }
  5. 如果编写了数据类(起了一个c++结构体的作用,因为数组只能传递一个),创建对应的List并且赋值
  6. 给GridView添加适配器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    var grid:GridView = findViewById(R.id.panel)
    var pics = arrayOf(R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e, R.mipmap.f, R.mipmap.g, R.mipmap.h)
    var descs = arrayOf("超级大帅哥刘甜甜", "刘甜甜最喜欢的大明星周周", "刘甜甜最喜欢的性感裸男", "刘甜甜最想养的橘猫", "还是超级大帅哥刘甜甜", "刘甜甜最喜欢的动画人物米奇", "还是刘甜甜最喜欢的动画人物米奇", "用来凑数的发际线哥")
    var data:MutableList<myItems> = mutableListOf()
    for (i in pics.indices) {
    data.add(myItems(descs[i], pics[i]))
    }
    grid.adapter = GridAdapter(this, data, Color.GRAY)
    grid.numColumns = 2//设置列数

Kotlin学习笔记——ListView

1
#define 小毛驴 xml

使用方法

  1. 设计好界面
  2. 新建一个小毛驴文件,这个小毛驴文件是ListView中,每一个Item的界面布局文件
  3. (可选)编写一个数据类,用来保存每个item中的数据,用data class可以很方便
  4. 编写一个继承BaseAdapter适配器的类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    class ListViewAdapter(private val context: Context, private val strList:MutableList<myItems>, private val background:Int) : BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var view = convertView
    val holder:ViewHolder
    if (convertView == null) {
    view = LayoutInflater.from(context).inflate(R.layout.item, null)
    //我猜这个函数的作用是指定这个类所对应的小毛驴文件
    holder = ViewHolder()
    holder.ll_item = view.findViewById<LinearLayout>(R.id.ll_item)
    holder.iv_icon = view.findViewById<ImageView>(R.id.iv_icon)
    holder.tv_name = view.findViewById<TextView>(R.id.tv_name)
    holder.tv_desc = view.findViewById<TextView>(R.id.tv_desc)
    view.tag = holder
    } else {
    holder = (view?.tag) as ViewHolder
    }
    //以上是固定格式
    val myItem = strList[position]
    //传进来的数据数组,适配器根据数组大小反复调用这个函数构造ViewList
    //position是当前位置,对应数组下标
    holder.ll_item.setBackgroundColor(background)
    holder.iv_icon.setImageResource(myItem.image)
    holder.tv_name.text = myItem.name
    holder.tv_desc.text = myItem.desc
    //以上是自定义每个控件的显示内容,根据之前传进来的List里面的数据
    return view!!
    }

    override fun getItem(position: Int): Any = strList[position]
    override fun getItemId(position: Int): Long = position.toLong()
    override fun getCount(): Int = strList.size

    inner class ViewHolder {
    lateinit var ll_item:LinearLayout
    lateinit var iv_icon:ImageView
    lateinit var tv_name:TextView
    lateinit var tv_desc:TextView
    }
    }
  5. 如果编写了数据类(起了一个c++结构体的作用,因为数组只能传递一个),创建对应的List并且赋值
  6. 给ListView添加适配器
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    var item:MutableList<myItems> = mutableListOf()
    val imageIds = arrayOf(R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e)
    var name = arrayOf("超级大帅哥刘甜甜", "还是超级大帅哥刘甜甜", "可爱的橘猫", "性感裸男","周周")
    var desc = arrayOf("是他是他就是他,我们的大帅哥,刘天天", "是他是他还是他,我们的大帅哥,刘天天", "刘天天最想养的橘猫", "刘天天最喜欢的性感裸男","刘天天最喜欢的大明星周周")
    //各种数据
    setContentView(R.layout.activity_clickhere)
    for (i in imageIds.indices) {
    item.add(myItems(name[i], desc[i], imageIds[i]))
    }
    //初始化要传递的List

    var list:ListView = findViewById<ListView>(R.id.list)
    list.adapter = ListViewAdapter(this,item ,Color.WHITE)//你刚才自己写的适配器类
    //为ListView添加适配器

Kotlin学习笔记——EditText

输入监听器

方便起见,在activity的内部写一个内部类,用来监听输入

编写监听器

1
2
3
4
5
6
7
8
9
10
11
inner class EditWatcher : TextWatcher {
override fun afterTextChanged(s:Editable) {

}
override fun beforeTextChanged(s:CharSequence, start:Int, count:Int, after:Int) {

}
override fun onTextChanged(s:CharSequence, start:Int, count:Int, after:Int) {

}
}

注意

  1. 把Editable直接toString()就是用户当前的输入
阅读更多