例题1
链接:点击打开链接
原题:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6648 | Accepted: 3627 |
Description
Write a program to convert octal numerals between 0 and 1, inclusive, into equivalent decimal numerals.
Input
Output
where the left side is the input (in octal), and the right hand side the decimal (base 10) equivalent. There must be no trailing zeros, i.e. Dm is not equal to 0.
Sample Input
0.75 0.0001 0.01234567
Sample Output
0.75 [8] = 0.953125 [10] 0.0001 [8] = 0.000244140625 [10] 0.01234567 [8] = 0.020408093929290771484375 [10]
Source
java.source:
import java.util.*;
import java.io.*;
import java.math.*;
public class da {
public static void main(String args[])
{
BigDecimal a,t,c,ans,tmp;
String s;
Scanner cin=new Scanner(new BufferedInputStream(System.in));
t=BigDecimal.valueOf(8);
while(cin.hasNextLine())
{
s=cin.nextLine();
tmp=t;
ans=BigDecimal.ZERO;
for(int i=2;i<s.length();i++)
{
c=BigDecimal.valueOf(s.charAt(i)-'0');
ans=ans.add(c.divide(tmp));
tmp=tmp.multiply(t);
}
System.out.println(s+" [8] = "+ans+" [10]");
}
}
}
例题2
链接:点击打开链接
原题:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 798 | Accepted: 478 |
Description
As part of a coordinated plan, a pipeline is setup in order to connect cities to the sewage treatment plants distributed along the river. It is more efficient to have treatment plants running at maximum capacity and less-used ones switched off for a period. So, each city has its own treatment plant by the river and also a pipe to its neighbouring city upstream and a pipe to the next city downstream along the riverside. At each city's treatment plant there are three choices:
- either process any water it may receive from one neighbouring city, together with its own dirty water, discharging the cleaned-up water into the river;
- or send its own dirty water, plus any from its downstream neighbour, along to the upstream neighbouring city's treatment plant (provided that city is not already using the pipe to send it's dirty water downstream);
- or send its own dirty water, plus any from the upstream neighbour, to the downstream neighbouring city's plant, if the pipe is not being used.
![](https://i-blog.csdnimg.cn/blog_migrate/5bd237d9d6060abda1c3e1367e2119d2.jpeg)
The choices above ensure that:
every city must have its water treated somewhere and
at least one city must discharge the cleaned water into the river.
Let's represent a city discharging water into the river as "V" (a downwards flow), passing water onto its neighbours as ">" (to the next city on its right) or else "<" (to the left). When we have several cities along the river bank, we assign a symbol to each (V, < or >) and list the cities symbols in order. For example, two cities, A and B, can
each treat their own sewage and each discharges clean water into the river. So A's action is denoted V as is B's and we write "VV" ;
or else city A can send its sewage along the pipe (to the right) to B for treatment and discharge, denoted ">V";
or else city B can send its sewage to (the left to) A, which treats it with its own dirty water and discharges (V) the cleaned water into the river. So A discharges (V) and B passes water to the left (<), and we denote this situation as "V<".
We could not have "><" since this means A sends its water to B and B sends its own to A, so both are using the same pipe and this is not allowed. Similarly "<<" is not possible since A's "<" means it sends its water to a non-existent city on its left.
So we have just 3 possible set-ups that fit the conditions:
A B A > B A < B V V V V RIVER~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~RIVER "VV" ">V" "V<"
If we now consider three cities, we can determine 8 possible set-ups.
Your task is to produce a program that given the number of cities NC (or treatment plants) in the river bank, determines the number of possible set-ups, NS, that can be made according to the rules define above.
You need to be careful with your design as the number of cities can be as large as 100.
Input
Output
Sample Input
2 3 20
Sample Output
3 8 102334155
Source
题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择
1、将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里
2、将左边来的污水连同自己的污水排到右边
3、将右边来的污水连同自己的污水排到左边
问总共有多少种处理情况,即不同又符合实际的><V组合。
思路:DP+高精度。DP部分,易得最右边城市的状态只可能用3种:>V, V, <。故分三种状态讨论,设dp[i][0]为第i个城市的状态为:> V ,dp[i][1]为:V ,dp[i][2]为:<。由实际流动的可能性可以得到状态转移方程:
dp[i][0] = dp[i-1][0] + dp[i-1][1];
dp[i][1] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2];
dp[i][2] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2];
然后可以整理为:dp[i] = 3 * dp[i-1] -dp[i-2]。然后用java的BigInteger预处理就OK了。
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static void main(String args[])
{
Scanner cin=new Scanner(new BufferedInputStream(System.in));
BigInteger dp[]=new BigInteger[111];
dp[1]=BigInteger.valueOf(1);
dp[2]=BigInteger.valueOf(3);
for(int i=3;i<=100;i++)
{
dp[i]=BigInteger.valueOf(3).multiply(dp[i-1]).subtract(dp[i-2]);
}
while(cin.hasNext())
{
int n;
n=cin.nextInt();
System.out.println(dp[n]);
}
}
}
例题3
链接:点击打开链接
原题:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4322 | Accepted: 1949 |
Description
{ 0-9,A-Z,a-z }
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number.
Input
Output
Sample Input
8 62 2 abcdefghiz 10 16 1234567890123456789012345678901234567890 16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 23 333YMHOUE8JPLT7OX6K9FYCQ8A 23 49 946B9AA02MI37E3D3MMJ4G7BL2F05 49 61 1VbDkSIMJL3JjRgAdlUfcaWj 61 5 dl9MDSWqwHjDnToKcsWE1S 5 10 42104444441001414401221302402201233340311104212022133030
Sample Output
62 abcdefghiz 2 11011100000100010111110010010110011111001001100011010010001 10 1234567890123456789012345678901234567890 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 333YMHOUE8JPLT7OX6K9FYCQ8A 35 333YMHOUE8JPLT7OX6K9FYCQ8A 23 946B9AA02MI37E3D3MMJ4G7BL2F05 23 946B9AA02MI37E3D3MMJ4G7BL2F05 49 1VbDkSIMJL3JjRgAdlUfcaWj 49 1VbDkSIMJL3JjRgAdlUfcaWj 61 dl9MDSWqwHjDnToKcsWE1S 61 dl9MDSWqwHjDnToKcsWE1S 5 42104444441001414401221302402201233340311104212022133030 5 42104444441001414401221302402201233340311104212022133030 10 1234567890123456789012345678901234567890
Source
题意:大数的进制转换。将a进制的正整数num转换为b进制的正整数。
思路:高精度,java,要注意为num = 0的情况。
题意思路注释转自:http://blog.sina.com.cn/s/blog_6635898a0100ovel.htmlMy java.source:
import java.io.*;
import java.text.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
int cas,i,w;
BigInteger sum,t,b1,b2;
char c;
cas=cin.nextInt();
while((cas--)!=0)
{
b1=cin.nextBigInteger();//
b2=cin.nextBigInteger();
String s=cin.next(); //
sum=BigInteger.valueOf(0);
t=BigInteger.valueOf(1);
for(i=s.length()-1;i>=0;i--)//
{
c=s.charAt(i);
if(c>='0'&&c<='9')
w=c-'0';
else if(c>='A'&&c<='Z')
w=c-'A'+10;
else
w=c-'a'+36;
sum=sum.add(BigInteger.valueOf(w).multiply(t));
t=t.multiply(b1);
}
int top=0;
int stack[]=new int[200];
while(sum.compareTo(BigInteger.valueOf(0))!=0)//
{
stack[++top]=sum.mod(b2).intValue();
sum=sum.divide(b2);
}
System.out.print(b1+" "+s+"\n"+b2+" ");
if(top==0)
System.out.print(0);//
while(top!=0)
{
w=stack[top--];
if(w<10)
c=(char)(w+'0');
else if(w<36)
c=(char)(w-10+'A');
else
c=(char)(w-36+'a');
System.out.print(c);
}
System.out.print("\n\n");
}
}
}
例题4
链接:点击打开链接
原题:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7129 | Accepted: 2645 |
Description
Relative #1 will get 1 / ... of the whole heritage,
Relative #2 will get 1 / ... of the whole heritage,
---------------------- ...
Relative #n will get 1 / ... of the whole heritage.
The logical desire of the relatives is to fill the blanks in such way that the uncle's will is preserved (i.e the fractions are non-ascending and the church gets something) and the amount of heritage left for the church is minimized.
Input
Output
Sample Input
2
Sample Output
2 3
Source
题意:将一份遗产分成n份,1/X1, 1/X2, 1/X3, ...,1/Xn。 使得X1 >= X2 >= X3 >= ... >= Xn,都为正整数。最后剩下的分给教堂,让其分得的财产最少,但不能没有。
思路:贪心+高精度。首先由贪心的思想可以知道,如果剩下1/6的遗产给最后一人和教堂,易得最后一人的得到的遗产为1/7。由这个思想可以列出 1/(Xn-1) = 1 - 1/X1 - 1/X2 -...- 1/X(n-1)。然后这个式子和X(n+1)的式子相消,化简,可得X(n+1) = Xn * (Xn - 1) + 1。然后就用java的大数A掉。
题意思路注释转自: http://blog.sina.com.cn/s/blog_6635898a0100ovom.htmlMy java source:
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main{
public static void main(String args[])
{
int i,n,cas;
BigInteger ans;
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
ans=BigInteger.valueOf(2);
n=cin.nextInt();
for(i=1;i<=n;i++)
{
System.out.println(ans);
ans=ans.multiply(ans.subtract(BigInteger.valueOf(1))).add(BigInteger.valueOf(1));
}
}
}
}
例题5
链接:点击打开链接
原题:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30091 | Accepted: 11718 |
Description
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)
Input
The final input line will contain a single zero on a line by itself.
Output
Sample Input
123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0
Sample Output
370370367037037036703703703670
Source
My java.source:
import java.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main{
public static void main(String args[])
{
BigInteger num,ans;
Scanner cin=new Scanner(System.in);
ans=BigInteger.valueOf(0);
while(cin.hasNext())
{
num=cin.nextBigInteger();
if(num.compareTo(BigInteger.valueOf(0))==0)
break;
ans=ans.add(num);
}
System.out.println(ans);
}
}
例题6
链接:点击打开链接
原题:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8747 | Accepted: 4645 |
Description
N N! 0 1 1 1 2 2 3 6 4 24 5 120 10 3628800
For this problem, you are to write a program that can compute the last non-zero digit of any factorial for (0 <= N <= 10000). For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 is the last nonzero digit of 120.
Input
Output
Sample Input
1 2 26 125 3125 9999
Sample Output
1 -> 1 2 -> 2 26 -> 4 125 -> 8 3125 -> 2 9999 -> 8
Source
java.source:
import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
public class Main {
public static void main(String args[])
{
BigInteger n,nn;
String s;
int i;
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
n=cin.nextBigInteger();
nn=BigInteger.valueOf(1);
for(i=2;i<=n.intValue();i++)
{
nn=nn.multiply(BigInteger.valueOf(i));
}
s=nn.toString();
for(i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)!='0')
{
break;
}
}
System.out.printf("%5d -> %c\n",n,s.charAt(i));
}
}
}