1007 C基础-计负均正

import java.util.*;
import java.io.*; public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x;
int negativeCnt=0,positiveSum=0,zeroCnt=0;
for(x=1;x<=20;++x)
{
int n=sc.nextInt();
if(n<0) negativeCnt++;
else if(n==0) zeroCnt++;
else positiveSum+=n;
}
System.out.printf("%d\n%.2f",negativeCnt,positiveSum/(double)(20-negativeCnt-zeroCnt));
}
}

1008 C基础-公约公倍

注意一下题干有问题,是空行不是空格。

 import java.util.*;
import java.io.*;
import java.math.BigInteger; public class Main
{
static int gcd(int x,int y)
{
if(y==0) return x;
else return gcd(y,x%y);
}
static int lcm(int x, int y)
{
return x/gcd(x,y)*y;
}
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt(),m=cin.nextInt();
System.out.println(String.valueOf(gcd(n,m))+"\n"+String.valueOf(lcm(n,m)));
}
}

1009 C基础-统计字符

import java.util.*;
import java.io.*; public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String inputStr=sc.nextLine();
int ch=0,sp=0,num=0,other=0;
for(int i=0;i!=inputStr.length();++i)
{
if(Character.isDigit(inputStr.charAt(i)))
num++;
else if(Character.isLetter(inputStr.charAt(i)))
ch++;
else if(inputStr.charAt(i)==' ') sp++;
else other++;
}
System.out.printf("%d\n%d\n%d\n%d\n",ch,sp,num,other);
}
}

1010 C基础-阶乘数列

import java.util.*;
import java.io.*;
import java.math.BigInteger; public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
BigInteger x=BigInteger.valueOf(0);
for(int i=1;i<=30;++i)
{
BigInteger tmp=BigInteger.valueOf(1);
for(int j=1;j<=i;++j)
{
tmp=tmp.multiply(BigInteger.valueOf(j));
//System.out.println(tmp.toString()+" "+String.valueOf(j));
}
//System.out.println(tmp);
x=x.add(tmp);
}
System.out.printf("%.2e",x.doubleValue());
}
}

1011 C基础-进制转换

import java.util.*;
import java.io.*;
import java.math.BigInteger; public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
System.out.println(Integer.toOctalString(x));
}
}

1012 SZ斐波拉契数列

保存计算结果以加快运算速度。注意每次清空。

import java.util.*;
import java.io.*;
import java.math.BigInteger; public class Main
{
static long[] arr=new long[35];
static int a,b;
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
while(T--!=0)
{
for(int i=0;i<=30;++i) arr[i]=0;
a=sc.nextInt();
b=sc.nextInt();
int n=sc.nextInt();
System.out.println(f(n));
}
} public static long f(int x)
{
//System.out.println(x);
if(arr[x]!=0) return arr[x];
else
{
if(x==1) arr[x]=a;
else if(x==2) arr[x]=b;
else if(x%2==1) arr[x]=f(x-1)+f(x-2);
else arr[x]=f(x-1)+f(x-2)+f(x-3);
return arr[x];
}
}
}
05-19 19:50