java oj

java a+b Problem

题目描述

对于给定的两个整数a和b,求它们的和。

输入描述:

a b

输出描述:

a,b之和


Sample

input:

555 445

output:

1000

    import java.util.Scanner;

    public class Main {
     public static void main(String[] arg) {
      Scanner sc=new Scanner(System.in);
       while(sc.hasNextInt()) {
          int a=sc.nextInt();
          int b=sc.nextInt();
          System.out.println(a+b);
      }
     }
    }

打印沙漏

题目描述

写程序把给定数量的符号打印成最大沙漏的形状,并输出剩余的符号个数。例如给定17个“*”,要求按下列格式打印  


*****
\***/
||*||
/***\
*****

“沙漏形状”,是指每行输出奇数个输入的特定符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。分别使用\和/填补两侧。特定符号个数为1时,使用'|'填补两侧。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。

输出格式:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数

输入 38 *

输出

******* 
\*****/ 
\\***// 
|||*||| 
//***\\ 
/*****\ 
*******
7

import java.util.Scanner;
public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            char c = sc.next().charAt(0);sc.close();

            int m = (int) Math.sqrt((n + 1) / 2);
            for (int i = 0; i < 2 * m - 1; i++) {
                if(i>(2*m-1)/2)
                {
                    for (int j = 0; j < 2 * m - 1; j++)
                    {
                        if (i > j && i + j < 2 * m - 2)
                        { System.out.print("/"); }
                        else if(i < j && i + j > 2 * m - 2)
                        { System.out.print("\\");}
                        else
                        {System.out.print(c);}
                    }
                }
                else if(i<(2*m-1)/2)
                {
                    for (int j = 0; j < 2 * m - 1; j++)
                    {
                        if (i > j && i + j < 2 * m - 2)
                        { System.out.print("\\"); }
                        else if(i < j && i + j > 2 * m - 2)
                        { System.out.print("/");}
                        else
                        {System.out.print(c);}
                    }
                }
                else
                {
                    for (int j = 0; j < 2 * m - 1; j++)
                    {
                        if (i > j && i + j < 2 * m - 2)
                        { System.out.print("|"); }
                        else if(i < j && i + j > 2 * m - 2)
                        { System.out.print("|");}
                        else
                        {System.out.print(c);}
                    }
                }


                System.out.println();
            }

            System.out.println(n - 2 * m * m + 1);
        }
    }




old bill

题目描述

Among grandfather's papers a bill was found.

    72 turkeys $_679_

    The first and the last digits of the number that obviously represented the total price of those turkeys are replaced here by blanks (denoted _), for they are faded and are illegible. What are the two faded digits and what was the price of one turkey?

    We want to write a program that solves a general version of the above problem.

    N turkeys $_XYZ_

    The total number of turkeys, N, is between 1 and 99, including both. The total price originally consisted of five digits, but we can see only the three digits in the middle. We assume that the first digit is nonzero, that the price of one turkeys is an integer number of dollars, and that all the

turkeys cost the same price.

    Given N, X, Y, and Z, write a program that guesses the two faded digits and the original price. In case that there is more than one candidate for the original price, the output should be the most expensive one. That is, the program is to report the two faded digits and the maximum price per turkey for the turkeys.

输入:    The first line of the input file contains an integer N (0<N<100), which represents the number of turkeys. In the following line, there are the three decimal digits X, Y, and Z., separated by a space, of the original price $_XYZ_.

输出:    For each case, output the two faded digits and the maximum price per turkey for the turkeys. If there is no the integer price, output  0 directly。
// OldBill问题JAVA实现
import java.awt.print.Printable;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			int turkeynum;
			int x,y,z;
			turkeynum = in.nextInt();
			x = in.nextInt();
			y = in.nextInt();
			z = in.nextInt();
			int temp0prise = x*100 + y*10 + z;
			loop1:{
				for (int i = 9; i > 0 ; i--) {
					int temp1prise = temp0prise + i*1000;
					loop2:
						for (int j = 9; j >= 0; j--) {
							int temp2prise = j + temp1prise*10;
							if (temp2prise%turkeynum == 0) {
								int priseperturkey = temp2prise/turkeynum;
								System.out.println(i + " " + j + " " + priseperturkey);
								break loop1;
							}
						}
				}
				System.out.println(0);
			}
		}
	}

}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值