P1044 谭浩强C语言(第三版)习题5.8
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int x=cin.nextInt(),ans=0;
int[] thres={100000,100000,200000,200000,400000};
double[] rate={0.1, 0.075, 0.05, 0.03, 0.015};
for(int i=0;i!=5;++i)
{
if(x<thres[i])
{
ans+=x*rate[i]; x=0; break;
}
else
{
ans+=thres[i]*rate[i]; x-=thres[i];
}
if(x<=0) break;
}
if(x>=0) ans+=x*0.01;
System.out.println(ans);
}
}
P1045 谭浩强C语言(第三版)习题6.1
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
static int gcd(int x,int y)
{
if(y==0) return x;
else return gcd(y,x%y);
}
static int lcm(int x, int y)
{
return x/gcd(x,y)*y;
}
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt(),m=cin.nextInt();
System.out.println(String.valueOf(gcd(n,m))+" "+String.valueOf(lcm(n,m)));
}
}
P1046 谭浩强C语言(第三版)习题6.2
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);
String str=cin.nextLine();
int l=0, n=0, s=0, o=0;
for(int i=0; i!=str.length(); ++i)
{
char chr=str.charAt(i);
if(Character.isLetter(chr))
{
l++;
}
else if(Character.isDigit(chr))
n++;
else if(Character.isWhitespace(chr))
s++;
else o++;
}
System.out.printf("%d %d %d %d
",l,n,s,o);
}
}
P1047 谭浩强C语言(第三版)习题6.3
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 x=2;
long sum=0;
for(int i=1;i<=n;++i)
{
sum+=x;
x=x*10+2;
}
System.out.println(sum);
}
}
P1048 谭浩强C语言(第三版)习题6.4
注意数据范围。java的long
相当于c++的long long
。
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();
long x=1,sum=0;
for(int i=1;i<=n;++i)
{
x*=(long)i;
sum+=x;
}
System.out.println(sum);
}
}