PTA_2022实验二
填空
# 1.
System.in
a[i].length
s=0
max=a[i][j]
a[i][j]
# 2
int n=sc.nextInt();
public static double fun(int m)
for(i=2;i<=m;i++)t=t-1.0/(i*i);
return t;
# 3
str.toCharArray();
i<x.length()
ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'
ch>='1'&&ch<='9'
c++
# 4
float s=0,t=1;
t=1
t=t*j;
s=s+t
# 5
int n=sc.nextInt();
System.out.println(fun(n));
int k=1
num/=10
函数题
2-1
public static int numofHan(String data[])
{
int cnt=0;
for(int i=0;i<data.length;i++)
{
if(data[i].indexOf("汉族")>=0) cnt++;
}
return cnt;
}
编程题
2-1
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int tt=in.nextInt();
int[] a=new int[tt];
int[] b=new int[tt];
for(int i=0;i<tt;i++)
a[i]=in.nextInt();
for(int i=0;i<tt;i++)
{
for(int j=0;j<tt;j++)
{
if(a[i]==a[j])b[i]++;
}
}
int maxv=0,idx=0;
for(int i=0;i<tt;i++)
{
if(b[i]>maxv)
{
idx=i;
maxv=b[i];
}
}
System.out.println(a[idx]+" "+maxv);
}
}
2-2
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = 0;
int q = 0, w = 0, sum = 0, index = 0;
n = sc.nextInt();
do
{
if(n>0) q++;
else if(n<0) w++;
index++;
sum+=n;
n = sc.nextInt();
} while(n!=0);
System.out.println(q);
System.out.println(w);
System.out.println(sum);
System.out.println((float)sum/index);
}
}
2-3
import java.math.BigDecimal;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
while (input.hasNext())
{
BigDecimal a=new BigDecimal(input.next().toString());
BigDecimal b=new BigDecimal(input.next().toString());
System.out.println(a.add(b));
System.out.println(a.multiply(b));
}
}
}
2-4
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int m=input.nextInt();
System.out.println(Math.max(n,m));
}
}
2-5![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d1ee47b47ca309e87bba1dde62a2d6c8.png)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
int ou=0,ji=0;
cin>>n;
for(int x=1;x<=n;x++)
{
if(x%2==1&&x%3==0)ji++;
if(x%2==0&&x%3==0)ou++;
}
cout<<ji<<","<<ou<<endl;
return 0;
}