201512-1数位之和
问题描述
给定一个十进制整数n,输出n的各位数字之和。
输入格式
输入一个整数n。
输出格式
输出一个整数,表示答案。
样例输入
20151220
样例输出
13
方法一:
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine().trim();
int sum = 0;
for(int i=0;i<string.length();i++){
sum += string.charAt(i)-'0';
}
System.out.println(sum);
}
}
方法二:
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
while (n>0){
sum += n%10;
n = n/10;
}
System.out.println(sum);
}
}
201604-3路径解析
分析正规化操作的情况:
- 把连续多个’/‘变成一个’/’;
- 以/结尾:不是根目录(/)的,要去掉结尾的’/’;
- 空字符串:处理为当前目录;
- 不包含’.‘和’…’。
- 巧妙使用split("/")分割,使得.和…的处理变得简单
public class Main {
public static void main(String[] args){
//输入
Scanner input = new Scanner(System.in);
int n = input.nextInt();//路径个数
input.nextLine();
String cur = input.nextLine();//当前目录
String[] paths = new String[n];
for(int i=0;i<n;i++){
paths[i]=input.nextLine();
}
//处理
for(int i=0;i<n;i++){
String str = paths[i];
//空字符串:处理为当前目录;
if(str.equals("")){
str=cur;
}
// if(str.matches(".*\\/+.*")){
// str = str.replaceAll("\\/+","/");
// }
//去除连续的
str=str.replaceAll("/+","/");
//不是绝对路径,在它前面加上当前目录和'/'
if (str.charAt(0)!='/'){
str = cur + '/'+str;
}
//分割字符串
String[] temp = str.split("/");
ArrayList<String > ans = new ArrayList<String>();
for(int j=1;j<temp.length;j++){
if (temp[j].equals(".")){ continue;}
else if (temp[j].equals("..")&&ans.size()>0){
ans.remove(ans.size()-1);
}else if (!temp[j].equals("..")){
ans.add(temp[j]);
}
}
System.out.print("/");
for(int j=0;j<ans.size();j++){
if (j!=0){
System.out.print("/");
}
System.out.print(ans.get(j));
}
System.out.println();
}
}
}
201803-1跳一跳
问题描述
近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱。
简化后的跳一跳规则如下:玩家每次从当前方块跳到下一个方块,如果没有跳到下一个方块上则游戏结束。
如果跳到了方块上,但没有跳到方块的中心则获得1分;跳到方块中心时,若上一次的得分为1分或这是本局游戏的第一次跳跃则此次得分为2分,否则此次得分比上一次得分多两分(即连续跳到方块中心时,总得分将+2,+4,+6,+8…)。
现在给出一个人跳一跳的全过程,请你求出他本局游戏的得分(按照题目描述的规则)。输入格式
输入包含多个数字,用空格分隔,每个数字都是1,2,0之一,1表示此次跳跃跳到了方块上但是没有跳到中心,2表示此次跳跃跳到了方块上并且跳到了方块中心,0表示此次跳跃没有跳到方块上(此时游戏结束)。
输出格式
输出一个整数,为本局游戏的得分(在本题的规则下)。
样例输入
1 1 2 2 2 1 1 2 2 0
样例输出
22
数据规模和约定
对于所有评测用例,输入的数字不超过30个,保证0正好出现一次且为最后一个数字。
思路:
- 结束条件:遇到零;
- 一分:跳到了但没到中心,也就是type为1的时候;
- 两分:上一次一分,这次跳到了中心;本局的第一次跳跃。
- 递增两分:跳到中心
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int sum = 0;
int preScore = 1;
int type = input.nextInt();
while (type!=0){
if (type == 1) {
preScore = 1;
sum += 1;
}else if (preScore == 1 && type == 2){
preScore = 2;
sum += preScore;
}else {
preScore += 2;
sum += preScore;
}
type = input.nextInt();
}
System.out.println(sum);
}
}
也可以用数组实现,但上面这种方法更优。
202006-1线性分类器
思路很简单,把点的坐标带入公式,如果类型A和B的点全部异号即可。
9 3
1 1 A
1 0 A
1 -1 A
2 2 B
2 3 B
0 1 A
3 1 B
1 3 B
2 0 A
0 2 -3
-3 0 2
-3 1 1
我自己写的不知道为啥老是65分,留下了学渣的眼泪!!!
下面是我找的满分代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt();
// 使用List存储A、B两类
List<Node> A = new ArrayList<>(), B = new ArrayList<>();
// 读入数据
for ( int i = 0; i < n; i++ ) {
Node node = new Node(in.nextInt(), in.nextInt());
if ( in.next().charAt(0) == 'A' ) A.add(node);
else B.add(node);
}
// 判断m个直线
while ( m-- != 0 ) {
if ( valid(A, B, in.nextInt(), in.nextInt(), in.nextInt()) ) System.out.println("Yes");
else System.out.println("No");
}
}
// 返回(x,y)点代入公式后的结果
public static int compute( int a, int b, int c, int x, int y ) {
return a + b*x + c*y;
}
// 用于判断直线是否能隔开两类点,a、b、c参数如题意所示
public static boolean valid( List<Node> A, List<Node> B, int a, int b, int c ) {
// 先判断A类点在哪一边
Boolean flag = compute(a,b,c,A.get(0).x,A.get(0).y) > 0? true:false;
// 判断A类点是否跟第一个点在一边,不是的话直接返回false
for ( Node node: A )
if ( (flag && compute(a,b,c,node.x,node.y) < 0) || (!flag && compute(a,b,c,node.x,node.y) > 0) ) return false;
// 经过上面的判断,A类点的位置已经确定,接着判断B类点是否全部在另一边
for ( Node node: B )
if ( (flag && compute(a,b,c, node.x, node.y) > 0) || (!flag && compute(a,b,c, node.x, node.y) < 0) ) return false;
return true;
}
}
class Node {
int x,y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
//由所有点的坐标和给定直线的三个参数均为整数,且绝对值≤10^6;
//可能会使得lines[i].theta1 * locations[j].x + lines[i].theta2 * locations[j].y + lines[i].theta0; 超过int的范围 约 2x10^9
//故用long存储。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
int N = inScanner.nextInt();
int M = inScanner.nextInt();
location[] locations = new location[N];
line[] lines = new line[M];
//读取坐标和标记值
for (int i = 0; i < locations.length; i++) {
locations[i] = new location();
locations[i].x = inScanner.nextInt();
locations[i].y = inScanner.nextInt();
locations[i].type = inScanner.next();
}
//读取线段的参数
for (int i = 0; i < lines.length; i++) {
lines[i] = new line();
lines[i].theta0 = inScanner.nextInt();
lines[i].theta1 = inScanner.nextInt();
lines[i].theta2 = inScanner.nextInt();
}
for (int i = 0; i < lines.length; i++) {
long result = lines[i].theta1 * locations[0].x + lines[i].theta2 * locations[0].y + lines[i].theta0;//先读取第一个坐标
String flagString = locations[0].type;//先读取第一个坐标
boolean flag = true;//标记这条直线是否符合要求
//根据与第一个坐标运算的值 相乘, 符号相同,则是同类,否则是异类
for (int j = 1; j < locations.length; j++) { // 从第二行开始,遍历每一个坐标
long temp = lines[i].theta1 * locations[j].x + lines[i].theta2 * locations[j].y + lines[i].theta0;
if(result * temp > 0 && !locations[j].type.equals(flagString)) // 符号相同,应该是同类的,但标记的类型不同,于是这条直线不符合要求
{
flag = false;
break;
}
if(result * temp < 0 && locations[j].type.equals(flagString))// 符号相异,不应该是同类的,但标记的类型相同,于是这条直线不符合要求
{
flag = false;
break;
}
}
if (flag) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
}
class location{
int x,y;
String type;
}
class line{
int theta0,theta1,theta2;
}
今日推歌
----《倒数》 邓紫棋
还没到的樱花季 还没用的照相机
还没光临的餐厅 还在期待 有着你的旅行
等待日落的巴黎 铁塔之下牵着你
等待说着我愿意 等待未来 每天身边有你
一点一滴每一天珍惜
怕突然来不及 好好的爱你