HDU 1228
http://acm.hdu.edu.cn/showproblem.php?pid=1228
A + B
需要注意的是:A和B的每一位数字由对应的英文单词给出.
one + two = three four + five six = zero seven + eight nine = zero + zero =
3 90 96
/*strcmp函数,比较两个字符串
设这两个字符串为str1,str2,
若str1==str2,则返回零;
若str1>str2,则返回正数;
若str1<str2,则返回负数。*/
#include<stdio.h>
#include<string.h>
char f[10][6]= {"zero","one","two","three","four","five","six","seven","eight","nine"};
int search(char ch[])//将英文数字转化为阿拉伯数字
{
int i;
for(i=0; i<10; i++)
{
if(strcmp(ch,f[i])==0)
{
break;
}
}
return i;
}
int main (void)
{
char count[10];
int a,b;
while(1)
{
a=0;
while(scanf("%s",count)&&strcmp(count,"+")!=0)
{
a=a*10+search(count);//将a数字整合
}
b=0;
while(scanf("%s",count)&&strcmp(count,"=")!=0)
{
b=b*10+search(count);//将b数字整合
}
if(a==0&&b==0)
{
return 0;//a,b同时为零的时候退出
}
else
{
printf("%d\n",a+b);
}
}
return 0;
}
HDU 4548
美素数
问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数。
给定一个区间,你能计算出这个区间内有多少个美素数吗?
接下来共T行,每行输入两个整数L,R(1<= L <= R <= 1000000),表示区间的左值和右值。
每组数据占一行,具体输出格式参见样例。
3 1 100 2 2 3 19
Case #1: 14 Case #2: 1 Case #3: 4
#include<stdio.h>
#include<string.h>
int w[1000001];
int sushu(int n)//素数判定
{
int i;
if(n==1)
{
return 0;
}
else
{
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
}
int he (int n)//各个数字相加和
{
int z=0,s=0,j;
while(n!=0)
{
s+=n%10;
n=n/10;
}
return s;
}
int main(void)
{
int t,a,b,i,d,l;
memset(w,0,sizeof(w));
w[0]=0;
w[1]=0;
w[2]=1;
for(i=3;i<=1000000;i++)//放while外面,否则超时
{
l=he(i);
if(sushu(i)==1&&sushu(l)==1)
{
w[i]=w[i-1]+1;
}
else
{
w[i]=w[i-1];
}
}
scanf("%d",&t);
d=0;
while(t--)
{
d++;
scanf("%d%d",&a,&b);
printf("Case #%d: %d\n",d,w[b]-w[a-1]);
}
return 0;
}
HDU 2566
统计硬币
接下来的T行,每行有两个数n,m,n和m的含义同上。
每组输出占一行。
2 3 5 4 8
1 2
//简单数学题
#include<stdio.h>
int main(void)
{
int t,a,b,i,j,d;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
d=0;
for(i=0;i<=a;i++)
{
for(j=0;j<=a-i;j++)
{
if(b==i+2*j+(a-i-j)*5)
{
d++;
}
}
}
printf("%d\n",d);
}
return 0;
HDU 4515
小Q系列故事——世界上最遥远的距离
世界上最遥远的距离
不是生与死
而是我就站在你面前
你却不知道我爱你
世界上最遥远的距离
不是我就站在你面前你却不知道我爱你
而是明明知道彼此相爱
却不能在一起
世界上最遥远的距离
不是明明知道彼此相爱却不能在一起
而是相约好了私奔的时间
我穿越到了未来 你却回去了古代
——摘自《小Q失恋日记 》第117卷513页
当小Q使出浑身解数,终于赢得HR女神芳心的时候,却出现了一个意外情况,那就是白富美HR的妈妈并不同意他们交往,当听说小Q只是一个码农,特别是听说小Q曾经参加过资本主义国家发起的SM/ICPC比赛的时候,更是坚决反对!
爱情是伟大的,但是得不到亲人祝福的爱情却备受折磨,小Q和HR相约在腾讯第二届编程马拉松大赛进行到第5天的时候(即2013年3月24日),一起“向前穿越D天,然后开启幸福新生活”。
其勇气可谓令人赞叹,但可怜的小Q却总是备受折磨——小Q理解的”向前穿越”是朝着未来的方向,而女友HR理解的“向前穿越”却是朝着古代的方向!
假设已知现在的日期和穿越的天数D,你能计算出小Q和女友各自到达的年代吗?
接下来N行是N组数据,每一行包含一个正整数D(D<=10,0000),D表示向前穿越的天数。
2 6 30
2013/03/30 2013/03/18 2013/04/23 2013/02/22
//年月日的题目又臭又长 代码应该好懂 就是容易错
#include<stdio.h>
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
const int Y=2013,M=3,D=24;
int distinguish(int y)
{
if(y%400==0||(y%4==0&&y%100!=0))
return 1;
return 0;
}
void before(int k)
{
int y = Y,m = M,d = D;
while(k--)
{
if(distinguish(y))
a[2] = 29;
else
a[2] = 28;
d--;
if(d<=0)
{
m--;
if(m<=0)
{
m = 12;
y--;
}
d = a[m];
}
}
printf("%04d/%02d/%02d\n",y,m,d);
}
void after(int k)
{
int y = Y,m = M,d = D;
while(k--)
{
if(distinguish(y))
a[2] = 29;
else
a[2] = 28;
d++;
if(d>a[m])
{
m++;
if(m>12)
{
m = 1;
y++;
}
d = 1;
}
}
printf("%04d/%02d/%02d ",y,m,d);
}
int main(void)
{
int n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
after(m);
before(m);
}
return 0;
}
HDU 1274
展开字符串
该问题的描述是这样的:常用纱线的品种一般不会超过25种,所以分别可以用小写字母表示不同的纱线,例如:abc表示三根纱线的排列;重复可以用数字和括号表示,例如:2(abc)表示abcabc;1(a)=1a表示a;2ab表示aab;如果括号前面没有表示重复的数字出现,则就可认为是1被省略了,如:cd(abc)=cd1(abc)=cdabc;这种表示方法非常简单紧凑,也易于理解;但是计算机却不能理解。为了使计算机接受,就必须将简单紧凑的表达方式展开。某ACM队接受了此项任务。现在你就是该ACM队的一员,请你把这个程序编写完成。
已知条件:输入的简单紧凑表达方式的长度不超过250个字符;括号前表示重复的数不超过1000;不会出现除了数字、括号、小写字母以外的任何其他字符;不会出现括号不配对等错误的情况(错误处理已由ACM其他队员完成了)。
2 1(1a2b1(ab)1c) 3(ab2(4ab))
abbabc abaaaabaaaababaaaabaaaababaaaabaaaab
#include <stdio.h>
#include <string.h>
char input[250005];
char str[250005];
int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%s", input);
int len;
while (1)
{
int flag = 0;
int c = 0;
int i, j, k;
len = strlen(input);
for (i = 0; i < len; i++)
{
if (input[i] >= 'a' && input[i] <= 'z')
{
str[c++] = input[i];//字符
}
else if (input[i] >= '0' && input[i] <= '9')
{
int cnt = 0;
while (input[i] >= '0' && input[i] <= '9')
{
cnt *= 10;
cnt += input[i] - '0';
i++;
}
if (input[i] != '(')
{
while (cnt--)
{
str[c++] = input[i];
}
}
else
{
int t = 1;
int temp = i + 1;
while (t != 0)
{
if (input[temp] == '(')
{
++t;
}
if (input[temp] == ')')
{
--t;
}
++temp;
}
for (j = 0; j < cnt; ++j)
{
for (k = i + 1; k < temp - 1; ++k)
{
str[c++] = input[k];
if (input[k] == '(' || input[k] == ')' || (input[k] >= '0' && input[k] <= '9'))
{
flag = 1;
}
}
}
i = temp - 1;
}
}
}
str[c] = 0;
strcpy(input, str);
if (flag == 0)
{
break;
}
}
puts(str);
}
return 0;
}
codeforces 548c
Mike and Frog
Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time0), height of Xaniar is h1 and height of Abol ish2. Each second, Mike waters Abol and Xaniar.
So, if height of Xaniar is h1 and height of Abol ish2, after one second height of Xaniar will become and height of Abol will become wherex1, y1, x2 andy2 are some integer numbers and denotes the remainder ofa modulo b.
Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania isa1 and height of Abol isa2.
Mike has asked you for your help. Calculate the minimum time or say it will never happen.
The first line of input contains integer m (2 ≤ m ≤ 106).
The second line of input contains integers h1 anda1 (0 ≤ h1, a1 < m).
The third line of input contains integers x1 andy1 (0 ≤ x1, y1 < m).
The fourth line of input contains integers h2 anda2 (0 ≤ h2, a2 < m).
The fifth line of input contains integers x2 andy2 (0 ≤ x2, y2 < m).
It is guaranteed that h1 ≠ a1 andh2 ≠ a2.
Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.
5 4 2 1 1 0 1 2 3
3
1023 1 2 1 0 1 2 1 1
-1
In the first sample, heights sequences are following:
Xaniar:
Abol:
//题意:给一个青蛙和一朵花浇水,它们会按每分钟(h*x+y)mod m的速度长高,问它们分别长到a1和a2,最少需要多长时间。
//思路:注意是同时给它们两个浇水啊!!并且是同时长到a1和a2
#include<stdio.h>
int main()
{
__int64 h1,a1,x1,y1,h2,a2,x2,y2,m;
__int64 b1,c1,b2,c2;
b1 = c1 = b2 = c2 = -1;
int i;
while(scanf("%I64d",&m)!=EOF)
{
scanf("%I64d %I64d",&h1,&a1);
scanf("%I64d %I64d",&x1,&y1);
scanf("%I64d %I64d",&h2,&a2);
scanf("%I64d %I64d",&x2,&y2);
for(i=1; i<=2*m; i++)//循环节最大到m,估计的时候大点吧 2m
{
h1=(h1*x1+y1)%m;
if(h1==a1)
{
if(b1==-1)
{
b1=i;//最开始的达到a1的时间
}
else if(c1==-1)
{
c1=i-b1;//循环长度
}
}
h2=(h2*x2+y2)%m;
if(h2==a2)
{
if(b2==-1)
{
b2=i;
}
else if(c2==-1)
{
c2=i-b2;
}
}
}
if(b1==-1||b2==-1)
{
printf("-1\n");
}
else if(b1==b2)
{
printf("%I64d",b1);
}
else
{
for(i=1; i<=2*m; i++)
{ if(b1<b2)
{
b1=b1+c1;
}
else
{
b2=b2+c2;
}
if(b1==b2)
{
printf("%I64d\n",b1);
return 0;
}
}
printf("-1\n");
}
}
return 0;
}
POJ 1833
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 20083 | Accepted: 7721 |
Description
大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。
任务描述:
给出某个排列,求出这个排列的下k个排列,如果遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。
比如:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。
Input
Output
Sample Input
3 3 1 2 3 1 3 1 3 2 1 10 2 1 2 3 4 5 6 7 8 9 10
Sample Output
3 1 2 1 2 3 1 2 3 4 5 6 7 9 8 10
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[1025];
void next(int N)
{
int i,j,index=0;
bool flag = false;
for( i=N-2; i>=0; --i )
{
if( a[i]<a[i+1] )//找到第一个小于后一个数的数字
{
flag = true;
index = i;
break;
}
}
if( flag == false )//这个序列已经是递减的排列了,下一个肯定是递增的
{
sort(a, a+N);
return;
}
int temp = index+1;
//如果后面已经是递减了,那么就找到位于现在制定的两个数中间那个数换上来,并将后面按小到大排序
//例如1 2 4 3,假设现在index指向2,则temp指向4,i指向了3,因为3<4&&3>2所以将3与2交换,后面的按小到大排序
//因为这么做的前提是2后面是递减的,所以我们只要从最后找到一个最先位于index和tem之间的值即可,如此循环
for( i=N-1; i>=index+2; --i )
{
if( a[i]<a[temp] && a[i]>a[index])
{
temp = i;
break;
}
}
int change = a[index];
a[index] = a[temp];
a[temp] = change;
sort(a+index+1,a+N);
}
int main()
{
int T;
int N,K;
int i;
scanf("%d",&T);
while( T-- )
{
scanf("%d%d",&N,&K);
for( i=0; i<N; ++i )
scanf("%d",&a[i]);
while(K--)
next(N);
for( i=0; i<N; ++i )
{
if( i!=0)
printf(" ");
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}
下面是自己做法 比较简洁
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 1028
int f[maxn];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
scanf("%d", &f[i]);
while (k--)
next_permutation(f, f + n);
printf("%d", f[0]);
for (int i = 1; i < n; i++)
printf(" %d", f[i]);
printf("\n");
}
return 0;
}
HDU 1003
A. Watermelon
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 0 Accepted Submission(s) : 0
One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.
Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.
The first (and the only) input line contains integer number w (1≤w≤100) the weight of the watermelon bought by the boys.
Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.
8
YES
//题意:把一个分为两个偶数之和。假设能够分。输出YES 否则输出NO
//值得注意的这道题的背景是两个人分西瓜。所以每一个人至少分到一磅,so 0的情况就要排除掉,尽管0也是偶数
#include<stdio.h>
int main() {
int num;
scanf("%d", &num);
printf("%s\n", num % 2 == 0 && num != 2 ? "YES": "NO");
return 0;
}
codeforces 298A
There is a straight snowy road, divided into n blocks. The blocks are numbered from 1 to n from left to right. If one moves from the i-th block to the (i + 1)-th block, he will leave a right footprint on the i-th block. Similarly, if one moves from the i-th block to the (i - 1)-th block, he will leave a left footprint on the i-th block. If there already is a footprint on the i-th block, the new footprint will cover the old one.
At the beginning, there were no footprints. Then polar bear Alice starts from the s-th block, makes a sequence of moves and ends in thet-th block. It is known that Alice never moves outside of the road.
You are given the description of Alice's footprints. Your task is to find a pair of possible values of s, t by looking at the footprints.
The first line of the input contains integer n (3 ≤ n ≤ 1000).
The second line contains the description of the road — the string that consists of n characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint).
It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists.
Print two space-separated integers — the values of s and t. If there are several possible solutions you can print any of them.
9 ..RRLL...
3 4
11 .RRRLLLLL..
7 5
#include<stdio.h>
#include<string.h>
char str[1005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",str);
int i;
int len=strlen(str);
int flag=0,flag1=0;
int start,end;
for( i=0; i<len; i++)
{
if(str[i]=='R'&&flag==0)
{
flag=1;
start=i;
end=i;
}
if(str[i]=='R'&&str[i+1]=='L')
{
end=i;
break;
}
if(str[i]=='R' && str[i+1]=='.')
{
end=i+1;
break;
}
if(str[i]=='L'&&flag1==0)
{
flag1=1;
start=i;
end=i-1;
}
if(str[i]=='L'&&str[i+1]=='R')
{
start=i;
}
if(str[i]=='L' && str[i+1]=='.')
start=i;
}
printf("%d %d\n",start+1,end+1);
}
return 0;
}