hdu水题习题集(3)
hdu-2563
统计问题
#include<stdio.h>
int f(int n)
{
if(n==0) return 0;
if(n==1) return 3;
if(n==2) return 7;
else return 2*f(n-1)+f(n-2);
}
int main()
{
int n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
printf("%d\n",f(m));
}//while(1);
return 0; 找规律,找到规律就容易解决了
}
hdu-2562
奇偶位互换
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,i,j,k,t;
char str[100];
char ch;
while(~scanf("%d",&n))
{
for(int r=1;r<=n;r++)
{
memset(str,'\0',sizeof(str));
scanf("%s",str);
k=strlen(str);
for(i=1;i<k;i+=2)
{
ch=str[i];
str[i]=str[i-1];
str[i-1]=ch;
}
printf("%s\n",str);
}
}
return 0; // 只需一次,就过了
}
hdu-2561
第二小整数
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,m,i,j;
int a[100];
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%d",&m);
for(int r=0;r<m;r++)
scanf("%d",&a[r]);
sort(a,a+m); 水题,一次就过。。。。。。。。。。
// for(int r=0;r<m;r++)
printf("%d\n",a[1]);
}
}//while(1);
return 0;
}
母牛的故事
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41885 Accepted Submission(s): 20813
n=0表示输入数据的结束,不做处理。
每个输出占一行。
2 4 5 0
2 4 6
int main()
{
int a[100],i,n;
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
for(i=5;i<=100;i++)
a[i]=a[i-1]+a[i-3]; // 只有看到发现的规律,我才会做,这是怎么回事。。。
while(~scanf("%d",&n),n!=0)
printf("%d\n",a[n]);
return 0;
}
hdu-3788
ZOJ问题
Total Submission(s): 2710 Accepted Submission(s): 825
是否AC的规则如下:
1. zoj能AC;
2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空;
3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空;
zoj ozojo ozoojoo oozoojoooo zooj ozojo oooozojo zojoooo
Accepted Accepted Accepted Accepted Accepted Accepted Wrong Answer Wrong Answer
#include<string.h>
int main()
{
int n,m,i,h,j,k,t;
int a,b,c;
char str[2000];
while(gets(str))
{
int flag=0;
int p1=0,p2=0;
a=b=c=0;
t=-1;
h=-1;
k=strlen(str);
for(i=0;i<k;i++)
{
if(str[i]=='z')
{
p2++;
t=i;
}
if(str[i]!='z'&&str[i]!='o'&&str[i]!='j')
flag=1;
}
for(i=k-1;i>=0;i--)
if(str[i]=='j')
{
p1++;
h=i;
}
if(t==-1||h==-1)
flag=1;
for(i=0;i<t;i++)
{
if(str[i]!='o')
break;
a++;
}
for(i=k-1;i>=h;i--)
{
if(str[i]!='o')
break;
c++;
}
for(i=t+1;i<h;i++)
{
if(str[i]!='o')
break;
b++;
}
if(flag==1)
{
printf("Wrong Answer\n");
continue;
}
if(c==a*b&&b>0&&p1==1&&p2==1)
printf("Accepted\n");
else
printf("Wrong Answer\n");
// printf("%d %d %d\n",a,b,c);
}
return 0; 这道题我过得十分不易。。。。。
}
{A} + {B}
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11814 Accepted Submission(s): 4933
注:同一个集合中不会有两个相同的元素.
1 2 1 2 3 1 2 1 1 2
1 2 3 1 2注意数组大小就行了。。。#include<stdio.h> #include<algorithm> using namespace std; int main() { int a[50000]; int n,m,i,j; while(~scanf("%d%d",&n,&m)) { for(i=0;i<n+m;i++) scanf("%d",&a[i]); sort(a,a+n+m); printf("%d",a[0]); for(i=1;i<n+m;i++) { if(a[i]==a[i-1]) continue; printf(" %d",a[i]); } printf("\n"); } return 0; }A C
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3776 Accepted Submission(s): 2413Problem DescriptionAre you excited when you see the title "AC" ? If the answer is YES , AC it ; You must learn these two combination formulas in the school . If you have forgotten it , see the picture.Now I will give you n and m , and your task is to calculate the answer .
InputIn the first line , there is a integer T indicates the number of test cases. Then T cases follows in the T lines. Each case contains a character 'A' or 'C', two integers represent n and m. (1<=n,m<=10)OutputFor each case , if the character is 'A' , calculate A(m,n),and if the character is 'C' , calculate C(m,n). And print the answer in a single line.Sample Input2 A 10 10 C 4 2
Sample Output3628800 6#include<stdio.h> int main() { int n,m,i,j,k,t; int num; char s; int a[13]; a[0]=1; a[1]=1; for(i=2;i<13;i++) a[i]=a[i-1]*i; scanf("%d",&t); getchar(); while(t--) { scanf("%s%d%d",&s,&n,&m); if(s=='A') { printf("%d\n",a[n]/a[n-m]); } if(s=='C') printf("%d\n",a[n]/a[n-m]/a[m]); } return 0; }真的感觉我好水啊。。。。。这道题我竟然没有一次通过hdu-2022海选女主角
#include<stdio.h> #include<math.h> int main() { int n,m; int a[100][100]; int i,j,max; while(~scanf("%d%d",&n,&m)) { int hang,lie; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) scanf("%d",&a[i][j]); } max=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) if(abs(a[i][j])>abs(max)) { max=a[i][j]; hang=i; lie=j; } } printf("%d %d %d\n",hang,lie,max); } return 0; }hdu-2099整除的尾数
#include<stdio.h> int main() { int a,b; int i,j,k,t; int n,m; while(~scanf("%d%d",&a,&b)) { if(a==0&&b==0) break; int sum; int r=0; for(i=0;i<=9;i++) { for(j=0;j<=9;j++) { sum=a*100+i*10+j; if(sum%b==0) { r++; if(r!=1) printf(" "); printf("%d%d",i,j); } } } printf("\n"); } return 0; }
hdu-1716
排列2
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int shu1[12]; int shu2[12]; int shu3[100]; int i,j,k,t; int r=0; while(~scanf("%d%d%d%d",&shu1[0],&shu1[1],&shu1[2],&shu1[3])){ if(shu1[0]==0&&shu1[1]==0&&shu1[2]==0&&shu1[3]==0) break; if(r!=0) printf("\n"); r++; sort(shu1,shu1+4); t=0; for(i=1000;i<10000;i++){ shu2[0]=i%10; shu2[1]=i/10%10; shu2[2]=i/100%10; shu2[3]=i/1000; sort(shu2,shu2+4); if(shu1[0]==shu2[0]&&shu1[1]==shu2[1]&&shu1[2]==shu2[2]&&shu1[3]==shu2[3]) shu3[t++]=i; } printf("%d",shu3[0]); for(i=1;i<=t-1;i++) { if(shu3[i]/1000==shu3[i-1]/1000) { printf(" "); printf("%d",shu3[i]); } else { printf("\n"); printf("%d",shu3[i]); } } printf("\n"); } return 0; }注意输出格式
hdu-1718
Rank
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int cmp(int a,int b){ return a>b; } int main() { int a[1200]; char s[1200][11]; while(~scanf("%s",s[0])){ int t=1; int i,j; int k; while(scanf("%s%d",s[t],&a[t])){ if(s[t][0]=='0') break; t++; } for(i=1;i<t;i++){ if(strcmp(s[i],s[0])==0){ k=a[i]; } } // printf("%d\n",k); sort(a+1,a+t,cmp); int rank; int r=0; for(i=1;i<t;i++){ // printf("%d ",a[i]); if(a[i]==k){ r++; rank=i; } } printf("%d\n",rank-r+1); } return 0; }
hdu-2097Sky数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13593 Accepted Submission(s): 7906Problem DescriptionSky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,啊哈,真是巧啊。Sky非常喜欢这种四位数,由于他的发现,所以这里我们命名其为Sky数。但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是Sky数吧。Input输入含有一些四位正整数,如果为0,则输入结束。Output若n为Sky数,则输出“#n is a Sky Number.”,否则输出“#n is not a Sky Number.”。每个结果占一行。注意:#n表示所读入的n值。Sample Input2992 1234 0
Sample Output2992 is a Sky Number. 1234 is not a Sky Number.此题为进制转换,比较简单#include<stdio.h> #include<string.h> #define MAXN 10000 int sky[MAXN]; int main() { int i,j,k; int h,t,x,y; int a,b,c; memset(sky,0,sizeof(sky)); for(i=2992;i<MAXN;i++){ h=t=k=i; a=b=c=0; while(k){ a+=k%10; k/=10; } while(t){ b+=t%12; t/=12; } if(a!=b) continue; while(h){ c+=h%16; h/=16; } if(a==b&&b==c){ sky[i]=1; } } int n; while(~scanf("%d",&n),n!=0){ if(sky[n]){ printf("%d is a Sky Number.\n",n); } else printf("%d is not a Sky Number.\n",n); } return 0; }hdu-1701-ACMer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3758 Accepted Submission(s): 1723Problem DescriptionThere are at least P% and at most Q% students of HDU are ACMers, now I want to know how many students HDU have at least?InputThe input contains multiple test cases. The first line has one integer,represent the number of test cases. The following N lines each line contains two numbers P and Q(P < Q),which accurate up to 2 decimal places.OutputFor each test case, output the minumal number of students in HDU.Sample Input1 13.00 14.10
Sample Output15题目解析:这道题如果是理解了题意的话,比较简单,但是应该注意的是整型,应该改为浮点型代码:#include<stdio.h>
int main()
{
int i,t;
double n,m;
scanf("%d",&t);
while(t--){
scanf("%lf %lf",&n,&m);
for(i=1;;i++){
if((int)(i*n/100)<(int)(i*m/100))
break;
}
printf("%d\n",i);
}
return 0;
}
hdu-1702ACboy needs your help again!
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3244 Accepted Submission(s): 1691
Problem DescriptionACboy was kidnapped!!
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
InputThe input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
OutputFor each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.
Sample Input4 4 FIFO IN 1 IN 2 OUT OUT 4 FILO IN 1 IN 2 OUT OUT 5 FIFO IN 1 IN 2 OUT OUT OUT 5 FILO IN 1 IN 2 OUT IN 3 OUT
Sample Output题目解析:1 2 2 1 1 2 None 2 3这道题目可以考虑使用栈,虽然我对栈才有一点点的了解。。。。。。代码如下:#include<stdio.h>
#include<string.h>
int main(){
int N,T;
int i,j,k,t;
int a[10000];
char str1[10],str2[10];
char zifu[4];
scanf("%d",&N);
while(N--){
int top=1,num=1;
scanf("%d %s",&t,str1);
for(int r=1;r<=t;r++){
scanf("%s",zifu);
if(strcmp(zifu,"IN")==0){
scanf("%d",&k);
a[top++]=k;
// printf("%d %d\n",top,a[top-1]);
}
else if(strcmp(zifu,"OUT")==0){
if(strcmp(str1,"FIFO")==0){
if(num<top)
printf("%d\n",a[num++]);
else
printf("None\n");
}
if(strcmp(str1,"FILO")==0){
if(top-1>=1)
printf("%d\n",a[--top]);
else
printf("None\n");
}
}
}
}
return 0;
}
hdu-1703PBD
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 783 Accepted Submission(s): 262
Problem DescriptionPrisonBreak is a popular TV programme in HDU. ACboy likes it very much, and he join a PrisonBreak discussing team called "PBD".Every Tuesday night, a lot of PBDers will contact with each other to discuss the newest plot of PrisonBreak season2. Generally speaking, every PBDer has distinct ideas about the play, so everyone want to know all the others' ideas. For example, when ACboy contract with Sam, ACboy will tell all the ideas he konws to Sam, and Sam will also tell all the ideas he konws to ACboy, and the call costs 5 yuan.
If there are N people in the "PBD" team, what is the minimum cost to let everyone knows all the others' ideas?
InputThe input contains multiple test cases.
Each test case contains a number N, means there are N people in the "PBD" team.N = 0 ends the input.(A call cost 5 yuan).
Outputfor each case, output a integer represent the minimum cost to let everyone knows all the others' ideas.
Sample Input1 2 3 4 0
Sample Output题目解析:0 5 15 20HintIf there are 2 people, for example, named A, B. Then A calls B, then A and B will know each other's ideas, so it only needs one call, so the minimum cost is 1*5 = 5 yuan.这是一道递推题目,当n>=5时,符合条件f(n)=f(n-1)+2;代码如下:#include<stdio.h>
#define MAX 1000000
int num[MAX];
int main(){
int N,i;
num[1]=0;
num[2]=1;
num[3]=3;
num[4]=4;
for(i=5;i<1000000;i++){
num[i]=num[i-1]+2;
}
while(~scanf("%d",&N),N!=0){
printf("%d\n",num[N]*5);
}
return 0;
}
hdu-1032:The 3n + 1 problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22381 Accepted Submission(s): 8364
Problem DescriptionProblems 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 is 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.
InputThe 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.
OutputFor 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 Input1 10 100 200 201 210 900 1000
Sample Output1 10 20 100 200 125 201 210 89 900 1000 174
3n+1问题,在这道题目中,需要注意的是,如果输入的两个数不是先小后大的话,要先交换两个数,但是输出的时候还是按输入的来说代码如下:#include<stdio.h>
int main(){
int n,m,k;
int num1,num2;
while(~scanf("%d %d",&n,&m)){
int t;
int i,j;
num1=n;
num2=m;
if(num1>num2){
t=num1;
num1=num2;
num2=t;
}
int max=0;
for(i=num1;i<=num2;i++){
int r;
r=i;
int ans=1;
while(r!=1){
if(r&1){
r=3*r+1;
ans++;
}
else{
r/=2;
ans++;
}
}
if(ans>max)
max=ans;
}
printf("%d %d %d\n",n,m,max);
}
return 0;
}
hdu-3787A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2856 Accepted Submission(s): 1619
Problem Description给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
Input输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
Output请计算A+B的结果,并以正常形式输出,每组数据占一行。
Sample Input-234,567,890 123,456,789 1,234 2,345,678
Sample Output题目分析:这道题目比较简单,一次就过。。。使用字符串就行-111111101代码如下:#include<stdio.h>
#include<string.h>
int main(){
char str1[10000];
char str2[10000];
int i,j,k,t;
int num1,num2;
while(~scanf("%s %s",str1,str2)){
int a=0;
int b=0;
int sum=0;
num1=strlen(str1);
num2=strlen(str2);
for(i=0;i<num1;i++){
if(str1[i]>='0'&&str1[i]<='9'){
a=a*10+(str1[i]-'0');
}
}
if(str1[0]=='-')
a*=(-1);
for(i=0;i<num2;i++){
if(str2[i]>='0'&&str2[i]<='9'){
b=b*10+(str2[i]-'0');
}
}
if(str2[0]=='-')
b*=(-1);
sum=a+b;
printf("%d\n",sum);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理