PAT-A Java实现

1001 A+B Format (20)

输入:两个数a,b,-1000000 <= a, b <= 1000000

输出:a+b,并以每3个用逗号隔开的形式展示。

思路一:

1)计算出a+b的值,赋给sum。判断sum<0,则先输出一个“”-”号,并将sum=-sum(转换为正值);判断sum==0时,则输出0;

2)然后将sum存到一个数组num[10]中,将sum数值的低位存到数组的低位(个位存在数组第1位),用一个while循环:num[i]=sum%10,sum=sum/10;i++;

3)将数组num[]从高位到低位进行输出,每逢3位输出逗号,即i%3==0.(注意,输出最后一位后不加逗号)

 1     int a,b;
2 int sum,i=0;//存放a+b的值
3 int num[10];
4 scanf("%d %d",&a,&b);
5
6 sum=a+b;
7 if(sum<0){
8 printf("-");
9 sum=-sum;}
10 else if(sum==0){
11 printf("0");}
12 while(sum>0){
13 num[i]=sum%10;
14 sum=sum/10;
15 i++;}
16 int j=0;
17 for(j=i-1;j>=0;j--){
18 printf("%d",num[j]);
19 if(j%3==0&&j!=0){
20 printf(",");}}

思路二:

1)计算出a+b的值,赋给sum。判断sum<0,则先输出一个“”-”号,并将sum=-sum(转换为正值);

2)判断sum>=1000000,输出printf("%d,%03d,%03d",sum/1000000,sum%1000000/1000,sum%1000);

sum>=1000,输出printf("%d,%03d",sum/1000,sum%1000);

其他情况,输出sum

    int a,b;
int sum;//存放a+b的值
scanf("%d %d",&a,&b);
sum=a+b;
if(sum<){
printf("-");
sum=-sum;}
if(sum>=){
printf("%d,%03d,%03d",sum/,sum%/,sum%);
}
else if(sum>=) {
printf("%d,%03d",sum/,sum%);
}
else{
printf("%d",sum);
}

思路三:

1)计算出a+b的值,赋给sum。判断sum<0,则先输出一个“”-”号,并将sum=-sum(转换为正值);

2)将sum转换成字符形式,判断sum>=1000000,在倒数第7位插入逗号,并在倒数第3位插入逗号;

sum>=1000,在倒数第3位插入逗号;其他情况,不作处理;

最后将处理后的字符进行输出

1005 Spell It Right (20 分)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
String N = input.next();
char[] n = N.toCharArray();
int sum =0,numLen=0;
int digit[] = new int[10];
for(int i=0;i<n.length;i++){
sum += n[i] - '0';
}
if(sum==0){
System.out.println(num[0]);
}else{
while(sum!=0){
digit[numLen++] = sum%10; //从低位到高位将每位存于digit中
sum /= 10;
}
}
for(int i=numLen-1;i>=0;i--){
System.out.print(num[digit[i]]);
if(i>0){
System.out.print(" ");
}
}
}
}

  

1008 Elevator (20 分)

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int time = 0;
int before = 0;
for(int i=0;i<N;i++){
int now = input.nextInt();
if(now > before){
time += (now - before)*6;
}else {
time += (before - now)*4;
}
time+=5;
before=now;
}
System.out.println(time);
}
}

  

1009 Product of Polynomials

注意:有多个类,要提交OJ时, 可以将多个类写入一个文件。但只有Main类使用public修饰。

import java.text.DecimalFormat;
import java.util.Scanner;
class Poly {
public int exp;
public double cof;
}
public class Main {
//1009 Product of Polynomials
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Poly[] poly = new Poly[1001];
double[] ans = new double[2001];
int n = input.nextInt();
for(int i=0;i<n;i++){
poly[i]= new Poly();
poly[i].exp = input.nextInt();
poly[i].cof = input.nextDouble();
}
int m = input.nextInt();
for(int i=0;i<m;i++){
int exp = input.nextInt();
double cof = input.nextDouble();
for(int j=0;j<n;j++){
ans[exp+poly[j].exp] += cof*poly[j].cof;
}
}
int num=0;
for(int i=0;i<2001;i++){
if(ans[i]!=0.0){
num++;
}
}
System.out.print(num);
DecimalFormat format = new DecimalFormat("0.0");
for(int i=2000;i>=0;i--){
if(ans[i]!=0.0){
System.out.print(" "+i+" "+format.format(ans[i]));
}
}
} }

  

1011 World Cup Betting (20 分)
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] game = {"W","T","L"};
double profit=1;
double[] a = new double[3];
for(int i=0;i<3;i++){
a[0] = input.nextDouble();
a[1] = input.nextDouble();
a[2] = input.nextDouble();
int j=max(a);
profit *= a[j];
System.out.print(game[j]+" ");
}
profit = (profit*0.65-1)*2;
DecimalFormat df = new DecimalFormat("0.00");
System.out.print(df.format(profit));
}
public static int max(double a[]){
double max=a[0];
int maxi=0;
for(int i=1;i<a.length;i++){
if(a[i]>max){
max=a[i];
maxi=i;
}
}
return maxi;
}
}

  

1042 Shuffling Machine

import java.util.Scanner;
public class Main {
//Shuffling Machine
public static void main(String[] args){
int N=54;
char[] mp={'S','H','C','D','J'};//牌的编号与花色的关系
int[] start = new int[N+1];
int[] next = new int[N+1];
int[] end = new int[N+1];
for(int i=1;i<=N;i++){
start[i]=i; //初始化牌的编号
}
Scanner input = new Scanner(System.in);
int K = input.nextInt(); //输入变换的次数
for(int i=1;i<=N;i++){
next[i]=input.nextInt(); //输入每个位置上的牌在操作后的位置
} for(int step=0;step<K;step++){ //执行K次操作
for(int i=1;i<=N;i++){
end[next[i]]=start[i]; //把第i个位置的牌的编号存于位置next[i];
}
for(int i=1;i<=N;i++){
start[i]=end[i]; //把end数组赋值给start数组,以供下次操作使用
}
} for(int i=1;i<=N;i++){
if(i!=1){
System.out.print(" ");
}
System.out.print(mp[(start[i]-1)/13]); //输出花色
System.out.print((start[i]-1)%13+1); //输出编号
} }
}

  

1065 A+B and C (64bit) (20 分)
import java.util.Scanner;
public class Main {
//1065 A+B and C (64bit)
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int T = input.nextInt();
for(int i=1;i<=T;i++){
long A = input.nextLong();
long B = input.nextLong();
long C = input.nextLong();
boolean flag;
long res = A+B;
if(A>0&&B>0&&res<0) //正溢出
flag=true;
else if(A<0&&B<0&&res>=0) //负溢出
flag=false;
else if(res>C) //无溢出时,A+B>C时为true;
flag=true;
else //无溢出时,A+B<=C时为false;
flag=false;
System.out.println("Case #"+i+": "+flag);
}
}
}

 

 

  

上一篇:Android布局优化:include 、merge、ViewStub的详细总结


下一篇:[Android]Android布局优化之