求把一串数字变成日期的最小值

c++ 经验分享(2)
题目:算出大于一串数字的最小值,要求此数值的相邻两位不能相同。
#include &iostream&#include &string&
//补足最小数:void insertMin(string &sStr,string::iterator it){int i=0;for(;it!=sStr.end();it++){*it='0' + i++%2;} }
//转换字符串void ConvertNum(string &sStr){for (string::iterator it = sStr.begin();it!=sStr.end();){//如果是第一个字符if(it==sStr.begin()){//如果第一个字符就是":",则直接补足最小数,例如:999+1,则第一位为'9'+1=':',得到010.if(*it=='9'+1){insertMin(sStr,it);}it++;}//如果本位字符是":",则从本位开始补足最小值,迭代器前移一位,继续判断if(*it=='9'+1){insertMin(sStr,it);if(it!=sStr.begin()) it--;*it+=1;}//如果本位同前一位相同,则给本位加1,补足最小值,从本位开始继续转换。if(*it==*(it-1)){*it+=1;insertMin(sStr,it+1);}it++;}}int main(){cout&&"输入三个零则退出!"&&while(cout&&"请输入字符串: ",cin&&str){if(str=="000")cout&&string::reverse_iterator rit=str.rbegin();*rit+=1;ConvertNum(str);cout&&"-&"&&str&&endl&&}return 0;}
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4177次
排名:千里之外java中,用户输入一串数字,保存到数组中,怎么输入的最大最小值。 求详细代码,谢谢!-学网-中国IT综合门户网站-提供健康,养生,留学,移民,创业,汽车等信息
> 信息中心 >
java中,用户输入一串数字,保存到数组中,怎么输入的最大最小值。 求详细代码,谢谢!
来源:互联网 发表时间: 18:14:48 责任编辑:王亮字体:
为了帮助网友解决“java中,用户输入一串数字,保存到数组中,怎么输入的最大最小值。 求详细代码,谢谢!”相关的问题,学网通过互联网对“java中,用户输入一串数字,保存到数组中,怎么输入的最大最小值。 求详细代码,谢谢!”相关的解决方案进行了整理,用户详细问题包括:RT,我想知道:java中,用户输入一串数字,保存到数组中,怎么输入的最大最小值。 求详细代码,谢谢!,具体解决方案如下:解决方案1: /&#47S i++) { BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.out. 5; try { /); } } / } catch (NumberFormatException e) {请输入五个数&最大值 S从小到大进行排序 A&#47.readLine()); &#47.printStackTrace(); for (int i = 0;&#47.printStackTrace(); i &lt.println(num[0]).out.parseInt(bufferedR最小值 System.in)).println(num[4]);&#47.sort(num);将输入的五个数保存到数组中 num[i]=I int[] num=new int[5].println(& } catch (IOException e) { e解决方案2:
&&0.in);args)throws&nbsp,(i+1)));i&&nbsp:&quot.parseInt(line.&&=&&&nbsp.*;最小值为;A&&&&&new&&&&&nbsp.sort(arr);&&&void&&I&&&&arr[0]);public&&nbsp.length()];&+&MinDemo{&Exception{&&=&&=&new&&&&&&&arr&&&&nbsp.&class&&int[]&&&&&&&&&nbsp.println(&&line&System.substring(i;Scanner(S&&&&&i&public&&&&&i++){&=&&&&sc&&nbsp.next();main(String[]&&&&static&=&Scanner&&&}&&&arr[i]&&&String&nbsp.&&int[for(int&&&nbspimport&&&&nbsp
解决方案3:
问题描述不详细啊,表述的不太明白,能说清楚点吗---用户输入一些数,如10,20,30,40……然后呢你想要做什么
1个回答1个回答1个回答1个回答1个回答1个回答1个回答1个回答1个回答1个回答
相关文章:
最新添加资讯
24小时热门资讯
Copyright © 2004- All Rights Reserved. 学网 版权所有
京ICP备号-1 京公网安备02号c++ 经验分享(2)
题目:算出大于一串数字的最小值,要求此数值的相邻两位不能相同。
#include &iostream&#include &string&
//补足最小数:void insertMin(string &sStr,string::iterator it){int i=0;for(;it!=sStr.end();it++){*it='0' + i++%2;} }
//转换字符串void ConvertNum(string &sStr){for (string::iterator it = sStr.begin();it!=sStr.end();){//如果是第一个字符if(it==sStr.begin()){//如果第一个字符就是":",则直接补足最小数,例如:999+1,则第一位为'9'+1=':',得到010.if(*it=='9'+1){insertMin(sStr,it);}it++;}//如果本位字符是":",则从本位开始补足最小值,迭代器前移一位,继续判断if(*it=='9'+1){insertMin(sStr,it);if(it!=sStr.begin()) it--;*it+=1;}//如果本位同前一位相同,则给本位加1,补足最小值,从本位开始继续转换。if(*it==*(it-1)){*it+=1;insertMin(sStr,it+1);}it++;}}int main(){cout&&"输入三个零则退出!"&&while(cout&&"请输入字符串: ",cin&&str){if(str=="000")cout&&string::reverse_iterator rit=str.rbegin();*rit+=1;ConvertNum(str);cout&&"-&"&&str&&endl&&}return 0;}
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4177次
排名:千里之外下载作业帮安装包
扫二维码下载作业帮
1.75亿学生的选择
一串自然数:8,15,22,29,36,43,…从它的第二个数开始,每个数字都比它前面相邻的数大7.已知这串自然数的前n-1个相乘的积的末尾0的个数比前n个数相乘的积的末尾0的个数少3个,求出n的最小值?
爪机粉丝00806
首先,分析×2×5×5×5,对于这串数列,显然2的因子是足够的(相对于5来说),所以可以得出第n个数一定可以被125整除,所以对于最小的n,一定有125整除第n个数.数列的通项是An=7n+1,所以只需解满足7n+1=125m(m,n为正整数)的n的最小值这个式子可以用数论的知识解,7n=125m-1,n=(125m-1)/7=125m/7-1/7又125=7×17+6,所以原式等价于n=6m/7-1/7易得满足条件的m=6,n=107所以,n的最小值为107
为您推荐:
其他类似问题
扫描下载二维码& 找出最小值
PHP min 找出最小值
min & 找出最小值
( array $numbers
[, array $...
如果仅有一个参数且为数组,min()
返回该数组中最小的值。如果给出了两个或更多参数,
min() 会返回这些值中最小的一个。
PHP 会将非数值的
0,但如果这个正是最小的数值则仍然会返回一个字符串。如果多个参数都求值为
0 且是最小值,min()
会返回按字母表顺序最小的字符串,如果其中没有字符串的话,则返回数值的 0。
Example #1 使用 min() 的例子
&?phpecho&min(2,&3,&1,&6,&7);&&//&1echo&min(array(2,&4,&5));&//&2echo&min(0,&'hello');&&&&&//&0echo&min('hello',&0);&&&&&//&helloecho&min('hello',&-1);&&&&//&-1//&对多个数组,min&从左向右比较。//&因此在本例中:2&==&2,但&4&&&5$val&=&min(array(2,&4,&8),&array(2,&5,&1));&//&array(2,&4,&8)//&如果同时给出数组和非数组作为参数,则不可能返回数组,因为//&数组被视为最大的$val&=&min('string',&array(2,&5,&7),&42);&&&//&string?&
PHP min note #1
Here is function can find min by array key
function min_by_key($arr, $key){
&&& $min = array();
&&& foreach ($arr as $val) {
&& & && if (!isset($val[$key]) and is_array($val)) {
&& & & & && $min2 = min_by_key($val, $key);
&& & & & && $min[$min2] = 1;
&& & && } elseif (!isset($val[$key]) and !is_array($val)) {
&& & & & && return false;
&& & && } elseif (isset($val[$key])) {
&& & & & && $min[$val[$key]] = 1;
&&& return min( array_keys($min) );
PHP min note #2
If NAN is the first argument to min(), the second argument will always be returned.
If NAN is the second argument, NAN will always be returned.
The relationship is the same but inverted for max().
print max(0,NAN);
print max(NAN,0);
print min(0,NAN);
print min(NAN,0);
PHP min note #3
A condensed version (and possible application) of returning an array of array keys containing the same minimum value:
$min_keys = array();
$player_score_totals = array(
'player1' =& 300,
'player2' =& 301,
'player3' =& 302,
'player4' =& 301,
foreach($player_score_totals as $playerid =& $score)
&&& if($score == min($player_score_totals)) array_push($min_keys, $playerid);
print_r($min_keys);
PHP min note #4
When using a variable with an array that has a list of numbers, put just the variable in min(). Don't use integer index's. Seems pretty straight forward now, but I wasn't used to just putting down the variable for an array in functions.
& $list = array(9,5,4,6,2,7);
& echo min($list); ?&
PHP min note #5
min/max works with yyyy-mm-dd dates, e.g.
$a = array(&& '',& & ''& );
print min($a); print max($a); ?&
Which is a unexpected since at the top of this page it says.
"PHP will evaluate a non-numeric string as 0"
And if we cast it as an int then we get the year
print (int)""; ?&
PHP min note #6
PHP_INT_MAX is the MAXINT equivalent
PHP min note #7
I have found a very useful trick to help get round the problem of setting a variable to max int when finding a min i.e.
$val = 10;
$min = 100000;& if ($val & $min)
& $min = $val;
try this ...
$dataSet = Array(7, 8, 9, -1, -100, 0, 1, 2, 3, 5, 6);
$min = true;
$max = true;
&& & & & &&
foreach ($dataSet as $data )
& echo("data = $data");
& $min = min($data, $min);
& $max = max($data, $max);
&& & & & &&
echo("min = $min");
echo("max = $max");
$max can be set to anything e.g. "infinity", but the same trick doesn't work with min, however true does work (false doesn't). Not sure why though.
The above code even works with a data set like this...
&?php $dataSet = Array("0.5", 1, 2, "3", "-1", "5"); ?&
but min doesn't like null or negative float e.g. "-1.2" and "" can give some odd results too.
PHP min note #8
I've modified the bugfree min-version to ignore NULL values (else it returns 0).
function min_mod () {
& $args = func_get_args();
& if (!count($args[0])) return false;
&&& $min = false;
&&& foreach ($args[0] AS $value) {
&& && if (is_numeric($value)) {
&& & && $curval = floatval($value);
&& & && if ($curval & $min || $min === false) $min = $curval;
& return $min;&&
PHP min note #9
A way to bound a integer between two values is:
function bound($x, $min, $max)
&& & return min(max($x, $min), $max);
which is the same as:
$tmp = $x;
if($tmp & $min)
&&& $tmp = $
if($tmp & $max)
&& & $tmp = $
So if you wanted to bound an integer between 1 and 12 for example:
echo bound(0, 1, 12).'&br /&';
echo bound($x, 1, 12).'&br /&';
echo bound($x, 1, 12).'&br /&';
echo bound($x, 1, 12).'&br /&';
echo bound($x, 1, 12).'&br /&';
PHP min note #10
You will get an "Wrong parameter count" error (PHP 4 and possibly 5) if your array looks like the following:
min(115.23,432.11,0.00,45.76)
The 0.00 creates the error. Convert the 0.00 to a high number such as .00 or remove it from the array before running the min() function.
PHP min note #11
Be very careful when your array contains both strings and numbers. This code works strange (even though explainable) way:
var_dump(max('25.1.1', '222', '99'));
var_dump(max('2.1.1', '222', '99'));
PHP min note #12
empty strings '' will also return false or 0, so if you have something like
$test = array('', 1, 5, 8, 44, 22);
'' will be returned as the lowest value
if you only want to get the lowest number, you'll have to resort to the old fashioned loop
// default minimum value
$minVal = 100;
foreach ($test as $value) {
if (is_numeric($value) && $value & $minVal) {
$minVal = $
PHP min note #13
Regarding boolean parameters in min() and max():
(a) If any of your parameters is boolean, max and min will cast the rest of them to boolean to do the comparison.
(b) true & false
(c) However, max and min will return the actual parameter value that wins the comparison (not the cast).
Here's some test cases to illustrate:
1.& max(true,100)=true
2.& max(true,0)=true
3.& max(100,true)=100
4.& max(false,100)=100
5.& max(100,false)=100
6.& min(true,100)=true
7.& min(true,0)=0
8.& min(100,true)=100
9.& min(false,100)=false
10. min(100,false)=false
11. min(true,false)=false
12. max(true,false)=true
PHP min note #14
& NEVER EVER use this function with boolean variables !!!
& Or you'll get something like this: min(true, 1, -2) ==
& Just because of:
& min(true, 1, -2) == min(min(true,1), -2) == min(true, -2) ==
It is possible to use it with booleans, there is is just one thing, which you need to keep in mind, when evaluating using the non strict comparison (==) anyting that is not bool false, 0 or NULL is consideret true eg.:
(5 == true) =
(0 == true) =
true is also actually anything else then 0, false and null. However when true is converted to a string or interger true == 1, therefore when sorting true = 1. But if true is the maximum number bool true is returned. so to be sure, if you only want to match if true is the max number remember to use the strict comparison operater ===
PHP min note #15
NEVER EVER use this function with boolean variables !!!
Or you'll get something like this: min(true, 1, -2) ==
Just because of:
min(true, 1, -2) == min(min(true,1), -2) == min(true, -2) ==
You are warned !
PHP min note #16
Here is my slightly modified version of the bugfree min-version. Now the max() function is no longer used in the modification and overall it's fasten up. Would be nice to get some feedback.
function min_mod () {
& $args = func_get_args();
& if (!count($args)) return false;
&&& $min = false;
&&& foreach ($args AS $value) {
&& && $curval = floatval($value);
&& && if ($curval & $min || $min === false) $min = $curval;
& return $min;& &
PHP min note #17
If you want min to return zero (0) when comparing to a string, try this:
min(3,4,";");& min(0,min(3,4,";")) ?&
PHP min note #18
I tested this with max(), but I suppose it applies to min() too: If you are working with numbers, then you can use:
&&& $a = ($b & $c) ? $b : $c;
&which is somewhat faster (roughly 16%) than
&&& $a = min($b, $c);
&I tested this on several loops using integers and floats, over 1 million iterations.
&I'm running PHP 4.3.1 as a module for Apache 1.3.27.
PHP min note #19
min() can be used to cap values at a specific value. For instance, if you're grading papers and someone has some extra credit, but& that shouldn't make it to the final score:
$pts_possible = 50;
$score = 55;
// Percent will equal 1 if $score/$pts_possible is greater than 1
$percent = min($score/$pts_possible,1);
PHP min note #20
Further modifications to the minnum function above.. This is for a project where I had to grab an entire column out of a database consisting of values that might be string, might be string representations of numbers (floating point or integer) or might be NULL, and find the minimum NUMERIC value:
function minnum($numarray){
&&& //dont use min(), it contains a bug!
&&& $min=0;
&&& if ( ! is_array($numarray) ) $numarray = func_get_args();
&&& if(is_array($numarray)==true){
&& & && $min=max($numarray);
&& & && for($z=0;$z&count($numarray);$z++){
&& & & & && $curval=floatval($numarray[$z]);
&& & & & && if(($curval != 0) && ($curval & $min)){
&& & & & & & && $min=$
&& & & & && }
&&& return $
Gets the floating point value of each entry and uses this to check whether it's actually a number before checking whether it's the minimum or not. Also contains modifications noted above to use it as a drop in replacement for min - ie multiple values passed.
PHP min note #21
Caution : it seems that min() can return a string :
min(";",50)=";" (I expected zero)
PHP min note #22
if you have an array like this
$arSrc[0]=14;
$arSrc[1]=16;
$arSrc[2]=13;
$arSrc[3]=17;
then in order to get the min element and its position in the array you can do:
$iMinValue = min($arSrc);
$arFlip = array_flip($arSrc);
$iMinPosition = $arFlip[$iMinValue];
&'&br /&min_value=',
&$iMinValue,
&'&br /&min_position=',
&$iMinPosition
this example works for also for
of course with numeric values
PHP min note #23
Re: above example - for a proper drop in replacement for the above, insert
if ( ! is_array($numarray) )
&& $numarray = func_get_args();
&& $min=0;
(For PHP3, check
if (intval(PHP_VERSION) &= 4 && ! is_array($numarray))
&& $numarray = func_get_args();
PHP min note #24
The 'undefined' behaviour can bit you badly. I would expect min(undefined, -1000) to return -1000. Not so.
PHP min note #25
If one of elements is undefided, min() result is underfinded too
PHPMath - 函数

我要回帖

更多关于 把一串数字变成日期 的文章

 

随机推荐