ZOJ1926
猜数游戏,判断S有没有说谎
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n,a,b,c;
char ch[10];
while(~scanf("%d",&n)&&n)
{
a=0;b=11;//n正好为1,10时 right on
getchar();
while(gets(ch)&&ch[0]!='r')
{
if(ch[4]=='l'&&n>a)a=n;
else if(ch[4]=='h'&&n<b)b=n;
scanf("%d",&n);
getchar();
}
if(n>a&&n<b)printf("Stan may be honest\n");//too low too high 不能写成n>=a&&n<=b
else printf("Stan is dishonest\n");
}
return 0;
}
ZOJ1099字符串处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
// freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
char a[1000];
int k=0,i,len;
while(~scanf("%s",a))
{
if(a[0]=='<'&&a[1]=='b'){
printf("\n");
k=0;
}
else if(a[0]=='<'&&a[1]=='h')
{
if(k!=0)printf("\n");
for(i=0;i<80;i++)printf("-");
printf("\n");
k=0;
}
else
{
len=strlen(a);
if(k==0&&len==80){
printf("%s\n",a);
continue;//一个词占一行的单独考虑
}
if(k+len+1>80){//空格也要算进去
printf("\n");
k=0;
}
if(k!=0){
printf(" ");
k++;
}
printf("%s",a);
k+=len;
}
//最后不用打出空行
}
return 0;
}
ZOJ1205这题没坑,是我自己太粗心了.....= =
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char s1[110],s2[110];
int a[110],b[110],c[110];
while(cin>>s1>>s2)
{
int len;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));//勿忘初始化
int i,j;
len=strlen(s1);
for(i=0;i<len;i++)
{
if(s1[i]>='0'&&s1[i]<='9')a[len-1-i]=s1[i]-'0';
else a[len-1-i]=s1[i]-'a'+10;
}
len=strlen(s2);
for(i=0;i<len;i++)
{
if(s2[i]>='0'&&s2[i]<='9')b[len-1-i]=s2[i]-'0';
else b[len-1-i]=s2[i]-'a'+10;
}
int s=0,jin=0;
for(i=0;i<110;i++)
{
s=a[i]+b[i]+jin;
c[i]=s%20;
jin=s/20;
// printf("%d %d %d\n",a[i],b[i],c[i]);
}
for(i=102;i>=1;i--)if(c[i])break;//写成了i=110 wa无数次 ,都是泪啊..,i>=1是因为可能存在两个0的情况
for(;i>=0;i--)
{
if(c[i]>=0&&c[i]<=9)printf("%d",c[i]);
else printf("%c",c[i]+'a'-10);
}
printf("\n");
}
return 0;
}
ZOJ1854 坑啊,就是数字后面可以有很多空格,不搜题解谁知道啊!
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct ss
{
char name[100];
char party[100];
int vote;
}cand[30];
int main()
{
int n,m,i,j;
char ch;
while(~scanf("%d",&n))
{
ch=getchar();
while(ch!='\n')ch=getchar();
for(i=0;i<n;i++)
{
gets(cand[i].name);
gets(cand[i].party);
cand[i].vote=0;
}
scanf("%d",&m);
ch=getchar();
while(ch!='\n')ch=getchar();
char str[100];
for(i=0;i<m;i++)
{
gets(str);
for(j=0;j<n;j++)
{
if(strcmp(str,cand[j].name)==0)
{
cand[j].vote++;
break;
}
}
}
int ma=0,index,num=0;
for(i=0;i<n;i++)
{
// printf("%d %s %d\n",i,cand[i].name,cand[i].vote);
if(cand[i].vote>ma){
ma=cand[i].vote;
index=i;
}
}
for(i=0;i<n;i++)if(cand[i].vote==ma)num++;
if(ma!=0&&num==1)printf("%s\n",cand[index].party);
else printf("tie\n");
}
return 0;
}
pat1030. 完美数列(25)
给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列。
现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完美数列。
输入格式:
输入第一行给出两个正整数N和p,其中N(<= 105)是输入的正整数的个数,p(<= 109)是给定的参数。第二行给出N个正整数,每个数不超过109。
输出格式:
在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。
输入样例:10 8 2 3 20 4 5 1 6 7 8 9输出样例:
8
a[i]*p 会超出int范围
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define mod 10000007
#define N 100010
int main()
{
long long n,p,a[N];
while(~scanf("%lld%lld",&n,&p))
{
for(int i=0;i<n;i++)scanf("%lld",&a[i]);
sort(a,a+n);
//printf("%d\n",upper_bound(a,a+n,20)-a);
long long ma=0;
for(int i=0;i<n;i++)
{
long long t=upper_bound(a,a+n,(long long)a[i]*p)-a;
ma=t-i>ma?t-i:ma;
if(t==n)break;
}
printf("%lld\n",ma);
}
return 0;
}
pat1002. A+B for Polynomials (25)
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
Sample Input2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 2 1.5 1 2.9 0 3.2
提交代码
题目要求不输出系数为0的项,精度0.1
所以用fabs
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define mod 10000007
#define N 100010
int main()
{
int n;
double a[1100];
while(cin>>n)
{
memset(a,0,sizeof(a));
int e;
double c;
for(int i=0;i<n;i++)
{
cin>>e>>c;
a[e]+=c;
}
cin>>n;
for(int i=0;i<n;i++)
{
cin>>e>>c;
a[e]+=c;
}
int s=0;
for(int i=0;i<=1000;i++)
{
if(abs(a[i])>=0.1)s++;
}
cout<<s;
for(int i=1000;i>=0;i--)
{
if(fabs(a[i])>=0.1)printf(" %d %.1lf",i,a[i]);
}
printf("\n");
}
return 0;
}
/*
2 1 2.4 0 3.2
2 2 1.5 1 -2.3
*/
1012. The Best Rank (25)
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:
StudentID C M E A 310101 98 85 88 90 310102 70 95 88 84 310103 82 87 94 88 310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.
Input
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.
Output
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.
The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output "N/A".
Sample Input5 6 310101 98 85 88 310102 70 95 88 310103 82 87 94 310104 91 91 91 310105 85 90 90 310101 310102 310103 310104 310105 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#define N 2020
#define INF 0x7fffffff
using namespace std;
int n,m;
struct ss
{
char id[10];
int c,m,e;
int ran[4];
double a;
}stu[N];
bool cmpa(ss a,ss b){return a.a>b.a;}
bool cmpc(ss a,ss b){return a.c>b.c;}
bool cmpm(ss a,ss b){return a.m>b.m;}
bool cmpe(ss a,ss b){return a.e>b.e;}
map<string,int>q;
void output(int k)
{
if(stu[k].ran[0]<=stu[k].ran[1]&&stu[k].ran[0]<=stu[k].ran[2]&&stu[k].ran[0]<=stu[k].ran[3]){
printf("%d A\n",stu[k].ran[0]);
return;
}
if(stu[k].ran[1]<=stu[k].ran[0]&&stu[k].ran[1]<=stu[k].ran[2]&&stu[k].ran[1]<=stu[k].ran[3]){
printf("%d C\n",stu[k].ran[1]);
return;
}
if(stu[k].ran[2]<=stu[k].ran[0]&&stu[k].ran[2]<=stu[k].ran[1]&&stu[k].ran[2]<=stu[k].ran[3]){
printf("%d M\n",stu[k].ran[2]);
return;
}
if(stu[k].ran[3]<=stu[k].ran[0]&&stu[k].ran[3]<=stu[k].ran[1]&&stu[k].ran[3]<=stu[k].ran[2]){
printf("%d E\n",stu[k].ran[3]);
return;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
q.clear();
for(int i=0;i<n;i++)
{
scanf("%s%d%d%d",stu[i].id,&stu[i].c,&stu[i].m,&stu[i].e);
stu[i].a=(stu[i].c+stu[i].m+stu[i].e)/3;
}
sort(stu,stu+n,cmpa);
stu[0].ran[0]=1;
for(int i=1;i<n;i++){
if(stu[i].a!=stu[i-1].a)stu[i].ran[0]=i+1;
else stu[i].ran[0]=stu[i-1].ran[0];
}
sort(stu,stu+n,cmpc);
stu[0].ran[1]=1;
for(int i=1;i<n;i++){
if(stu[i].c!=stu[i-1].c)stu[i].ran[1]=i+1;
else stu[i].ran[1]=stu[i-1].ran[1];
}
sort(stu,stu+n,cmpm);
stu[0].ran[2]=1;
for(int i=1;i<n;i++){
if(stu[i].m!=stu[i-1].m)stu[i].ran[2]=i+1;
else stu[i].ran[2]=stu[i-1].ran[2];
}
sort(stu,stu+n,cmpe);
stu[0].ran[3]=1;
for(int i=1;i<n;i++){
if(stu[i].e!=stu[i-1].e)stu[i].ran[3]=i+1;
else stu[i].ran[3]=stu[i-1].ran[3];
}
for(int i=0;i<n;i++){
// printf("** %s %lf %d %d %d \n",stu[i].id,stu[i].a,stu[i].c,stu[i].m,stu[i].e);
q.insert(make_pair((string)stu[i].id,i));
}
while(m--)
{
char ch[10];
scanf("%s",ch);
string s=ch;
if(q.find(s)==q.end()){
puts("N/A");
continue;
}
int p=q[s];
// printf("%s %d %d %d %d\n",stu[p].id,stu[p].ran[0],stu[p].ran[1],stu[p].ran[2],stu[p].ran[3]);
output(p);
}
}
return 0;
}
/*
6 7
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310106 32 32 32
310101
310102
310103
310104
310105
310106
999999
*/