求一算法……

ty_fzpb 2009-06-03 02:44:27
N为正整数,有一个函数f(N),返回0到N之间出现的数字"1"的个数。比如f(12)=5,现在f(1)=1,求100000以内的,使得f(N)=N的所有N?要求:算法简洁高效
...全文
250 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ty_fzpb 2009-06-04
  • 打赏
  • 举报
回复
先谢谢大家了,我也觉得题目有些问题。 结贴
sachmx1234 2009-06-04
  • 打赏
  • 举报
回复
顶了
wuyaowen2000 2009-06-04
  • 打赏
  • 举报
回复
请人做作业就是比较好,我也喜欢,哈哈
yhl13773563078 2009-06-04
  • 打赏
  • 举报
回复
题目好象有点问题
huberypang 2009-06-04
  • 打赏
  • 举报
回复
占沙发
复习时有空就想想.
Adebayor 2009-06-03
  • 打赏
  • 举报
回复
帮顶
lulu0126 2009-06-03
  • 打赏
  • 举报
回复
mark!

等待高效算法!

rypgood 2009-06-03
  • 打赏
  • 举报
回复
纯粹的数学题
mulinqiu 2009-06-03
  • 打赏
  • 举报
回复
想想这问题!waiting!
高亮 2009-06-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 seaman_xh 的回复:]
引用 13 楼 csljs412 的回复:
确实只有1


遍历的数太少 10000000内
f(199981)=199981
f(199982)=199982
f(199983)=199983
f(199984)=199984
f(199985)=199985
f(199986)=199986
f(199987)=199987
f(199988)=199988
f(199989)=199989
f(199990)=199990
f(200000)=200000
f(200001)=200001
f(1599981)=1599981
f(1599982)=1599982
f(1599983)=1599983
f(1599984)=1599984
f(1599985)=159…
[/Quote]

public class Test28 {
public static void main(String[] args) {
int count = 0;
int sum = 0;
for(int i = 1; i < 100000001; i++) {
sum += fx(i);
if(sum == i) {
show(i);
count++;
}
}
System.out.println("100000000以内有"+count+"个数满足f(N)=N");
}
/*N为正整数,有一个函数f(N),返回0到N之间出现的数字"1"的个数。
*比如f(12)=5,现在f(1)=1,求100000以内的,使得f(N)=N的所有N?
*要求:算法简洁高效*/
public static int f(int n) {
return 0; // 未实现
}
public static int fx(int n) { //求N中有几个1
int result = 0;
while(n > 0) {
if(n % 10 == 1) result++;
n = n / 10;
}
return result;
}
public static void show(int n) {
System.out.println("f("+n+")="+n+"\t");
}
}


15楼说的好,满足这个要求的数在后面。我找了100000000(一亿)中的这样的数,只有40个。可以运行下我写的程序。机子一般,用不了一分钟的。
afeilxc 2009-06-03
  • 打赏
  • 举报
回复

修改一下 呵呵 刚才忘记一个循环了 现在我想可以了!
public class Main {

public static void main(String[] args) {
System.out.println(getMarkNum(100000));
}
public static int getMarkNum(int n){
int count=0;
for(int i=1;i<=n; i++)
{
String str=String.valueOf(i);
for (int j = 0; j < str.length(); j++) {
char ch = str.charAt(j);
if(ch=='1')
count++;
}
}
return count;
}
}
seaman_xh 2009-06-03
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 csljs412 的回复:]
确实只有1
[/Quote]

遍历的数太少 10000000内
f(199981)=199981
f(199982)=199982
f(199983)=199983
f(199984)=199984
f(199985)=199985
f(199986)=199986
f(199987)=199987
f(199988)=199988
f(199989)=199989
f(199990)=199990
f(200000)=200000
f(200001)=200001
f(1599981)=1599981
f(1599982)=1599982
f(1599983)=1599983
f(1599984)=1599984
f(1599985)=1599985
f(1599986)=1599986
f(1599987)=1599987
f(1599988)=1599988
f(1599989)=1599989
f(1599990)=1599990
f(2600000)=2600000
f(2600001)=2600001
afeilxc 2009-06-03
  • 打赏
  • 举报
回复

public class Main {

public static void main(String[] args) {
System.out.println(getMarkNum(12121));
}
public static int getMarkNum(int n){
String str=String.valueOf(n);
int count=0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if(ch=='1')
count++;
}
return count;
}}



不知道楼主是不是这个意思?
csljs412 2009-06-03
  • 打赏
  • 举报
回复
确实只有1
seaman_xh 2009-06-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ty_fzpb 的回复:]

谢谢2楼积极回帖
我所要的不是f(int n)方法……
[/Quote]

高效的不会,笨方法遍历,
既然遍历,下边效率也一样,容易理解

public static void f(int n) {
int tmp, count = 0;

for(int i=1; i<=n; i++){
tmp = i;
while(tmp > 0) {
if(tmp%10 == 1)
count ++;
tmp /= 10;
}
if(count == i) {
System.out.println("f(" + i + ")=" + i + "\t");
}
}
}

loveappleforever 2009-06-03
  • 打赏
  • 举报
回复
学习了
Landor2004 2009-06-03
  • 打赏
  • 举报
回复
package testjava;
public class Main {

public static void main(String[] args) {
System.out.println(test(1000000));
}
public static int test(int n){
int count=0;
for(int i=1;i<=n;i++){
String str = i+"";
for(int j=0;j<str.length();j++){
if(str.charAt(j)=='1'){
count++;
}
}
}
return count;
}
}
lycrsx 2009-06-03
  • 打赏
  • 举报
回复
int 就是整数的意思啊,楼主不是要算法吗
三楼的算法对的啊
alex_0XFF 2009-06-03
  • 打赏
  • 举报
回复
我晕,微软的面试题啊,去网上找一下就ok
kome2000 2009-06-03
  • 打赏
  • 举报
回复
那你题出错了吧!
f(N)=N?
也只能是 f(1)=1
别的没答案了吧!
加载更多回复(5)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧