UVa 1585 UVa1586 UVa 1225 learning summary

From a scum study record, I do n’t know why. I do n’t seem to see anyone writing algorithms in java, but I want to participate in the blue bridge cup java group to summarize the three questions I wrote in the past two days. I wrote three two days, I was too good, eh.

The first question, UVA 1585, gives a string composed of O and X, and calculates the score. The score of each O is the number of consecutive occurrences of O, and the score of X is 0. For example, the score of OOXXOXXOOO is 1 + 2 + 0 + 0 + 1 + 0 + 0 + 1 + 2 + 3

Difficult point:
In fact, it seems to be nothing, but the logic is a bit overwhelming, this question should be a little ks for the big brother. Code directly:

import java.util.*;
/*
                  给出一个由O与X组成的串,统计得分,每个O的得分为目前连续出现O的个数,X的得分为0,。例如,OOXXOXXOOO的得分为1+2+0+0+1+0+0+1+2+3
        */
public class Test1 {
 public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  String s = sc.next(); //输入字符串
  char c[] = s.toCharArray(); //将字符串变为数组
  int sum = 0; // 存总分
  int cound = 0; //存‘O’连续出现次数
  int t = 1;
  for(int i = 0; i<s.length(); i++){
   if(c[i] == 'O'){
    cound ++;
   }
   else { 
    for(int j = 1; j <= cound; j++){
     sum = sum+t;
     t++;
    }
    cound = 0; //出现‘X’重新计数
    t = 1;
   }
  }
  for(int j = 1; j <= cound; j++){//最后不是以X结尾的话我们要把cound值算完
   sum = sum+t;
   t++;
  }
  System.out.println(sum);
 }
}

The second question UVA1586 gives the molecular formula of a substance (without parentheses), and find the molecular weight. The molecular formula of this question only includes 4 kinds of atoms, namely C, H, O, N, and the atomic weight is 12.01, 1.008, 16.00, 14.01. For example, the molecular weight of C6H5OH is 94.108.
Difficulty:
When storing the input string with an array, the characters and numbers are Stored together, we have to separate him when we perform the operation, and we have to subtract the corresponding ASCLL code value, I do n’t know how you guys, anyway, I think for a long time, eh. Code above:

import java.util.*;
/*
 *     给出一个物质的分子式(不带括号),求分子量。本题的分子式只包括4种原子,分别为C,H,O,N, 原子量为12.01,1.008,16.00,14.01,例如C6H5OH的分子量为94.108
 */
public class Test2 {
 public static void main(String args[]){
  Scanner sc = new Scanner(System.in);
  String s = sc.next();
  char c[] = new char[100];
  char a[] = new char[100];
  c = s.toCharArray();  // 字符串变为数组
  int t = s.length(); 
  for(int i = 0; i < t; i++){ // 将c复制给a  便于最后一个元素没有下标时添加下标
   a[i] = c[i];
  }
  a[t] = 0; // 添加下标
  double sum = 0;
  for(int i = 0; i < t; i++){
   if(a[i] == 'C'){
    if(a[i+1] > '0' && a[i+1] <='9') // 有下标的话
     sum = sum + (a[i+1] - '0')*12.01;
    else
     sum = sum + 12.01;
   }
   if(a[i] == 'H'){
    if(a[i+1] > '0' && a[i+1] <='9')
     sum = sum + (a[i+1] - '0')*1.008;
    else
     sum = sum + 1.008;
   }
   if(a[i] == 'O'){
    if(a[i+1] > '0' && a[i+1] <='9')
     sum = sum + (a[i+1] - '0')*16.00;
    else
     sum = sum + 16.00;
   }
   if(a[i] == 'N'){
    if(a[i+1] > '0' && a[i+1] <='9')
     sum = sum + (a[i+1] - '0')*14.01;
    else
     sum = sum + 14.01;
   }
  }
  System.out.println(sum);
 }
}

In fact, the logic of the code is still very simple. As for why 0 is not added at the end, I do n’t understand it.-If someone sees it, I can see it in a comment or a private letter. Thank you. I do n’t want to think about it. It ’s invalid. -.

The third question UVA1225 input n integers connected together for example 123456789101112 requires the input of 10 integers to find the number of '0,1,2,3,4,5,6,7,8,9'
Difficulties:
one count How can I add a single digit and add other digits and save him, in fact, it is still quite simple to think of, this question is not very good I did not think, Baidu referred to others. Code above:

import java.util.Scanner;
import java.util.Arrays;
/*
 * 输入n个整数  连在一起 例如 123456789101112  要求输入10个整数 求‘0,1,2,3,4,5,6,7,8,9’的个数
 */
public class Test3 {
 public static void main(String args[]){
  int a[] = new int[10];
  Arrays.fill(a,0); //将数组元素全赋值为0
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  for(int i = 1; i <= n; i++){
   int j = i;
   while(j != 0){//最重要部分  每一个数都分隔出各个位数算一遍 同时利用数组下标直接++
    a[j%10]++;
    j = j/10;
   }
  }
  for(int b:a)
   System.out.print(b+" ");
 }
}

If there is something wrong, welcome to comment. I am a scum student. The school does not have a school team. I want to participate in some competitions. So I have a few questions, but it feels very difficult for me. Come on, change Can not change the environment can only change themselves, if there is a big guy to write algorithms in java, can you give an address.

Published 32 original articles · praised 5 · visits 862

Guess you like

Origin blog.csdn.net/shizhuba/article/details/102527033