当前位置:网站首页>2022.05.29(LC_6079_价格减免)
2022.05.29(LC_6079_价格减免)
2022-06-10 18:16:00 【Leeli9316】

方法:模拟
import java.text.DecimalFormat;
class Solution {
public String discountPrices(String sentence, int discount) {
String[] str = sentence.split(" ");
for (int i = 0; i < str.length; i++) {
if (satisfy(str[i])) {
java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
double num = Double.valueOf(str[i].substring(1, str[i].length()));
num = num - (num * ((double)discount / 100));
str[i] = '$' + df.format(num);
}
}
String ans = "";
for (int i = 0; i < str.length; i++) {
if (i != str.length - 1) {
ans += str[i] + " ";
} else {
ans += str[i];
}
}
return ans;
}
public boolean satisfy(String s) {
if (s.charAt(0) != '$') return false;
if (s.charAt(s.length() - 1) == '$') return false;
int count$ = 0, countD = 0, countC = 0;
for (char ch : s.toCharArray()) {
if (ch == '$') {
count$++;
} else if (Character.isDigit(ch)) {
countD++;
} else {
countC++;
}
}
if (count$ != 1 || countC > 0) return false;
return true;
}
}class Solution {
public String discountPrices(String sentence, int discount) {
String[] s = sentence.split(" ");
for (int i = 0; i < s.length; i++) {
if (satisfy(s[i])) {
double num = Double.valueOf(s[i].substring(1));
num = num * (1 - (double)discount / 100);
s[i] = "$" + String.format("%.2f", num);
}
}
return String.join(" ", s);
}
//判断单词是否为价格(单词满足的条件:第一位是$,其他位是数字)
public boolean satisfy(String s) {
if (s.charAt(0) != '$' || s.length() == 1) return false;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') {
return false;
}
}
return true;
}
}使用库函数:String.format(),String.join()。
边栏推荐
- Adobe Premiere Basics - introduction, configuration, shortcut keys, creating projects, creating sequences (I)
- Adobe Premiere Foundation (animation) (VII)
- Adobe Premiere基础(动画制作)(七)
- What is Bi? Talk about the definition and function of Bi
- 直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
- Openssl1.1.1 vs2013 compilation tutorial
- openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC
- 第三章 数据类型(二)
- 【杂谈】恭喜自己获得CSDN专家称号,努力终会换来结果
- 调试的技巧
猜你喜欢

Adobe Premiere基礎-工具使用(選擇工具,剃刀工具,等常用工具)(三)

Metadata management, the basic construction of enterprises in the digital era

How to correctly understand the real-time nature of Bi?

Real time business intelligence Bi (II): reasonable ETL architecture design to realize quasi real time Business Intelligence BI

SaleSmartly | 再添新渠道Slack,助你拉近客户关系

Adobe Premiere Foundation (animation) (VII)

How to set up salesmartly for Google Analytics tracking

Adobe Premiere基础-不透明度(混合模式)(十二)

Stream流的常用方法-Lambder

直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
随机推荐
Wireshark learning notes (I) common function cases and skills
How to correctly understand the real-time nature of Bi?
Adobe Premiere Basic - tool use (select tools, rasoir tools, and other Common Tools) (III)
Rewrite clear Bayesian formula with base ratio
How to realize the management "desire" of Business Intelligence BI service objects and enterprise managers?
数据库防火墙技术展望【终章】
第三章 数据类型(二)
Google Earth engine (GEE) -- Copernicus atmosphere monitoring (CAMs) global aerosol AOI near real-time observation data set
《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析
Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
Rk1126 adds a new module
SQL 函数
Adobe Premiere基础-不透明度(混合模式)(十二)
MySQL高级篇第一章(linux下安装MySQL)【上】
Data URL
Live broadcast preview | deconstruct OLAP! The new multidimensional analysis architecture paradigm is fully open! Apache Doris will bring five big issues!
Adobe Premiere Foundation (track related) (V)
[kuangbin]专题十二 基础DP1
Ruijie x32pro brush openwrt enable wireless 160MHz
WordPress 6.0 "Arturo Arturo" release