HDU1001----1013
1001 Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 266690 Accepted Submission(s): 66333 Problem Description Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The input will consist of a series of integers n, one integer per line. Output For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer. Sample Input 1 100 Sample Output 1 5050
#include<iostream> #include<cmath> using namespace std; int main() { int i,n,sum; while(cin>>n) { if(n==0) sum=0; if(n==1) sum=1; else { if(n%2==0) sum=(1+n)*(n/2); if(n%2!=0) sum=(1+n)*(n/2)+(1+n)/2; } cout<<sum<<endl<<endl; } return 0; }
1004 Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 69200 Accepted Submission(s): 25681 Problem Description Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result. This year, they decide to leave this lovely job to you. Input Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters. A test case with N = 0 terminates the input and this test case is not to be processed. Output For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case. Sample Input 5 green red blue red red 3 pink orange pink 0 Sample Output red pink
#include<iostream> #include<cstring> #include<algorithm> #define N 10000 using namespace std; struct SS { char color[105]; int no; }f[N]; int cmp(SS a,SS b) { return strcmp(a.color,b.color)<0; } int comp(SS a,SS b) { return a.no>b.no; } int main() { int i,n; while(cin>>n&&n!=0) { for(i=0;i<n;i++) { cin>>f[i].color; f[i].no=1; } sort(f,f+n,cmp); for(i=1;i<n;i++) { if(strcmp(f[i].color,f[i-1].color)==0) { f[i].no+=f[i-1].no; } } sort(f,f+n,comp); cout<<f[0].color<<endl; } return 0; }
1005 Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 97633 Accepted Submission(s): 23419 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed. Output For each test case, print the value of f(n) on a single line. Sample Input 1 1 3 1 2 10 0 0 0 Sample Output 2 5
#include <iostream> using namespace std; int main(){ int a,b,c; int f[100]; int i; while(cin>>a>>b>>c,a+b+c){ f[1]=f[2]=1; for(i=3;i<100;i++){ f[i]=(f[i-1]*a+f[i-2]*b)%7; if(f[i]==1&&f[i-1]==1) break; //cout<<f[i-2]<<endl; } //考虑取余后为0的情况 cout<<( (c%(i-2))?(f[(c)%(i-2)]):(f[i-2]) )<<endl; } return 0; }
1008 Elevator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41022 Accepted Submission(s): 22460 Problem Description The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop. For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled. Input There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed. Output Print the total time on a single line for each test case. Sample Input 1 2 3 2 3 1 0 Sample Output 17 41
#include<iostream> #include<algorithm> using namespace std; int main() { int i,n,m,time; int a[200]; while(cin>>n&&n!=0) { a[0]=0; time=0; for(i=1;i<=n;i++) cin>>a[i]; for(i=1;i<=n;i++) { if(a[i-1]<a[i]) time+=(a[i]-a[i-1])*6; if(a[i-1]>a[i]) time+=(a[i-1]-a[i])*4; } cout<<time+n*5<<endl; } return 0; }
1012 u Calculate e Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28762 Accepted Submission(s): 12799 Problem Description A simple mathematical formula for e is where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n. Output Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below. Sample Output n e - ----------- 0 1 1 2 2 2.5 3 2.666666667 4 2.708333333
#include<stdio.h> int main() { int f[10]={1,1,2,6,24,120,720,5040,40320,362880}; int n; double e; printf("n e\n"); printf("- -----------\n"); printf("%d %d\n",0,1); printf("%d %d\n",1,2); printf("%d %.1f\n",2,2.5); e=2.5; for(n=3;n<=9;n++) { e+=1.0/f[n]; printf("%d %0.9f\n",n,e); } return 0; }
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46522 Accepted Submission(s): 14359 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<cstring> using namespace std; int main() { int i,n,m,len,sum; char a[100000]; while(cin>>a) { if(strcmp("0",a)==0) break; else { len=strlen(a); if(len==1) cout<<a<<endl; else if(len>1) {sum=0; for(i=0;i<len;i++) { sum=sum+(a[i]-'0'); } //cout<<sum%9<<endl; if(sum%9!=0) cout<<sum%9<<endl; if(sum%9==0) cout<<"9"<<endl; } } } return 0; }