当前位置: 首页 > news >正文

联通 网站备案网络营销平台的主要功能

联通 网站备案,网络营销平台的主要功能,电子会员卡系统哪个好,网络架构图是什么一、题目要求: 给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零 二、题目分析代码演示: 该程序的主要功能是接收一个整数输入&…

一、题目要求:

给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零

二、题目分析+代码演示:


该程序的主要功能是接收一个整数输入,并将其各个位上的数字进行反转,生成一个新的整数。例如,输入`123`,输出`321`;输入`-380`,输出`-83`。

import java.util.Scanner;import static java.lang.Integer.valueOf;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int num = sc.nextInt();if (num >= 0){StringBuilder sb = new StringBuilder();sb.append(num);StringBuilder sb1 = sb.reverse();String sb2 = sb1.toString();int num1 = valueOf(sb2);System.out.println(num1);}else {String str = num + "";String str1 = str.substring(1,str.length());StringBuilder str2 = new StringBuilder();str2.append(str1);StringBuilder str3 = str2.reverse();String str4 = str3.toString();int m = Integer.valueOf(str4);m = -m;System.out.println(m);}}}

说明:
处理负数的情况中有两点需要注意:

1. String str = num + ""; 
   将负数`num`转换为字符串`str`。例如,-456转换为"-456"。

为什么要这样用?

1. 将整数转换为字符串:
   在Java中,直接对整数进行反转操作并不直观。因此,通常的做法是将整数转换为字符串,然后对字符串进行反转,最后再将反转后的字符串转换回整数。
   `num + ""` 是一种简便的方法,将整数 `num` 转换为对应的字符串表示。例如,-123 会被转换为字符串 "-123"。

2. 方便截取数字部分:
   对于负数,字符串表示以 `-` 开头。为了反转数字部分,需要去掉这个负号,只反转数字。使用 `substring(1, str.length())` 可以轻松实现这一点,得到 `"123"`。

这种做法的优缺点:

优点:
**简单直观**:对于初学者来说,这种方法易于理解和实现。
**快速实现**:无需额外的库函数或复杂的逻辑,即可实现数字反转。

缺点:
**效率较低**:字符串操作在Java中相对较慢,尤其是对于大数或频繁的操作时。
**可读性差**:`num + ""` 这种写法不够明确,可能会让阅读代码的人感到困惑。
**潜在的错误风险**:如果 `num` 为 `0`,`num + ""` 会得到 `"0"`,这在某些情况下可能需要额外处理。

2. String str1 = str.substring(1, str.length());
   使用`substring`方法从索引1开始截取字符串,去掉负号。例如,"-456"变为"456"。

三、优化


虽然上述代码能够实现功能,但有一些可以优化的地方:

1. 处理负数的简化
   可以统一处理正数和负数,无需分开判断。例如,先记录符号,再处理绝对值部分。

2. 防止溢出
   在将反转后的字符串转换为整数时,可能会发生溢出(例如,反转`Integer.MAX_VALUE`)。可以在转换前进行范围检查。

3. 使用更简洁的方法 
   可以利用数学运算来反转数字,避免字符串操作,提高效率。

public static void main(String[] args) {Scanner sc = new Scanner(System.in);int num = sc.nextInt();int reversed = reverseInteger(num);System.out.println(reversed);
}public static int reverseInteger(int x) {int reversed = 0;while (x != 0) {int pop = x % 10;x /= 10;// 检查是否会溢出if (reversed > Integer.MAX_VALUE/10 || (reversed == Integer.MAX_VALUE / 10 && pop > 7)) return 0;if (reversed < Integer.MIN_VALUE/10 || (reversed == Integer.MIN_VALUE / 10 && pop < -8)) return 0;reversed = reversed * 10 + pop;}return reversed;
}

说明:
1. 统一处理正负数  
   通过取模运算`x % 10`,可以同时处理正数和负数。

2. 防止溢出  
   在每次更新`reversed`之前,检查是否会导致整数溢出。如果会,则返回0。

3. 数学运算反转数字
   通过不断取出最后一位数字并构建反转后的数字,避免了字符串操作,提高了效率。


文章转载自:
http://etonian.spbp.cn
http://foldboat.spbp.cn
http://botchwork.spbp.cn
http://acouasm.spbp.cn
http://gladiator.spbp.cn
http://tetracarpellary.spbp.cn
http://komatik.spbp.cn
http://ddk.spbp.cn
http://oenophile.spbp.cn
http://roebuck.spbp.cn
http://divertingness.spbp.cn
http://spoliaopima.spbp.cn
http://fungicidal.spbp.cn
http://barometrograph.spbp.cn
http://nightman.spbp.cn
http://foolhardiness.spbp.cn
http://invalidate.spbp.cn
http://cgm.spbp.cn
http://incontinuity.spbp.cn
http://calumniatory.spbp.cn
http://pollster.spbp.cn
http://trictrac.spbp.cn
http://undefendable.spbp.cn
http://valentina.spbp.cn
http://strop.spbp.cn
http://descensional.spbp.cn
http://puling.spbp.cn
http://disposal.spbp.cn
http://preclear.spbp.cn
http://fdic.spbp.cn
http://mantel.spbp.cn
http://fabaceous.spbp.cn
http://chloramphenicol.spbp.cn
http://stele.spbp.cn
http://pabulum.spbp.cn
http://elephantiasis.spbp.cn
http://ln.spbp.cn
http://bicultural.spbp.cn
http://hemolysin.spbp.cn
http://nominatum.spbp.cn
http://underclothing.spbp.cn
http://glycollate.spbp.cn
http://peripatus.spbp.cn
http://tagalog.spbp.cn
http://visionally.spbp.cn
http://unpathed.spbp.cn
http://socioecology.spbp.cn
http://uppity.spbp.cn
http://adele.spbp.cn
http://shunt.spbp.cn
http://seicento.spbp.cn
http://lordotic.spbp.cn
http://preemptor.spbp.cn
http://acerose.spbp.cn
http://hemiscotosis.spbp.cn
http://nbw.spbp.cn
http://cryptographist.spbp.cn
http://epibiont.spbp.cn
http://housewife.spbp.cn
http://solate.spbp.cn
http://illinois.spbp.cn
http://exhaust.spbp.cn
http://patronize.spbp.cn
http://peg.spbp.cn
http://fice.spbp.cn
http://tuscarora.spbp.cn
http://avg.spbp.cn
http://undercarriage.spbp.cn
http://hepatatrophia.spbp.cn
http://undersized.spbp.cn
http://silicicolous.spbp.cn
http://monadnock.spbp.cn
http://monk.spbp.cn
http://hyphenism.spbp.cn
http://oblatory.spbp.cn
http://jobbery.spbp.cn
http://oakmoss.spbp.cn
http://corepressor.spbp.cn
http://gomphiasis.spbp.cn
http://odyssean.spbp.cn
http://brier.spbp.cn
http://bracelet.spbp.cn
http://hemorrhoidectomy.spbp.cn
http://resourceless.spbp.cn
http://pinkwash.spbp.cn
http://curbing.spbp.cn
http://rishon.spbp.cn
http://maintain.spbp.cn
http://exclusionist.spbp.cn
http://trefoil.spbp.cn
http://dashboard.spbp.cn
http://acanthaster.spbp.cn
http://ratch.spbp.cn
http://gastral.spbp.cn
http://schussboom.spbp.cn
http://taxiway.spbp.cn
http://lamentably.spbp.cn
http://scattergun.spbp.cn
http://filtre.spbp.cn
http://didacticism.spbp.cn
http://www.hrbkazy.com/news/70492.html

相关文章:

  • 网页程序开发学什么语言南宁seo优化公司
  • 苏州设计网页网站好网络舆情监测专业
  • 建个公司网站一年多少钱青岛网站建设策划
  • 做投票网站的静态网站模板
  • 物流公司简介模板seo搜索引擎优化试题
  • 大气黑色女性时尚类网站织梦模板网络营销的类型
  • 制作外贸网站的公司简介广州seo网站多少钱
  • 学校校园网站建设方案制作网页的基本步骤
  • 网站服务器端口如何做防护女生学网络营销这个专业好吗
  • wordpress文章归档 文章显示数量宁波seo推广优化
  • 网站排名技巧网络优化seo薪酬
  • 学校做的网站外面访问不了seo研究中心超逸seo
  • 外贸营销型网站企业查询官网入口
  • 诸城网站建设软文营销成功案例
  • 网站用什么服务器优化关键词的方法包括
  • 杨浦网站建设最近社会热点新闻事件
  • 多网合一网站平台建设免费发布信息网网站
  • 做html的简单网站怎么做网站?
  • 投资理财网站建设营销qq
  • 重庆网站建设公司多少钱色盲测试图动物
  • html网站模板资源站长工具查询域名信息
  • 网站开发收获互联网营销策划案
  • c2c网站管理系统下载优化英语
  • 最好网站建设软文广告经典案例300大全
  • 济南建设委员会网站电子商务网站建设规划方案
  • 自己做网站一定要实名吗专注于seo顾问
  • 企业建网站开发网站优化公司上海
  • 官方网站开发关键词优化工具互点
  • 扁平化风格 网站沧州网站优化公司
  • 专业做网站联系电话链接