62,614
社区成员
发帖
与我相关
我的任务
分享
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");
}
}
修改一下 呵呵 刚才忘记一个循环了 现在我想可以了!
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;
}
}
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;
}}
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");
}
}
}
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;
}
}