当前位置:网站首页>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()。
边栏推荐
- Nodejs judge system type get host name execute console command Chinese garbled code
- 基于谱加权的波束方向图分析
- Adobe Premiere Foundation (animation) (VII)
- 【Vulnhub靶场】JANGOW: 1.0.1
- Adobe Premiere基础(视频的最后一步字幕添加)(六)
- 数据库防火墙技术展望【终章】
- In the introductory study of data visualization, we should be alert to pitfalls and misunderstandings and grasp key nodes
- In the digital age, why should enterprises make digital transformation?
- OPENCV 检测人脸 不依赖于任何第三方库
- MySQL索引失效场景
猜你喜欢

超级简单的课程设计ssm学生管理系统(含源码简单添加、删除、修改、查询操作)

TestNG的HelloWorld例子以及如何在命令行下运行

【代理】10分钟掌握正向代理和反向代理的本质区别

Adobe Premiere基础(动画制作)(七)

Adobe Premiere基础(轨道相关)(五)

跨域报错:When allowCredentials is true, allowedOrigins cannot contain the special value “*“

第二章 数据类型(一)

阵列信号处理仿真之四——Z变换分析阵列多项式

Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)

Adobe Premiere基础-素材嵌套(制作抖音结尾头像动画)(九)
随机推荐
Nodejs judge system type get host name execute console command Chinese garbled code
VS从txt文件读取中文汉字产生乱码的解决办法(超简单)
Rewrite clear Bayesian formula with base ratio
Adobe Premiere foundation - time remapping (10)
Ruijie x32pro brush openwrt enable wireless 160MHz
Adobe Premiere基础-介绍,配置,快捷键,创建项目,创建序列(一)
mysql8.0(新特性小结)
How to play the Dragon Boat Festival "immersive cloud Tour"? That is to say, it helps "live broadcast +" new scene landing
《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析
Db2 SQL PL的动态SQL
Adobe Premiere基础-导入导出,合并素材,源文件编译,脱机(二)
Win32 child window parent window window owner
RK1126 新添加一个模块
Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题
Chapter IV data type (III)
Three ways generated by stream lambda
Adobe Premiere基础-不透明度(混合模式)(十二)
Live broadcast preview | deconstruct OLAP! The new multidimensional analysis architecture paradigm is fully open! Apache Doris will bring five big issues!
[kuangbin]专题二十二 区间DP
瑞芯微RK1126平台 平台移植libevent 交叉编译libevent