题目:
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.
For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么?这是小弟写的程序:public class Test12 {
public static void main(String[] args) {
int n = 2;
while(f(n) != n) {
++n;
}
System.out.println(n);
}
public static int f(int x) {
int sum =0;
for(int i=0; i<=x; i++) {
String s = Integer.toString(i);
for(int j=0; j<s.length(); j++) {
char c = s.charAt(j);
if(c == '1') {
++sum;
}
}
}
return sum;
}}我运行起来就一直没有显示结果了
不知是不是进入死循环了
请大家帮助解决一下
小弟先在这里谢谢大家了另外谁有更好更高效 的方法写写出来让小弟学习一下啊
谢谢了!!
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.
For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么?这是小弟写的程序:public class Test12 {
public static void main(String[] args) {
int n = 2;
while(f(n) != n) {
++n;
}
System.out.println(n);
}
public static int f(int x) {
int sum =0;
for(int i=0; i<=x; i++) {
String s = Integer.toString(i);
for(int j=0; j<s.length(); j++) {
char c = s.charAt(j);
if(c == '1') {
++sum;
}
}
}
return sum;
}}我运行起来就一直没有显示结果了
不知是不是进入死循环了
请大家帮助解决一下
小弟先在这里谢谢大家了另外谁有更好更高效 的方法写写出来让小弟学习一下啊
谢谢了!!
解决方案 »
- 多线程发送请求难题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`请教啊
- 如何让组件随着界面的拉伸而改变
- java.net包里datagrampacket的buffer只有8192字节,如何传输更大数据?
- 如何配制数据源
- assert怎么用??
- JAVA SE5为什么提出可变形参的概念?
- JDK与J2SDK是同样的软件吗?JDK现在最高版本是什么?各位觉得哪个版本最好?
- (高份请教)一个关于cloudscape的建库的问题。
- 怎么回事,,,帮帮菜鸟我。。。
- 关于j2sdkee的deploytool的初级问题,请指教!
- 小型图书管理系统如何增添书籍及其相关信息?
- 加急!单个Frame中显示多个jfreechart柱状图问题?
++n;
}
只要不等于就一直执行下去,肯定死循环了感觉应该有个限制,好像超过10以后就不可能再有f(n)=n了把,而且数字越大越不可能,最大的值可能就是n=1
没仔细想,大家发表下意见
/*
题目:
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.
For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么?
*/
public class FindN
{
public static void main(String[] args)
{
//System.out.println("-_-");
//todo
int n = 2;//从2开始找
int sum = 1;//f(1)=f(2)=f(3)=1
boolean ok = false;
while ((!ok)&&(n<1000000001))
{
n++;
//算数字n中包含的数码1的个数
String s = Integer.toString(n);
for(int i = 0;i < s.length();i++)
{
if (s.charAt(i) == '1')
{
sum++;
}
}
ok = (sum == n);
}
System.out.println("the number:" + n);
}
}
private static int f(int n)
{
int iCount = 0; int iFactor = 1; int iLowerNum = 0;
int iCurrNum = 0;
int iHigherNum = 0; while(n / iFactor != 0)
{
iLowerNum = n - (n / iFactor) * iFactor;
iCurrNum = (n / iFactor) % 10;
iHigherNum = n / (iFactor * 10); switch(iCurrNum)
{
case 0:
iCount += iHigherNum * iFactor;
break;
case 1:
iCount += iHigherNum * iFactor + iLowerNum + 1;
break;
default:
iCount += (iHigherNum + 1) * iFactor;
break;
} iFactor *= 10;
} return iCount;
}
你的复杂度是O(N*log2N)
而这个的复杂度是O(ln(n)/ln(10)+1),更好一些
* @param args
*/
static int sum=0;
public static void main(String[] args) {
int n=13;
count(n);
System.out.println(sum);
} private static void count(int n) {
if(n>0){
String nString=String.valueOf(n);
char[] chr=nString.toCharArray();
for(int i=0;i<chr.length;i++)
{
if('1'==chr[i])
sum++;
}
n--;
count(n);
} }}
package zgq;
/**
* 有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,
* 现在f(1)=1,问下一个最大的f(n)=n的n是什么?
* @author Administrator
*
*/
public class Test12 {
public static void main(String[] args) {
int n = 2;
int res=1;
while((getOnly(n)+res) != n) {
res+=getOnly(n);
++n;
}
System.out.println(n);
}
static String retStr(int num){
String s=num+"";
return s;
}
static int getOnly(int num){
int number=0;
int len=retStr(num).length();
String s=retStr(num);
if(len!=0){
for(int i=0;i<len;i++){
char a=s.charAt(i);
if(a=='1'){
number++;
}
}
}return number;
}
}
这样简洁些
public class Test12 {
public static void main(String[] args) {
int n = 2;
int res=1;
while((getOnly(n)+res) != n) {
res+=getOnly(n);
++n;
}
System.out.println(n);
}
static int getOnly(int num){
int number=0;
String s=num+"";
int len=s.length();
if(len!=0){
for(int i=0;i<len;i++){
char a=s.charAt(i);
if(a=='1'){
number++;
}
}
}return number;
}
}
你没给x赋值就对他进行赋值,可不一直循环下去被,给它赋值你试试,应该就可以了
没有错误,只是效率不怎么样
你可以用print的方式去看n和f(n)的变化,其实是一只在进行的,但是效率不佳,所以很慢
while(f(n) != n) {
System.out.println(n);
++n;
}
System.out.println(n+"finally");
}
int[][] aa = { { 68, 36, 22 }, { 59, 77, 39 }, { 81, 20, 17 } };
int [][]pos = new int[3][3];
for(int i=0;i<aa.length;i++){
for(int j=0;j<3;j++){
pos[i][j] = 1;
}
}
//找排序后的位置位置
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if( k != j && aa[k][i]<aa[j][i]){
pos[j][i]++;
}
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
System.out.print(pos[i][j]+"");
System.out.println();
}
}
如果是小于1则ABCDE ,
如E 则为十位 每出现一个1则多了10个1;
逐次考虑我觉得这样子,简化运行的效率了, 因为每次执行的知识循环数字的位数,剩下的就是与1的比较了!
int find()
{
int j =3;
int f_pre = 0;
int g_j =0;
f_pre = f(2);
g_j = g(3);
while(j!=f_pre+g_j)
{
f_pre +=g_j;
++j;
g_j = g(j);
}
return j;
}
分析下复杂度:
1,g()函数 取决于位数 :整数类型 24亿或者40多亿 <=10位
2,find()函数 跟n的大小有关,迭代n次。(n为要找的数)
3,总共 上界10*n ,为log(n)
4,用c的话 瞬间的事情(java通用)
....基于算法中常用的假设,要求f(n),则基于假设f(n-1)已知,利用前此跌打,减小复杂度
边界还望拍砖。