杭电的答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

求余运算

给出S和M,求0*S%M,1*S%M,2*S%M......(M-1)*S%M能否组成一个集合包含
0.1.。。。M-1;(这个是原题意改造而来);
算法:判断两个数是否互质;or 暴力解决
其实暴力完全可以解决这个问题(⊙﹏⊙b),只是其中用数学方法更加高效,巧妙;
证明如果S和M互质则满足题意:
另G=gcd(S,M);则S=A*G,M=B*G;
另X=K*S%M=K*S-T*M(T为整数,满足X属于0到M-1);
X=K*A*G-T*B*G;因此取余后的整数一定是G的倍数,G只能取1才能满足条件;
充分性的证明:(即当S与M互质,则0到M-1的S倍对M取余一定能遍历0到M-1)
只需证明的是,该余数中两两之间互不相等;
假设k*S和b*S对M取余相等(k和b∈[0,M),并且k和b不等);
则k*S=q1*M+r=q2*M+r=b*S <==> (k-b)*S=M*(q1-q2);
S与M互质,由上式子可得M|(k-b),与k和b∈[0,M),并且k和b不等矛盾;
因此得证;
另外,偶然看到一个很牛叉的辗转相除法;
int gcd(int a,int b)
{
while(b) b^=a^=b^=a%=b;
return a;
}
此代码,很好很强大;把涉及位运算的交换的程序加入,便到得这段简洁高效的代码;
注:A和B;经过A^=B^=A^=B,结果就得到A和B的交换
//////////////////////////// 1000

#include <stdio.h>
int main()
{
int a,b,i,;
scanf("%d",&a);
for(i=1;i<=a;i++)

{ int sum=0;
sum=sum+i;

printf("%d\n",sum);
}
return 0;
};

1001;
#include"stdio.h"

int main()
{
unsigned _int64 n;
unsigned _int64 temp;
while(scanf("%I64u",&n)!=EOF) //是i 非L
{
temp=(1+n)*n/2;
printf("%I64u\n\n",temp);
}
return 0;

}


//////////////////
HDU ACM 1014 Uniform Generator 三月 22nd, /showproblem.php?pid=1014

这个题目是判断给定的步长和mod,判断所产生的随机数已经覆盖0~mod-1中所有的数,如果是,则说明所选的步长和mod是一个Good choice,否则为bad choice.

需要懂得的基本内容为线性同余产生随机数,链接:/zh-cn/%E7%B7%9A%E6%80%A7%E5%90%8C%E9%A4%98%E6%96%B9%E6%B3%95

Problem Description
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

seed(x+1) = [seed(x) + STEP] % MOD

where '%' is the modulus operator.

Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.

For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cy

cle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.

If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.

Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.



Input
Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).



Output
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each output test set, your program should print exactly one blank line.


Sample Input
3 5
15 20
63923 99999


Sample Output
3 5 Good Choice

15 20 Bad Choice

63923 99999 Good Choice


线性同余方法(LCG)是个产生伪随机数的方法。

它是根据递归公式:


其中A,B,M是产生器设定的常数。

LCG的周期最大为M,但大部分情况都会少于M。要令LCG达到最大周期,应符合以下条件:

B,M互质;
M的所有质因子的积能整除A ? 1;
若M是4的倍数,A ? 1也是;
A,B,N0都比M小;
A,B是正整数。
由以上可知,本题的求解方案。代码如下:
#include <stdio.h>
int main()
{
int a, b, max, min, tmp;
while (scanf("%d%d",&a,&b) != EOF)
{
max = a>b?a:b;
min = a<b?a:b;
while (min)
{
tmp = max%min;
max = min;
min = tmp;
}
if (max==1) printf("%10d%10d Good Choice\n\n", a, b);
else printf("%10d%10d Bad Choice\n\n", a, b);
}
return 0;

/
//////////////////

Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.

"But what is the characteristic of the special integer?" Ignatius asks.

"The i

nteger will appear at least (N+1)/2 times. If you can't find the rig
ht integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.

Can you find the special integer for Ignatius?



Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.



Output
For each test case, you have to output only one line which contains the special number you have found.



Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1


Sample Output
3
5
1

#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main(){
int n,i,x;
//freopen("a.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
vector<int>v;
map<int,int>m;
for(i=0;i<n;i++){
scanf("%d",&x);
m[x]++;
}
n=(n+1)/2;
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
if(it->second>=n)v.push_back(it->first);
}
sort(v.begin(),v.end());
if(v.size()>=1){
printf("%d",v[0]);
for(i=1;i<v.size();i++)printf(" %d",v[i]);
}
printf("\n");
}
return 0;
}

//////////////// 1048;
用100°的AC热情,挑战39°的高温——加油!
The Hardest Problem Ever
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5571 Accepted Submission(s): 2540


Problem Description
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar's army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is 'A', the cipher text would be 'F'). Since you are creating plain text out of Caesar's messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.



Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines sep

arating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line
- A single line, "START"

Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line - A single line, "END"

Following the final data set will be a single line, "ENDOFINPUT".



Output
For each data set, there will be exactly one line of output. This is the original message by Caesar.



Sample Input
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT


Sample Output
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE



#include <stdio.h>
#include <string.h>
int main()
{

char a[15];
char b[100];
char c[15];
int j;
for(;;)
{
gets(a);
if(!strcmp(a,"START"))
{
gets(b);
gets(c);
if(!(strcmp(c,"END")))
{
for(j=0;b[j]!='\0';j++)
{
if(b[j]>='F' && b[j]<='Z')
b[j]=b[j]-5;
else if(b[j]>='A' && b[j]<='E')
b[j]=b[j]+21;
}

}
printf("%s\n",b);
}
else if(!strcmp(a,"ENDOFINPUT"))
break;
}


return 0;
}

////////////

1018 Big Number

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8372 Accepted Submission(s): 3701


Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.


Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.


Output
The output contains the number of digits in the factorial of the integers appearing in the input.



Sample Input
2
10
20


Sample Output
7
19

#include<stdio.h>

#include<math.h>

#define pi 3.14159265

int num,result;

void JC()

{ double t;

t=(num*log(num) - num + 0.5*log(2*num*pi))/log(10); //经典的转换技巧

result = (int)t+1;

printf("%d\n",result);

}
int main()

{ int i,n;

scanf("%d",&n);

for( i=0 ; i<n ; i++ )
{
//求阶乘共用多少位,经典常用
scanf("%d",&num);

JC();
}
return 0;

}

////////


1013

Problem Description
The digital root of

a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the
digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.



Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.



Output
For each integer in the input, output its digital root on a separate line of the output.



Sample Input
24
39
0


Sample Output
6
3


#include <iostream>
#include <string>
using namespace std;

int Digit(int n)
{
int digit=0;
while(true)
{
digit=0;


while(n)
{
digit+=n%10; //这步很经典,常用数字的判断及各位数的存取。如123 ,可以得出, //百位为1,十位为2;等等!
n/=10;
}



if(digit<10) return digit;
n=digit;
}
}
int main()
{
int digit, sum, i;
string s;


while(cin>>s)
{
sum=0;
for(i=0; i<s.size(); ++i) //还在这里,求和,
sum+=s[i]-'0';

/*
while(cin>>s)
{

for(i=0; i<s.size(); ++i) //还在这里,求和,//求各个字符时,用这行也可以
sum+=s[i]-'0';
*/


if(sum==0) break;
digit=Digit(sum);
cout<<digit<<endl;
}
return 0;
}
/////////////

1032
Problem Description
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

Consider the following algorithm:


1. input n

2. print n

3. if n = 1 then STOP

4. if n is odd then n <- 3n + 1

5. else n <- n / 2

6. GOTO 2


Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this i

s called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.



Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.



Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).



Sample Input
1 10
100 200
201 210
900 1000


Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174

#include<iostream>
using namespace std;
int main()
{
int m,n,lenm,lenn;
while(cin>>m>>n)
{
int start=m,end=n,i,max=0;
if(start>end)
{
int temp=start;
start=end;
end=temp;
} / /if
for(i=start;i<=end;i++)
{
int count=1,temp=i;
while(temp!=1)
{
if(temp%2!=0)
temp=3*temp+1;
else
temp/=2;
count++;
} //while
if(max<count)
max=count;
// cout<<count<<" ";
} //for
cout<<m<<" "<<n<<" "<<max<<endl;
} //while
return 0;
}

///////
1040//必看的,也是 必会的!!
As Easy As A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12053 Accepted Submission(s): 4798

Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!


Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.



Output
For each case, print the sorting result, and one line one case.



Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9


Sample Output
1 2 3
1 2 3 4 5 6 7 8 9

#include<stdio.h>
void main()
{
int n,i,m,j,k,t,l,a[10000];
scanf("%d",&n);
for(i=0;i<

;n;i++)

{
scanf("%d",&m);
for(j=0;j<m;j++)
scanf("%d",&a[j]);
for(k=0;k<m;k++)
{
for(t=0;t<m-1;t++)
if(a[t]>a[t+1])
{
l=a[t];
a[t]=a[t+1];
a[t+1]=l;
}
}
for(j=0;j<m;j++)
{
printf("%d",a[j]);

if(j<m-1) //就这两步要小心谨慎,陷阱,有时不用多加换行服!!!
printf(" ");
}
printf("\n");
}
}

/////////////////
1042 N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17235 Accepted Submission(s): 4385


Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!



Input
One N in one line, process to the end of file.



Output
For each N, output N! in one line.



Sample Input
1
2
3


Sample Output
1
2
6

include<stdio.h>
#include<string.h>
int main()
{
int n,k,t,p,sum,j,i;
int s[40000];
while(scanf("%d",&n)!=EOF)
{
k=0;
t=0;
p=0;
sum=0;
s[0]=1;
for(j=1;j<=n;j++)
{
for(i=0;i<=t;i++)
{
sum=s[i]*j+p;
p=0;
if(sum>9)
{
s[i]=sum%10;
p=sum/10;
if(t==i)
{t++;
s[t]=0;} //看不明白
}
else
s[i]=sum;
}
}
for(i=t;i>=0;i--)
printf("%d",s[i]);
printf("\n");
}
return 0;
}
、、、、、、、、、、、、、、、、、

Gridland
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 981 Accepted Submission(s): 474


Problem Description
For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the “easy” problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the “hard” ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.

The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwe
st, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North–South or East–West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 × 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 × 3-Gridland, the shortest tour has

length 6.



Input
The first line contains the number of scenarios.

For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.



Output
The output for each scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.



Sample Input
2
2 2
2 3


Sample Output
Scenario #1:
4.00

Scenario #2:
6.00

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i,j,cas,mid,k;
double res,root=sqrt(2.0);
cin>>cas;
for(k=1; k<=cas; k++){
cin>>i>>j;
mid = i*j;
if(mid%2==0) res = double(mid);
else res = double(mid) - 1 + root;
printf("Scenario #%d:\n%.2lf\n\n",k,res);
}
return 0;
}
/////////////////
1 095
A+B for Input-Output Practice (VII)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11057 Accepted Submission(s): 7506


Problem DescriptionYour task is to Calculate a + b.
InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
OutputFor each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.
Sample Input1 5
10 20

Sample Output
6

30
include<iostream>using namespace std;
int main(){
int a,b;
while(cin>>a>>b)
{ cout<<a+b<<endl<<endl; //经典在这里
}
return 0;


////////////
1096 A+B for Input-Output Practice (VIII)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30197 Accepted Submission(s): 8895


Problem Description
Your task is to calculate the sum of some integers.



Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.



Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.



Sample Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3


Sample Output
10

15


6




include<iostream
using namespace std;int main()
{ int n,m,a[200],i,sum;
cin>>n;
while(n--) { sum=0;
cin>>m;
for(i=0;i<m;i++)

cin>>a[i];
for(i=0;i<m;i++) sum+=a[i];
if(n==0)cout<<sum<<endl;
else cout<<sum<<endl<<endl;
}
return 0;}


、、、、、、、、、、




第二个


//////////////////
1097

Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.



Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)



Output
For each test case, you should output the a^b's last digit number.



Sample Input
7 66
8 800


Sample Output
9
6

b的值最大有2^30
一次遍历下来当然会T咯

看看我的代码把

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int a, b;
int s[10][10] = {
{1, 0},
{1, 1},
{4, 2, 4, 8, 6},
{4, 3, 9, 7, 1},
{2, 4, 6}, //经典这步
{1, 5},
{1, 6},
{4, 7, 9, 3, 1},
{4, 8, 4, 2, 6},
{2, 9, 1}
};
while(2 == scanf("%d %d", &a, &b))
{
a %= 10;
b %= s[a][0];
if(!b)
b = s[a][0];
printf("%d\n", s[a][b]);
}
return 0;
}
、、、、、、、、、
1097
#include <iostream>
using namespace std;

int main()
{
int a,b;
while ( cin>>a>>b )
{
long temp,s,count=0;
int ibuf[10]={0};
int flag[10]={0}; temp = a % 10;
ibuf[++count] = temp;
flag[temp] = 1;
s = temp;
while(1)
{
s *= temp ;
s %= 10;
if (flag[s] == 0)
{
flag[s] = 1;
ibuf[++count] = s;
}
else
break;
}
ibuf[0] = ibuf[count];
temp = b %count;
cout<<ibuf[temp]<<"\n";
}
return 0;
}

////////////////////
1106

排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12006 Accepted Submission(s): 2842


Problem Description
输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若
干个‘0’组成的,这时这个整数就是0)。

你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。

Input
输入包含多组测试用例,每组输入数据只有一行数字(数字之间没有空格),这行数字的长度不大于1000。

输入数据保证:分割得到的非负整数不会大于100000000;输入数据不可能全由‘5’组成。


Output
对于每个测试用例,输出分割得到的整数排序的结果,相邻的两个整数之间用一个空格分开,每组输出占一行。



Sample Input
0051231232050775


Sample Output
0 77 12312320


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char ch[1006];
int a[1006]

,ind;
while(scanf("%s",ch+1)!=EOF)
{
ind=1;
int i=1,len=strlen(ch+1);
while(i<=len)
{
while(i<=len&&ch[i]=='5')
i++;
int temp=0;
while(i<=len&&ch[i]!='5')
{
temp=10*temp+ch[i]-'0';
i++;
}
if(ch[i-1]!='5')
a[ind++]=temp;
}
sort(a+1,a+ind);
printf("%d",a[1]);
for(int i=2;i<ind;i++)
printf(" %d",a[i]);
puts("");
}
}

////////////////////
1012

自己的代码
#include<iostream>
#include<cstdio>
using namespace std ;

long factor(int a)
{
long i,s=1;
for(i=1;i<=a;i++)
{ s*=i;
}
return s;

}


int main()
{
long double e=1;
int n,i;

cout<<"n"<<" "<<"e"<<endl;
cout<<"- -----------"<<endl;
for(i=1;i<10;i++)
{
e+=1/(long double)factor(i);
cout<<i<<" ";
if(i==8) printf("%.9f\n",e);

else printf("%.10g\n",e)

}

return 0;

}


//////////////////
1162
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1688 Accepted Submission(s): 758


Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzze,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?



Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of
file.



Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.



Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0


Sample Output
3.41


1162
#include<stdio.h>
#include<math.h>
#define INF 0x7FFFFFFF
#define MAX 101
double c[MAX][MAX];
double dis[MAX],visit[MAX];
typedef struct
{
double x;
double y;
}Point;
double distance(Point a,Point b)
{
double len;
len=sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
return len;
}
int main()
{
Point p[105];
int i,j,k,n;
double sum,l;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=

n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(i==j)c[i][j]=0; //对a[][]进行初始化,一般都要;
else c[i][j]=INF;
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
l=distance(p[i],p[j]);
c[i][j]=c[j][i]=l;
}
for(i=1;i<=n;i++)
{
dis[i]=c[1][i];
visit[i]=0;
}
visit[1]=1;
sum=0;
for(i=1;i<n;i++)
{
double min=1.0*INF;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]<min)
{
min=dis[j];
k=j;
}
sum+=min;
visit[k]=1;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]>c[j][k])dis[j]=c[j][k];
}
printf("%.2lf\n",sum);
}
return 0;
}

今天一口气刷了6道最小生成树。。。。

、、、、、、、
HDU1322还是畅通工程

#include<stdio.h> //prim算法
#define INF 0x7FFFFFFF
#define MAX 101
int c[MAX][MAX];
int dis[MAX],visit[MAX];
int main()
{
int i,j,k,n,m,x,y,z,sum;
while(scanf("%d",&n)!=EOF&&n)
{
m=n*(n-1)/2;//n表示城市数,m表示路的数;
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(i==j)c[i][j]=0; //对a[][]进行初始化,一般都要;
else c[i][j]=INF;
}
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
if(c[x][y]>z)
c[x][y]=c[y][x]=z;
}//输入路径
for(i=1;i<=n;i++)
{
dis[i]=c[1][i]; //dis[]以1为起点进行初始化
visit[i]=0; //s[]用于标记
}
visit[1]=1;
sum=0;
for(i=1;i<n;i++)
{
int min=INF;
for(j=1;j<=n;j++) //选出最小的边;
if(visit[j]==0 && dis[j]<min)
{
min=d
is[j];
k=j;
}
sum+=min;
visit[k]=1;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]>c[j][k])dis[j]=c[j][k];
}
printf("%d\n",sum);
}
return 0;
}
/////////////////////
1170
Balloon Comes!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8542 Accepted Submission(s): 2843


Problem Description
The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very... easy problem.
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!



Input
Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.



Output
For each case, print the operation result. The result shoul

d be rounded to 2 decimal places If and only if it is not an integer.



Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2


Sample Output
3
-1
2
0.50



HDU ACM 1170 Balloon Comes! 三月 19th, 2010题目链接:/showproblem.php?pid=1170



这个题目主要是给定n组输入,每组输入包含一个操作符,两个操作数,求进行操作之后的结果。

有两个地方需要考虑:

1、每次输入的时候要将末尾的换行符处理掉,因为用scanf操作之后,仍剩下\n等在输入流中,需要将其处理掉。

2、在进行除法操作的时候,主要整除的时候输出整数,不能整除时候保留小数点后两位小数。



代码如下:


#include <stdio.h>
int main()
{
char c = 'c';int a,b;
int n;
scanf("%d%*c",&n);

while ( n-- )
{

scanf("%c%d%d%*c",&c,&a,&b);
if ( c == '+' )
{printf("%d\n",a+b);

}
if ( c == '-' )
{
printf("%d\n",a-b);

}

if ( c == '*' )

{printf("%d\n",a*b);}

if ( c == '/' )

{if ( a%b == 0 )

{printf("%d\n",a/b);}

else
printf("%.2f\n",a/(1.0*b));

}

}

return 0;



////////////////
HDU ACM 1060
Leftmost Digit 三月 22nd, 2010题目链接:Leftmost Digit
Time Limit: 200
0/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4032 Accepted Submission(s): 1441


Problem Description
Given a positive integer N, you should output the leftmost digit of N^N.



Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).



Output
For each test case, you should output the leftmost digit of N^N.



Sample Input
2
3
4


Sample Output
2
2

Hint
In the first case, 3 * 3 * 3 = 27, so the leftmost digit is 2.
In the second case, 4 * 4 * 4 * 4 = 256, so the leftmost digit is 2.

本题目主要是给定一个正整数n,计算n^n的结果的最高数位上的数字的值。

如果通过求n^n的最后结果再求最高数位的数字,方法简单,但是存在问题,就是n较大时候,n^n太大而无法用程序存储,需要设计大数的存储方案,且带来了计算过程中的难度而且消耗大量内存空间和时间。在本题中不可取。

另外一种方法,设n^n = d.xxx * 10^(k-1),其中k表示n^n的位数。

那么d.xxx = 10^(log10(n^n)-(k-1)),再对d.xxx取整即可获得最终结果。那么k是多少呢?

k = log10(n^n)的整数部分+1 = (int)log10(n^n)+1;至此,可以获得d的计算公式为

d = (int)(10^(log10(n^n)-(int)log10(n^n));

题目可解,代码如下:
#include <stdio.h>
#include <math.h>

int main()

{
int t,n;

double x = 0.0;

scanf("%d",&t);

while ( t-- )

{

scanf("%d",&n);

x = n*log10( (double)n );

x -= (__int64)x;

x = (int)pow(10,x);

printf("%.0lf\n",x);

}

return 0;

}

///////////////////
1071
The area
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2720 Accepted Submission(s): 1762


Problem Description
Ignatius bought a land last week, but he didn't know the area of the land because the land is enclosed by a parabola and a straight line. The picture below shows the area. Now given all the intersectant points shows in the picture, can you tell Ignatius the area of the land?

Note: The point P1 in the picture is the vertex of the parabola.


Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains three intersectant points which shows in the picture, they are given in the order of P1, P2, P3. Each point is described by two floating-point numbers X and Y(0.0<=X,Y<=1000.0).



Output
For each test case, you should output the area of the land, the result should be rounded to 2 decimal places.



Sample Input
2
5.000000 5.000000
0.000000 0.000000
10.000000 0.000000
10.000000 10.000000
1.000000 1.0000
00
14.000000 8.222222


Sample Output
33.33
40.69

Hint
For float may be not accurate enough, please use double instead of float

求斜线部分的面积。

可令抛物线表达式为y= a(x-x1)^2+y1;

直线表达式为y=kx+b;

面积可通过微积分来计算a(x-x1)^2+y1 -(kx+b)在x2到x3区间内的定积分的值则为面积结果。

至此,题目可解。

代码如下:

#include <stdio.h>

int main()

{

int n;

scanf("%d",&n);

while(n--)

{

double x0,y0,x1,y1,x2,y2,area=0;

scanf("%lf %lf %lf %lf %lf %lf",&x0,&y0,&x1,&y1,&x2,&y2);

area=(y0-y1)*(x2-x1)+(y2-y0)*(x2-x0)/3.0-(y1-y0)*(x1-x0)/3.0-(y2-y1)*(x2-x1)/2.0;

printf("%.2lf\n",area);

}

return 0;

}
///////////
题目非常简单,主要是给定两个数n,m;求0<a<b<n,满足(a*a+b*b+m)/(a*b)的正数对a,b一共有多少组。

题目没有难度,也没有其他需要考虑的知识点。唯一要注意的是输出的格式。



代码如下:


题目非常简单,主要是给定两个数n,m;求0<a<b<n,满足(a*a+b*b+m)/(a*b)的正数对a,b一共有多少组。

题目没有难度,也没有其他需要考虑的知识点。唯一要注意的是输出的格式。



代码如下:

#include <stdio.h>
int main()
{

int n = 0, m = 0, t, num ,a,b;

int N = 0;

scanf("%d",&N);

while ( N-- )

{


t = 0;

while ( scanf("%d%d",&n,&m) != EOF && n != 0 )

{

num = 0;
for ( int a = 1; a < n; a++ )
{

for ( int b = a+1; b < n; b++ )
{
if ( (a*a+b*b+m)%(a*b) == 0 )

{
++num;
}

}

}

t++;
printf("Case %d: %d\n",t,num);
}

if (N)
{

printf("\n");
}
}
return 0;
}
////////////////////1133

Problem Description
The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you?

Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).

Now the problem for you is to calculate the number of different ways of the queue that the buying process won't be stopped from the first person till the last person.
Note: initially the ticket-office has no money.

The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.



Input
The input file contains several test cases. Each test case is made up of two integer numbers: m and n. It is terminated by m = n = 0. Otherwise, m, n <=100.



Output
For each test case, first print the test number (counting from 1) in one line, then output the number of different ways in another line.



Sample Input
3 0
3 1
3 3
0 0


Sample Output
Test #1:
6
Test #2:
18
Test #3:
180



考虑(m+n)个人排队购票的情景,第(m+n)人站在第(m+n-1)个人的后面,则第(m+n )个人的排队方式可以由下列两种情况获得:
a.第(m+n )个人手持¥100的钞票,则在他之前的(m+(n-1))个人中有m个人手持¥50的钞票,有(n-1)个人手持¥100的钞票,此种情况共有f(m,n-1);
b.第(m+n )个人手持¥50的钞票,则在他之前的((m-1)+n)个人中有m-1个人手持¥50的钞票,有n个人手持¥100的钞票,此种情况共有f(m-1,n);
根据加法原理得到: f(m,n)=f(m-1,n)+f(m,n-1)
最终得到递推公式:f(m,n)= C(m+n,n)-C(m+n,m+1)
即:f(m,n) = ((m+1-n) / (m+1)) *((m+n)!) 阅读全文

#include<iostream>
using namespace std;
int main()
{
int i,j,len=1,r=0,temp=0;
int a[201][400]={0};
a[1][0]=1;
for(i=2;i<=200;i++)//求1……200的阶乘
{
for(j=0;j<len;j++)
a[i][j] = a[i-1][j] * i;
for(j=0,r=0;j<len;j++)
{
temp = a[i][j] + r;
a[i][j] = temp % 10;
r = temp /10;
}
while(r)
{
a[i][len++] = r%10;
r /= 10;
}
}
int m,n,cas=1;
while(cin>>m>>n)
{
if(m==0 && n==0)
break;
cout<<"Test #"<<cas++<<":"<<endl;
if(n>m)
{
cout<<0<<endl

;
continue;
}
r = 0;
len = 400;//改成300就错了
int b[500]={0};
for(j=0;j<len;j++)
{
temp = a[m+n][j]*(m+1-n) + r;
b[j] = temp % 10;
r = temp /10;
}
while(r)
{
b[len++] = r%10;
r /= 10;
}

for(j=len-1,r=0;j>=0;j--)
{ //除法从高位到低位
temp=r*10+b[j];
b[j]=temp/(m+1);
r=temp%(m+1);
}
while(!b[len]) //处理高位的零位
len--;
for(j=len;j>=0;j--)
cout<<b[j];
cout<<endl;
}
return 0;
}

/////////////////
1114

Piggy-Bank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1579 Accepted Submission(s): 789


Problem Description
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

But there is a big problem wit
h piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!



Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.



Output
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the g

iven total weight. If the weight cannot be reached exactly, print a line "This is impossible.".



Sample Input
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4


Sample Output
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.


#include<iostream>
struct node
{
int w;
int v;
}tt[10001];
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int i,j,k=1;
int c,e,f,n;
int dp[10001];//dp[i]表容量为i的时候所装东西的最小价值//w[10001],v[10001],
scanf("%d%d",&e,&f);
c = f - e;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&e,&f);
tt[k].w = f;
tt[k].v = e;
while(1)
{
k ++;
if(tt[k-1].w*2 > c)
break;
tt[k].w = tt[k-1].w * 2;
tt[k].v = tt[k-1].v * 2;
}
}
for(i=1;i<=c;i++)
dp[i] = 1000000;
dp[0] = 0;//背包容量为 0 啥也不能装
for(i=1;i < k;i++)
{
for(j=tt[i].w;j<=c;j++)
{
if(dp[j] > dp[j-tt[i].w] + tt[i].v)

dp[j] = dp[j-tt[i].w] + tt[i].v;
}
}
if(dp[c] == 1000000)
cout<<"This is impossible."<<endl;
else
cout<<"The minimum amount of money in the piggy-bank is "<<dp[c]<<"."<<endl;
}
return 0;
}
///////////////////
hutc 1041 最优装载Description

有一批集装箱要装上一艘载重量为c的轮船。其中集装箱i的重量为Wi。最优装载问题要求确定在装载体积不受限制的情况下,将尽可能多的集装箱装上轮船。
编程任务:
对于给定的n个集装箱和轮船的载重量C,编程计算装入最多时的集装箱个数。

Input

输入由多组测试数据组成。
每组测试数据输入的第1行中有2个正整数n和C。正整数n是集装箱个数;正整数C是轮船的载重量。接下来的一行中有n个整数,分别表示n个集装箱的重量,它们之间用空格分隔。

Output

对应每组输入,输出的每行是计算出的装入最多时的集装箱个数。

Sample Input


4 5
3 5 2 1


Sample Output
2



#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
struct Node {
int weight;
friend bool operator <(Node a,Node b)
{ return a.weight > b.weight; }}
;int main(){
int n,c,num,sum;
Node p; while(cin>>n>>c)
{ priority_queue<Node>Q;
while(n--) { scanf("%d",&p.weight); Q.push(p);
}
num = sum = 0;
while(!Q.empty()) {
p = Q.top();
Q.pop();
sum += p.weight;
num ++;
if(sum > c)
{ num --;
break; } }
cout<<num<<endl;
} return 0;}
////////////////
1023


Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.



Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.



Output
For each test case, you should output how many ways that all the trains can get out of the railway.



Sample Input
1
2
3
10


Sample Output
1
2
5
16796

HintHint
The result will be very large, so you may not process it by 32-bit integers.

//C(0) = 1 ; (n+2)*C(n+1) = (4n+2)*C(n);
#include<iostream>
using namespace std;
int a[101][101]={0};
int main()
int n,i,j,len,r,temp,t;
int b[101];
a[1][0] = 1;
len = 1;
b[1] = 1;
for(i=2;i<=100;i++)
{
t = i-1;
for(j=0;j<len;j++) //乘法
a[i][j] = a[i-1][j]*(4*t+2);
for(r=j=
0;j<len;j++) //处理相乘结果
{
temp = a[i][j] + r;
a[i][j] = temp % 10;
r = temp / 10;
}
while(r) //进位处理
{
a[i][len++] = r % 10;
r /= 10;
}

for(j=len-1,r=0;j>=0;j--) //除法
{
temp = r*10 + a[i][j];
a[i][j] = temp/(t+2);
r = temp%(t+2);
}
while(!a[i][len-1]) //高位零处理
len --;
b[i] = len;
}
while(cin>>n)
{
for(j=b[n]-1;j>=0;j--)
printf("%d",a[n][j]);
printf("\n");
}
return 0;
}
////////////////////
/Oliver的救援
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交:233 测试通过:94
描述

在你的帮助下,Oliver终于追到小X了,可有一天,坏人把小X抓走了。这正是Oliver英雄救美的时候。所以,Oliver又找到哆啦A梦,借了一个机器,机器显示出一幅方格地图,它告诉Oliver哪里能走,哪里不能走,。并且Oliver在这个地图的右下角,而小X在左上角。时间紧急,Oliver想知道,最少要走多少个格子,才能找到小X。(只能直走)。

输入

共N+1行,第一行为N,以下N行N列0-1矩阵,1表示不能通过,0表示可以通过(左上角和右下角为0). N<30.

输出

共一个数,为最少的走的格子数.

样例输入

5
0 1 1 1 1
0 0 1 1 1
1 0 0 0 1
1 1 1 0 1
1 1 1 0 0


样例输出

9


题目来源

Oliver

#include <stdio.h>
typedef struct point_type{
char x;
char y;
}point_type;
point_type queue[901];
int head, tail;
bool s[31][31];
char visit[31][31];
char layer[31][31];
int n;
bool isvalid(int x, int y)
{
return x>=0&&x<n&&y>=0&&y<n&&visit[x][y]=='0'&&s[x][y]==0;
}
void BFS(int x, int y)
{
int a, b;
a = x; b = y+1;
if (isvalid(a, b)){
queue[tail].x = a;
queue[tail].y = b;
tail++;
visit[a][b] = '1';
layer[a][b] = layer[x][y]+1;
}
a = x+1; b = y;
if (isvalid(a, b)){
queue[tail].

x = a;
queue[tail].y = b;
tail++;
visit[a][b] = '1';
layer[a][b] = layer[x][y]+1;
}
a = x; b = y-1;
if (isvalid(a, b)){
queue[tail].x = a;
queue[tail].y = b;
tail++;
visit[a][b] = '1';
layer[a][b] = layer[x][y]+1;
}
a = x-1; b = y;
if (isvalid(a, b)){
queue[tail].x = a;
queue[tail].y = b;
tail++;
visit[a][b] = '1';
layer[a][b] = layer[x][y]+1;
}
}
int main()
{
int i, j;
while (scanf("%d", &n) == 1){
for (i=0; i<n; i++)
for (j=0; j<n; j++)
scanf("%d", &s[i][j]);
for (i=0; i<n; i++){
for (j=0; j<n; j++){
vis
it[i][j] = '0';
layer[i][j] = -1;
}
visit[i][n] = '\0';
}
visit[n-1][n-1] = '1';
layer[n-1][n-1] = 0;

head = 0; tail = 0;
queue[tail].x = n-1;
queue[tail].y = n-1;
tail++;
while (1){
if (head == tail) break;
if (queue[head].x==0 && queue[head].y==0) break;
BFS(queue[head].x, queue[head].y);
head++;
}
printf("%d\n", layer[0][0]+1);
}
return 0;
}

////////
杭电ACM1279 验证角谷猜想2010-03-23 11:18验证角谷猜想
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1174 Accepted Submission(s): 620


Problem Description数论中有许多猜想尚未解决,其中有一个被称为“角谷猜想”的问题,该问题在五、六十年代的美国多个著名高校中曾风行一时,这个问题是这样描述的:任何一个大于一的自然数,如果是奇数,则乘以三再加一;如果是偶数,则除以二;得出的结果继续按照前面的规则进行运算,最后必定得到一。现在请你编写一个程序验证他的正确性。
Input本题有多个测试数据组,第一行为测试数据组数N,接着是N行的正整数。
Output输出验证“角谷猜想”过程中的奇数,最后得到的1不用输出;每个测试题输出一行;每行中只有两个输出之间才能有一个空格;如果没有这样的输出,则输出:No number can be output !。
Sample Input4
5
9
16
11

Sample Output5
9 7 11 17 13 5
No number can be output !
11 17 13 5

AuthorCai Minglun
Source杭电ACM集训队训练赛(VI)
Recommendlcy

#include <stdio.h>
int main()
{
int z, n;
char b;
scanf("%d", &z);
while (z--){
scanf("%d", &n);
b = 0;
while (n != 1){
if (n&1){
if (b) printf(" ");
b = 1;
printf("%d", n);
n = n*3+1;
}
else{
n >>= 1;
}
}
if (!b) printf("No number can be output !");
printf("\n");
}
return 0;
}

/////////////////
ACM1860 统计字符2010-01-28 23:43统计字符
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2509 Accepted Submission(s): 1546


Problem Description统计一个给定字符串中指定的字符出现的次数
Input

测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
Output对每个测试用例,统计第1行
中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
c0 n0
c1 n1
c2 n2
...
其中ci是第1行中第i个字符,ni是ci出现的次数。
Sample InputI
THIS IS A TEST
i ng
this is a long test string
#

Sample OutputI 2
i 3
5
n 2
g 2
注:第2个测试用例中,空格也是被统计的字符之一。

#include <stdio.h>
#include <string.h>
int main()
{
char c[6], s[81];
int a[5], i, j, len;
while (1){
gets(c);
if (strcmp(c, "#") == 0) break;
gets(s);
len = strlen(c);
for (i=0; i<len; i++){
a[i] = 0;
for (j=strlen(s)-1; j>=0; j--)
if (c[i] == s[j]) a[i]++;
}
for (i=0; i<len; i++)
printf("%c %d\n", c[i], a[i]);
}
return 0;
}

///////////////////////
杭电ACM1328 IBM Minus One2010-01-28 22:41IBM Minus One
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1141 Accepted Submission(s): 574


Problem DescriptionYou may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)

After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get ... IBM.

Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.
InputThe input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.
OutputFor each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.

Print a blank line after each test case.
Sample Input2
HAL
SWERC

Sample OutputString #1
IBM

Str

ing #2
TXFSD

SourceSouthwestern Europe 1997, Practice
RecommendIgnatius.L

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int y, z, i, len;
char s[51], c;
scanf("%d", &z);
for (y=1; y<=z; y++){
scanf("%s", s);
len = strlen(s);
printf("String #%d\n", y);
for (i=0; i<
len; i++){
c = s[i];
printf("%c", c=='Z' ? 'A' : c+1);
}
printf("\n\n");
}
system("pause");
return 0;
}

///////////////////
杭电ACM1128 Self Numbers2010-01-28 21:36Self Numbers
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1937 Accepted Submission(s): 831


Problem DescriptionIn 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence
33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ...

The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.


Write a program to output all positive self-numbers less than or equal 1000000 in increasing order, one per line.
Sample Output1
3
5
7
9
20
31
42
53
64
|
| <-- a lot more numbers
|
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993
|
|
|

SourceMid-Central USA 1998
RecommendEddy

#include <stdio.h>
#include <stdlib.h>
char b[1000000];
int f(int n)
{
int r = n;
while (n){
r += n%10;
n /= 10;
}
return r;
}
int main()
{
int i, j, t;
int MAX = 1000000;
for (i=1; i<=MAX; i++){
b[f(i)] = 1;
}
for (i=1; i<=MAX; i++)
if (!b[i]) printf("%d\n", i);
system("pause");
return 0;
}
///////
本题意为将n个单词首尾相连组成一个单词链,如:acm->malform->mouse(a -> m -> m -> m ->m -> e)
// 并查集+欧拉路实现
// 1.并查集判连通,这点不用多说
// 2.欧拉路,由图中可知除二端点外,其余字母的入度和出度均相等,二端点的出度
//和入度相差1,还有一种可能是,整个图就是一个欧拉回路,此时每个端点的入

相关文档
最新文档