P1049 譚浩強C語言(第三版)習題6.5 java import java.util. ; import java.io. ; import java.math.BigInteger; import java.lang.Character; public class Main { public ...
P1049 譚浩強C語言(第三版)習題6.5
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.lang.Character;
public class Main
{
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int a=cin.nextInt(),b=cin.nextInt(),c=cin.nextInt();
double ans=0;
for(int i=1;i<=a;++i)
{
ans+=i;
}
for(int i=1;i<=b;++i)
{
ans+=i*i;
}
for(int i=1;i<=c;++i)
{
ans=ans+1.0/i;
}
System.out.printf("%.2f\n", ans);
}
}
P1050 譚浩強C語言(第三版)習題6.6
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.lang.Character;
public class Main
{
public static void main(String args[])
{
for(int i=100;i<=999;++i)
{
int x=i/100, y=(i/10)%10, z=i%10;
if(x*x*x+y*y*y+z*z*z==i)
{
System.out.println(i);
}
}
}
}
P1051 譚浩強C語言(第三版)習題6.7
用了個比較複雜的結構,做的比較醜。
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.lang.Character;
public class Main
{
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
for(int k=1;k<=n;++k)
{
if(k==1) continue;
List<Integer> al=new ArrayList<Integer>();
for(int i=1;i*i<=k;++i)
{
if(k%i==0)
{
al.add(i);
al.add(k/i);
}
}
int sum=0;
for(Integer i: al)
sum+=i;
if(sum==k+k)
{
Collections.sort(al);
System.out.printf("%d its factors are ", k);
for(int i=0;i<al.size()-1;++i)
{
System.out.print(al.get(i));
if(i==al.size()-2) System.out.print(" ");
else System.out.print(" ");
}
System.out.println();
}
}
}
}
P1052 譚浩強C語言(第三版)習題6.8
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.lang.Character;
public class Main
{
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
int a=2,b=1,tmp;
double ans=0;
for(int i=1;i<=n;++i)
{
//System.out.printf("%d %d\n",a,b);
ans+=(double)a/b;
tmp=a;
a+=b;
b=tmp;
}
System.out.printf("%.2f\n", ans);
}
}
P1053 譚浩強C語言(第三版)習題6.9
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.lang.Character;
public class Main
{
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int m=cin.nextInt(),n=cin.nextInt();
double ans=0,now=m;
for(int i=1;i<=n;++i)
{
if(i==1) ans+=now;
else ans+=now*2;
now/=2.0;
}
System.out.printf("%.2f %.2f\n", now, ans);
}
}