PAT天梯赛Level1题目题解汇总
2018-01-27 22:51
288 查看
L1-001 Hello World
L1-010 比较大小
L1-020 帅到没朋友
L1-029 是不是太胖了
L1-035 情人节
L1-040 最佳情侣身高差
#include <stdio.h>
using namespace std;
int main() {
int N;
double h;
char sex;
while(~scanf("%d",&N)) {
while(N--) {
scanf(" %c %lf",&sex,&h);
//男性
if(sex == 'M') {
printf("%.2lf\n",h/1.09);
}
else {
printf("%.2lf\n",h*1.09);
}
}
}
return 0;
}
L1-041 寻找250
#include <stdio.h>
using namespace std;
int main() {
bool flag = false;
int N,cnt=0;
while(~scanf("%d",&N)) {
cnt++;
if(N == 250 && flag == false) {
printf("%d\n",cnt);
flag = true;
}
}
return 0;
}
L1-042 日期格式化
#include <stdio.h>
using namespace std;
char date[20];
void print(int left,int right) {
for(int i = left; i <= right; i++) {
printf("%c",date[i]);
}
}
int main() {
while(~scanf("%s",date)) {
print(6,9);
printf("-");
print(0,1);
printf("-");
print(3,4);
printf("\n");
}
return 0;
}
L1-043 阅览室
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 1002;
int book[maxn]; //book[i]存放书i被借走的时间
int main() {
int N,id,cnt,sum,hour,minute;
char ch,time[15];
memset(book,-1,sizeof(book));
while(~scanf("%d",&N)) {
cnt = sum = 0;
while(N) {
scanf("%d %c %s",&id,&ch,time);
if(id == 0) {
if(cnt == 0) {
printf("0 0\n");
}
else {
double temp = (sum*1.0)/cnt + 0.5;
printf("%d %d\n",cnt,(int)temp);
}
cnt = sum = 0;
N--;
memset(book,-1,sizeof(book));
} //关门的情况
else if(ch == 'S') {
hour = (time[0]-'0')*10 + (time[1]-'0');
minute = (time[3]-'0')*10 + (time[4]-'0');
book[id] = hour*60 + minute;
} //开始借某一本书
else if(ch == 'E') {
if(book[id] != -1) {
hour = (time[0]-'0')*10 + (time[1]-'0');
minute = (time[3]-'0')*10 + (time[4]-'0');
sum = sum + (hour*60+minute) - book[id];
cnt++;
book[id] = -1;
}
}
}
}
return 0;
}
L1-044 稳赢
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;
int main() {
map<string,string>m;
m["ChuiZi"] = "Bu";
m["Bu"] = "JianDao";
m["JianDao"] = "ChuiZi";
int K;
string str;
int cnt = 0;
scanf("%d",&K);
while(cin>>str) {
if(str == "End") {
break;
}
if(cnt == K) {
cout<<str<<endl;
cnt = 0;
}
else {
cnt++;
cout<<m[str]<<endl;
}
}
}
L1-045 宇宙无敌打招呼
#include <stdio.h>
char str[10];
int main() {
while(~scanf("%s",str)) {
printf("Hello ");
puts(str);
}
return 0;
}
L1-046 整除光棍
#include <stdio.h>
int ans[1000];
int main() {
int N;
while(~scanf("%d",&N)) {
int cnt = 0,bit = 0;
int num = 1;
while(true) {
if(cnt || num/N) {
ans[cnt++] = num/N;
}
bit++;
num = num%N;
if(num == 0) {
for(int i = 0; i < cnt; i++) {
printf("%d",ans[i]);
}
printf(" %d\n",bit);
break;
}
num = num*10 + 1;
}
}
return 0;
}
L1-047 装睡
#include <stdio.h>
int main() {
int N,a,b;
char name[100];
while(~scanf("%d",&N)) {
while(N--) {
scanf("%s %d%d",name,&a,&b);
if(a<15 || a>20 || b<50 || b>70) {
puts(name);
}
}
}
return 0;
}
L1-048 矩阵A乘以B
#include <stdio.h>
int A[101][101];
int B[101][101];
int r1,c1,r2,c2,item;
int main() {
scanf("%d%d",&r1,&c1);
for(int i = 1; i <= r1; i++) {
for(int j = 1; j <= c1; j++) {
scanf("%d",&A[i][j]);
}
}
scanf("%d%d",&r2,&c2);
for(int i = 1; i <= r2; i++) {
for(int j = 1; j <= c2; j++) {
scanf("%d",&B[i][j]);
}
}
if(c1 != r2) {
printf("Error: %d != %d\n",c1,r2);
}
else {
printf("%d %d\n",r1,c2);
for(int i = 1; i <= r1; i++) {
for(int j = 1; j <= c2; j++) {
item = 0;
for(int k = 1; k <= c1; k++) {
item += A[i][k]*B[k][j];
}
if(j != 1) {
printf(" ");
}
printf("%d",item);
}
printf("\n");
}
}
return 0;
}
#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace std; int main() { cout<<"Hello World!"<<endl; return 0; }L1-002 打印沙漏
#include <string> #include <math.h> #include <algorithm> #include <string.h> #include <stack> using namespace std; char ch; //打印星号的函数,该函数用来打印每一行的星号。 void print_star(int num_star,int num_space) { while(num_space--) printf(" "); while(num_star--) { printf("%c",ch); } printf("\n"); } int main() { int n,i,j,temp; scanf("%d",&n); int sum1=0,sum2=0; getchar(); scanf("%c",&ch); stack<int>s1; stack<int>s2; for(i = 1 ;sum2<n; i=i+2) { sum1=sum1+i; s1.push(i); sum2=2*sum1-1; } if(sum2>n) { s1.pop(); i=i-2; sum2=2*(sum1-i)-1; } j=0; while(!s1.empty()) { temp=s1.top(); s2.push(temp); s1.pop(); print_star(temp,j++); } s2.pop(); j=j-2; while(!s2.empty()) { temp=s2.top(); print_star(temp,j--); s2.pop(); } printf("%d\n",n-sum2); return 0; }L1-003
#include <iostream> #include <cstdio> #include <string.h> #include <math.h> #include <string.h> using namespace std; int main() { int a[10],i; char s1[1005]; scanf("%s",s1); memset(a,0,sizeof(a)); int len=strlen(s1); for(i=0;i<len;i++) { a[s1[i]-'0']++; } for(i=0;i<10;i++) if(a[i]) { printf("%d:%d\n",i,a[i]); } return 0; }L1-004 计算摄氏温度
#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace std; int main() { double C,F; cin>>F; C = 5*(F-32)/9; cout<<"Celsius = "<<(int)C<<endl; return 0; }L1-005 考试座位号
#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace std; struct M { char s[20]; int n1; int n2; }a[1005]; int main() { int T,n3,b[1005],i,j; scanf("%d",&T); for(i=0;i<T;i++) { scanf("%s%d%d",a[i].s,&a[i].n1,&a[i].n2); } scanf("%d",&n3); for(i=0;i<n3;i++) scanf("%d",&b[i]); for(i=0;i<n3;i++) { for(j=0;j<T;j++) { if(b[i] == a[j].n1) { printf("%s %d\n",a[j].s,a[j].n2); break; } } } return 0; }L1-006 连续因子
/** 题目没什么难的,但是就是感觉题目意思容易误解, 首先题目强调了1不算在因子内,这点一定要主义。 其次要最长连续因子,并且这些因子成起来后依然是 题目给出数字的因子。 例如12: 因子2 3 4 6. 按照题目意思,最长连续因子长度为2,是2 3. 而不是2 3 4.因为2*3*4=24,24不是12的因子。 */ #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> using namespace std; int main() { int N; while(~scanf("%d",&N)) { int limit,i,j,nowLen,start,maxLen,M; limit = (int)sqrt(N); i = 2; //从第一个能整除的因子开始 maxLen = 0; //最长连续因子为0. while(i <= limit+1) { M = N; if(M%i == 0) { nowLen = 0; j = i; while(M%j==0 && j<=limit+1) { nowLen++; M = M/j; j++; } if(nowLen > maxLen) { maxLen = nowLen; start = i; } } i++; } if(maxLen == 0) { printf("1\n%d\n",N); } else { printf("%d\n",maxLen); for(i = start; i < start+maxLen; i++) { if(i == start) { printf("%d",i); } else { printf("*%d",i); } } printf("\n"); } } return 0; }L1-007 念数字
/** 使用C++中STL的map,从数字映射到字符串。 **/ #include <iostream> #include <cstdio> #include <string.h> #include <string> #include <map> using namespace std; int main() { int i; char a[100]; map<int,string>m; m[0] = "ling"; m[1] = "yi"; m[2] = "er"; m[3] = "san"; m[4] = "si"; m[5] = "wu"; m[6] = "liu"; m[7] = "qi"; m[8] = "ba"; m[9] = "jiu"; scanf("%s",a); int len=strlen(a); if(a[0] == '-'){ printf("fu"); for(i=1;i<len;i++) cout<<" "<<m[a[i]-'0']; printf("\n"); } else{ for(i=0;i<len;i++){ if(i) cout<<" "<<m[a[i]-'0']; else cout<<m[a[i]-'0']; } printf("\n"); } return 0; }L1-008 求整数段和
#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <math.h> #include <algorithm> #include <string.h> #include <stack> using namespace std; int main() { int A,B,i,j,sum=0; scanf("%d%d",&A,&B); j=0; for(int i=A;i<=B;i++) { sum=sum+i; printf("%5d",i); j++; if(j%5==0) printf("\n"); } if(j%5!=0) printf("\n"); printf("Sum = %d\n",sum); return 0; }L1-009 N个数求和
/** 解题思路: 这道题目我的基本思路是,将输入的一个个分数,都看成字符串,然后提取出分子和分母,设当前和值为FZ/FM(FZ是汉语分子的首字母缩写,FM是汉语分母的首字母缩写) 则现在输入一个串,例如 -2/5, 则我先求出这个分数的分子fz,分母fm, 则当前和值为 : (FZ/FM)+ (fz/fm) 通分得: = (FZ*fm+FM*fz)/(FM*fm) 然后求(FZ*fm+FM*fz) 但是对于这样的想法,也是有普通情况的。比如当前输入的这个串fz是等于0的,则不管分母是什么,这个数都不用加。 比如测试数据: 2 2/5 0/1 第二个分数分子是0,则这个数是0,则直接跳过不要考虑了。 如果有这种情况: 4 2/5 0/1 -2/5 3/5 则当要加上第三个数时, (FZ*fm+FM*fz) 分子得0,这时候千万不要再去求公约数了。当前的和值为0. 则让FZ = 0 FM = 1; 这样的话输入下一个分数时,FZ/FM + fz/fm 其分子 FZ*fm+FM*fz = 0*fm + 1*fz = fz; 其分母 FM*fm = 1*fm = fm ,刚好就等于 fz/fm = 0+fz/fm **/ #include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> #include<cmath> #define INF 999999 using namespace std; int FZ,FM,fz,fm; //FZ当前和的分子,FM当前和的分母,fz当前求得分子,fm当前求得分母 //辗转相除法求n和m的求最大公约数 int gcd(int n,int m) { int temp,r; n = abs(n); //都取绝对值,先不用考虑符号 m = abs(m); //都取绝对值,先不用考虑符号 if(n < m) //辗转相除,我要让n是大的数,m是小的数,因此这里要进行比较 { temp = n; n = m; m = temp; } while(1) //辗转相除法 { r = n%m; if(r != 0) { n = m; m = r; } else return m; } } void fun(char str[]) //求分子,分母 { int len; int i; len = strlen(str); fz = 0; if(str[0]=='-') //分子是负数 { for(i = 1; i < len; i++) { if(str[i]=='/') //到除号截止 break; fz = fz*10 + (str[i]-'0'); } fz = -1*fz; //变成它的相反数 } else ///分子是正数 { for(i = 0; i < len; i++) { if(str[i]=='/') break; fz = fz*10 + (str[i]-'0'); } } fm = 0; //分母是0 i++; //跳过分号 if(str[i]=='-') //如果分母是负数 { for(i = i+1;i < len; i++) //跳过负号 { fm = fm*10 + (str[i]-'0'); } fm = -1*fm; //取相反数 } else //分母是正数 { for(; i < len; i++) { fm = fm*10 + (str[i]-'0'); } } } int main() { char s[500]; int N; //N个分数相加 while(~scanf("%d",&N)) { scanf("%s",s); //先输入第一个分数 fun(s); //求分子分母 if(fz == 0) //第一个数是0 { FZ = 0; FM = 1; } else { FZ = fz; FM = fm; } for(int i = 1; i < N; i++) { scanf("%s",s); //输入分数 fun(s); //求分子分母 if(fz == 0) //代表当前输入的值为0,不用加,直接跳过 continue; fz = FZ*fm + FM*fz; //这两部是通分 fm = FM*fm; if(fz == 0) //说明当前分子是0,即当前和值变为0 { FZ = 0; FM = 1; } else //否则求最大公约数,化简分数 { int num = gcd(fz,fm); //求最大公约数 FZ = fz/num; //化简分数 FM = fm/num; //化简分数 } } if(FZ == 0) //如果分子为0,则输出0 printf("0\n"); else if((FZ>0&&FM>0)||(FZ<0&&FM<0)) //分子分母同号 { FZ = abs(FZ); //不管正负,直接求绝对值 FM = abs(FM); if(FZ%FM == 0) //分子除尽分母,这种情况答案是整数。 printf("%d\n",FZ/FM); else { int t = FZ/FM; if(t) //这种情况是假分数。 printf("%d %d/%d\n",t,FZ-t*FM,FM); else printf("%d/%d\n",FZ,FM); } } else if((FZ>0&&FM<0)||(FZ<0&&FM>0)) { printf("-"); ///先输出负号 FZ = abs(FZ); FM = abs(FM); if(FZ%FM == 0) printf("%d\n",FZ/FM); else { int t = FZ/FM; if(t) ///是假分数 printf("%d %d/%d\n",t,FZ-t*FM,FM); else printf("%d/%d\n",FZ,FM); } } } return 0; }
L1-010 比较大小
#include <stdio.h> int main() { int a,b,c; while(~scanf("%d%d%d",&a,&b,&c)) { if(a>b) { int t = a; a = b; b = t; } if(c>=b) { printf("%d->%d->%d\n",a,b,c); } else if(c <= a) { printf("%d->%d->%d\n",c,a,b); } else { printf("%d->%d->%d\n",a,c,b); } } return 0; }L1-011 A-B
#include <stdio.h> #include <string.h> char a[10005],b[10005]; int vis[300]; int main() { gets(a); gets(b); memset(vis,0,sizeof(vis)); for(int i = 0; i < (int)strlen(b); i++) { vis[b[i]] = 1; //把出现过的字母标记。 } for(int i = 0; i < (int)strlen(a); i++) { if(vis[a[i]] == 0) { printf("%c",a[i]); } } printf("\n"); return 0; }L1-012 计算指数
#include <stdio.h> int main() { int N; while(~scanf("%d",&N)) { printf("2^%d = %d\n",N,1<<N); //使用移位运算 } return 0; }L1-013 计算阶乘和
#include <stdio.h> int ans[12]; void fun() { ans[0] = 0; int mul = 1; for(int i = 1; i <= 10; i++) { mul = mul*i; ans[i] = ans[i-1] + mul; } } int main() { int N; fun(); while(~scanf("%d",&N)) { printf("%d\n",ans ); } return 0; }L1-014 简单题
#include <stdio.h> int main() { printf("This is a simple problem."); return 0; }L1-015 跟奥巴马一起画方块
#include <stdio.h> int main() { int N; char ch; while(~scanf("%d %c",&N,&ch)) { for(int i = 1; i <= (N+1)/2; i++) { for(int j = 1; j <= N; j++) { printf("%c",ch); } printf("\n"); } } }L1-016 查验身份证
#include <iostream> #include <stdio.h> #include <string> using namespace std; string ans[102]; int power[20] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; char Map[12] = {'1','0','X','9','8','7','6','5','4','3','2'}; bool isLegal(string id) { int sum; if(id.length() != 18) { //身份证号不是18位。 return false; } else { sum = 0; for(int i = 0; i < 17; i++) { if(id[i]>='0' && id[i]<='9') { sum = sum + (id[i]-'0')*power[i]; } else { return false; } } sum = sum%11; if(Map[sum] == id[17]) { return true; } else { return false; } } } int main() { int N; while(~scanf("%d",&N)) { int cnt = 0; string str; for(int i = 0; i < N; i++) { cin>>str; if(!isLegal(str)) { ans[cnt++] = str; } } if(cnt == 0) { cout<<"All passed"<<endl; } else { for(int i = 0; i < cnt; i++) { cout<<ans[i]<<endl; } } } return 0; }L1-017 到底有多二
#include <stdio.h> #include <string.h> char str[55]; int main() { while(~scanf("%s",str)) { int a=0,b=0,len; len = strlen(str); for(int i = 0; i < len; i++) { if(str[i]>='0'&&str[i]<='9') { b++; if(str[i] == '2') { a++; } } } double ans = (a*1.0)/b; if(str[0] == '-') { ans = ans*1.5; } if((str[len-1]-'0')%2 == 0) { ans = ans*2; } ans = ans*100; printf("%.2lf%%\n",ans); } return 0; }L1-018 大笨钟
#include <stdio.h> char time[50]; int main() { int hour,minute; while(~scanf("%s",time)) { hour = (time[0]-'0')*10 + time[1]-'0'; minute = (time[3]-'0')*10 + time[4]-'0'; if(hour<=11 || (hour==12&&minute==0)) { printf("Only %s. Too early to Dang.\n",time); } else { hour = hour%12; int cnt = hour; if(minute != 0) { cnt++; } for(int i = 0; i < cnt; i++) { printf("Dang"); } printf("\n"); } } return 0; }L1-019 谁先倒
#include <stdio.h> int main() { int M,N,num; int a1,a2,b1,b2; //甲喊,甲划,乙喊,乙划 while(~scanf("%d%d",&M,&N)) { scanf("%d",&num); int cnt1=0,cnt2=0; bool flag = false; for(int i = 0; i < num; i++) { scanf("%d%d%d%d",&a1,&a2,&b1,&b2); if(flag) continue; //都输 if(a1+b1==a2 && a1+b1==b2) { continue; } else if(a1+b1!=a2 && a1+b1!=b2) { continue; } else if(a1+b1 == a2){ M--; cnt1++; if(M == -1) { printf("A\n%d\n",cnt2); flag = true; } }//甲输 else{ N--; cnt2++; if(N==-1) { printf("B\n%d\n",cnt1); flag = true; } }//乙输 } } return 0; }
L1-020 帅到没朋友
#include <stdio.h> #include <string.h> int vis[1000000]; int visans[1000000]; int ans[1000000]; int Max(int a,int b) { return a>b?a:b; } int main() { int N,M,a,cnt; while(~scanf("%d",&N)) { memset(vis,0,sizeof(vis)); memset(visans,0,sizeof(visans)); cnt = 0; for(int i = 0; i < N; i++) { scanf("%d",&M); for(int j = 0; j < M; j++) { scanf("%d",&a); vis[a] = Max(vis[a],M); } } scanf("%d",&M); for(int i = 0; i < M; i++) { scanf("%d",&a); if(visans[a] == 0) { if(vis[a]==0 || vis[a]==1) { ans[cnt++] = a; visans[a] = 1; } } } if(cnt == 0) { printf("No one is handsome\n"); } else { for(int i = 0; i < cnt; i++) { if(i == 0) { printf("%05d",ans[i]); } else { printf(" %05d",ans[i]); } } printf("\n"); } } return 0; }L1-021 重要的话说三遍
#include <stdio.h> int main() { printf("I'm gonna WIN!\n"); printf("I'm gonna WIN!\n"); printf("I'm gonna WIN!\n"); return 0; }L1-022 奇偶分家
#include <stdio.h> int main() { int N,num; while(~scanf("%d",&N)) { int odd=0,even=0; for(int i = 1; i <= N; i++) { scanf("%d",&num); if(num%2 == 0) { even++; } else { odd++; } } printf("%d %d\n",odd,even); } return 0; }L1-023 输出GPLT
#include <stdio.h> #include <string.h> char str[10000]; int a[10]; int main() { while(~scanf("%s",str)) { //1,2,3,4分别用来统计g,p,l,t a[1] = a[2] = a[3] = a[4] = 0; for(int i = 0; i < (int)strlen(str); i++) { if(str[i]=='g' || str[i]=='G') { a[1]++; } else if(str[i]=='p' || str[i]=='P') { a[2]++; } else if(str[i]=='l' || str[i]=='L') { a[3]++; } else if(str[i]=='t' || str[i]=='T') { a[4]++; } } int sum = a[1]+a[2]+a[3]+a[4]; int cnt = 0; while(cnt < sum) { if(a[1]) { printf("G"); cnt++; a[1]--; } if(a[2]) { printf("P"); cnt++; a[2]--; } if(a[3]) { printf("L"); cnt++; a[3]--; } if(a[4]) { printf("T"); cnt++; a[4]--; } } printf("\n"); } return 0; }L1-024 后天
#include <stdio.h> #include <string.h> int main() { int N; while(~scanf("%d",&N)) { N = N+2; N = N%7; if(N == 0) { printf("7\n"); } else { printf("%d\n",N); } } return 0; }L1-025 正整数A+B
#include <stdio.h> #include <string.h> int isLegal(char str[]) { if(str[0] == '-') { return -1; } int num = 0; for(int i = 0; i < (int)strlen(str); i++) { if(str[i]>='0' && str[i]<='9') { num = num*10 + (str[i]-'0'); if(num>1000) { return -1; } } else { return -1; } } if(num >= 1) return num; else return -1; } int main() { char a[100],b[100]; while(~scanf("%s ",a)) { gets(b); int A,B; A = isLegal(a); B = isLegal(b); if(A!=-1 && B!=-1) { printf("%d + %d = %d\n",A,B,A+B); } else if(A == -1 && B == -1) { printf("? + ? = ?\n"); } else if(A == -1 && B != -1) { printf("? + %d = ?\n",B); } else { printf("%d + ? = ?\n",A); } } return 0; }L1-026 I Love GPLT
#include <stdio.h> #include <string.h> int main() { char str[20]; strcpy(str,"I Love GPLT"); for(int i = 0; i < (int)strlen(str); i++) { printf("%c\n",str[i]); } return 0; }L1-027 出租
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int arr[12]; int Index[20]; int vis[12]; bool cmp(int a,int b) { return a>b; } int main() { char tel[20]; while(~scanf("%s",tel)) { memset(vis,0,sizeof(vis)); int cnt = 0,num; for(int i = 0; i < (int)strlen(tel); i++) { num = tel[i]-'0'; if(vis[num] == 0) { arr[cnt++] = num; vis[num] = 1; } } sort(arr,arr+cnt,cmp); for(int i = 0; i < cnt; i++) { vis[arr[i]] = i; } for(int i = 0; i < (int)strlen(tel); i++) { int num = tel[i]-'0'; Index[i] = vis[num]; } printf("int[] arr = new int[]{"); for(int i = 0; i < cnt; i++) { if(i != cnt-1) { printf("%d,",arr[i]); } else { printf("%d};\n",arr[i]); } } printf("int[] index = new int[]{"); for(int i = 0; i < 11; i++) { if(i != 10) { printf("%d,",Index[i]); } else { printf("%d};\n",Index[i]); } } } return 0; }L1-028 判断素数
#include <stdio.h> #include <math.h> bool isPrime(int N) { if(N==0 || N==1) { return false; } for(int i = 2; i <= sqrt(N); i++) { if(N%i == 0) { return false; } } return true; } int main() { int N,M; while(~scanf("%d",&N)) { while(N--) { scanf("%d",&M); if(isPrime(M)) { printf("Yes\n"); } else { printf("No\n"); } } } return 0; }
L1-029 是不是太胖了
#include <stdio.h> int main() { int N; while(~scanf("%d",&N)) { printf("%.1lf\n",(N-100)*1.8); } return 0; }L1-030 一帮一
#include <stdio.h> #include <iostream> using namespace std; int sex[55]; string name[55]; int main() { int N; while(~scanf("%d",&N)) { for(int i = 1; i <= N; i++) { cin>>sex[i]>>name[i]; } for(int i = 1; i <= N/2; i++) { for(int j = N; j >= 1; j--) { if(sex[j] == 1-sex[i]) { cout<<name[i]<<" "<<name[j]<<endl; sex[j] = 3; break; } } } } return 0; }L1-031 到底是不是太胖了
#include <stdio.h> #include <iostream> #include <math.h> using namespace std; int main() { int N; while(~scanf("%d",&N)) { double a,b; for(int i = 1; i <= N; i++) { scanf("%lf%lf",&a,&b); a = (a-100)*1.8; if(fabs(a-b)*10 < a) { printf("You are wan mei!\n"); } else if(a < b) { printf("You are tai pang le!\n"); } else if(a > b) { printf("You are tai shou le!\n"); } } } return 0; }L1-032 Left-pad
#include <stdio.h> #include <string.h> char ch,str[100000]; int main() { int N; while(~scanf("%d %c ",&N,&ch)) { gets(str); int len = strlen(str); if(len>=N) { for(int i = len-N; i < len; i++) { printf("%c",str[i]); } printf("\n"); } else { for(int i = 0; i < N-len; i++) { printf("%c",ch); } puts(str); } } return 0; }L1-033 出生年
#include <stdio.h> #include <string.h> int main() { int cnt,year,n,vis[10],temp; while(~scanf("%d%d",&year,&n)) { for(int i = year; ;i++) { memset(vis,0,sizeof(vis)); cnt = 0; temp = i; if(temp < 1000) { cnt++; vis[0] = 1; } while(temp) { if(vis[temp%10] == 0) { vis[temp%10] = 1; cnt++; } temp = temp/10; } if(cnt == n) { printf("%d %04d\n",i-year,i); break; } } } return 0; }L1-034 点赞
#include <stdio.h> #include <string.h> int vis[1005]; int main() { int N,M; while(~scanf("%d",&N)) { memset(vis,0,sizeof(vis)); int Max=0,ans,a; for(int i = 0; i < N; i++) { scanf("%d",&M); for(int j = 0; j < M; j++) { scanf("%d",&a); vis[a]++; if(vis[a]>Max) { Max = vis[a]; ans = a; } if(vis[a] == Max) { if(a > ans) { ans = a; } } } } printf("%d %d\n",ans,Max); } return 0; }
L1-035 情人节
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <string> #include <set> #include <map> #include <stack> #include <queue> #include <algorithm> using namespace std; int main() { char name1[20],name2[20],str[20]; int er = 0,shisi = 0,num = 0; while(scanf("%s",str)) { if(strcmp(str,".")==0) break; num++; if(num == 2) { strcpy(name1,str); er = 1; } if(num == 14) { strcpy(name2,str); shisi = 1; } } if(er == 0) { printf("Momo... No one is for you ...\n"); } else if(shisi == 0) { printf("%s is the only one for you...\n",name1); } else printf("%s and %s are inviting you to dinner...\n",name1,name2); getchar(); return 0; }L1-036 A乘以B
#include <stdio.h> int main() { int a,b; while(~scanf("%d%d",&a,&b)) { printf("%d\n",a*b); } return 0; }L1-037 A除以B
#include <stdio.h> int main() { int a,b; while(~scanf("%d%d",&a,&b)) { if(b == 0) { printf("%d/0=Error\n",a); } else if(b < 0) { printf("%d/(%d)=%.2lf\n",a,b,(a*1.0)/b); } else { printf("%d/%d=%.2lf\n",a,b,(a*1.0)/b); } } return 0; }L1-038 新世界
#include <stdio.h> int main() { printf("Hello World\n"); printf("Hello New World"); return 0; }L1-039 古风排版
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <string> #include <set> #include <map> #include <stack> #include <queue> #include <algorithm> //#include<bits/stdc++.h> using namespace std; char a[102][102]; int main() { int length,width,num; ///width存放列数,length用来存放行数 char str[1004]; while(~scanf("%d",&length)) { getchar(); ///接收空字符 gets(str); ///获取字符串,因为字符串中有空格,因此要用gets输入 int len = strlen(str); ///测串长 if(len%length == 0) ///求列宽 { width = len/length; } else { width = len/length+1; } num = 0; ///用来统计当前用的字符个数 for(int i = width; i > 0; i--) ///列数从右向左循环 { for(int j = 1; j <= length; j++) ///行数从上向下循环 { if(num < len) ///如果字符串中的字符都没有用完放入矩阵 { a[j][i] = str[num]; num++; } else ///字符用完了,剩余位置放空字符即可 { a[j][i] = ' '; } } } for(int i = 1; i <= length; i++) ///输出矩阵 { for(int j = 1; j <= width; j++) { printf("%c",a[i][j]); } printf("\n"); } } return 0; }
L1-040 最佳情侣身高差
#include <stdio.h>
using namespace std;
int main() {
int N;
double h;
char sex;
while(~scanf("%d",&N)) {
while(N--) {
scanf(" %c %lf",&sex,&h);
//男性
if(sex == 'M') {
printf("%.2lf\n",h/1.09);
}
else {
printf("%.2lf\n",h*1.09);
}
}
}
return 0;
}
L1-041 寻找250
#include <stdio.h>
using namespace std;
int main() {
bool flag = false;
int N,cnt=0;
while(~scanf("%d",&N)) {
cnt++;
if(N == 250 && flag == false) {
printf("%d\n",cnt);
flag = true;
}
}
return 0;
}
L1-042 日期格式化
#include <stdio.h>
using namespace std;
char date[20];
void print(int left,int right) {
for(int i = left; i <= right; i++) {
printf("%c",date[i]);
}
}
int main() {
while(~scanf("%s",date)) {
print(6,9);
printf("-");
print(0,1);
printf("-");
print(3,4);
printf("\n");
}
return 0;
}
L1-043 阅览室
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 1002;
int book[maxn]; //book[i]存放书i被借走的时间
int main() {
int N,id,cnt,sum,hour,minute;
char ch,time[15];
memset(book,-1,sizeof(book));
while(~scanf("%d",&N)) {
cnt = sum = 0;
while(N) {
scanf("%d %c %s",&id,&ch,time);
if(id == 0) {
if(cnt == 0) {
printf("0 0\n");
}
else {
double temp = (sum*1.0)/cnt + 0.5;
printf("%d %d\n",cnt,(int)temp);
}
cnt = sum = 0;
N--;
memset(book,-1,sizeof(book));
} //关门的情况
else if(ch == 'S') {
hour = (time[0]-'0')*10 + (time[1]-'0');
minute = (time[3]-'0')*10 + (time[4]-'0');
book[id] = hour*60 + minute;
} //开始借某一本书
else if(ch == 'E') {
if(book[id] != -1) {
hour = (time[0]-'0')*10 + (time[1]-'0');
minute = (time[3]-'0')*10 + (time[4]-'0');
sum = sum + (hour*60+minute) - book[id];
cnt++;
book[id] = -1;
}
}
}
}
return 0;
}
L1-044 稳赢
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;
int main() {
map<string,string>m;
m["ChuiZi"] = "Bu";
m["Bu"] = "JianDao";
m["JianDao"] = "ChuiZi";
int K;
string str;
int cnt = 0;
scanf("%d",&K);
while(cin>>str) {
if(str == "End") {
break;
}
if(cnt == K) {
cout<<str<<endl;
cnt = 0;
}
else {
cnt++;
cout<<m[str]<<endl;
}
}
}
L1-045 宇宙无敌打招呼
#include <stdio.h>
char str[10];
int main() {
while(~scanf("%s",str)) {
printf("Hello ");
puts(str);
}
return 0;
}
L1-046 整除光棍
#include <stdio.h>
int ans[1000];
int main() {
int N;
while(~scanf("%d",&N)) {
int cnt = 0,bit = 0;
int num = 1;
while(true) {
if(cnt || num/N) {
ans[cnt++] = num/N;
}
bit++;
num = num%N;
if(num == 0) {
for(int i = 0; i < cnt; i++) {
printf("%d",ans[i]);
}
printf(" %d\n",bit);
break;
}
num = num*10 + 1;
}
}
return 0;
}
L1-047 装睡
#include <stdio.h>
int main() {
int N,a,b;
char name[100];
while(~scanf("%d",&N)) {
while(N--) {
scanf("%s %d%d",name,&a,&b);
if(a<15 || a>20 || b<50 || b>70) {
puts(name);
}
}
}
return 0;
}
L1-048 矩阵A乘以B
#include <stdio.h>
int A[101][101];
int B[101][101];
int r1,c1,r2,c2,item;
int main() {
scanf("%d%d",&r1,&c1);
for(int i = 1; i <= r1; i++) {
for(int j = 1; j <= c1; j++) {
scanf("%d",&A[i][j]);
}
}
scanf("%d%d",&r2,&c2);
for(int i = 1; i <= r2; i++) {
for(int j = 1; j <= c2; j++) {
scanf("%d",&B[i][j]);
}
}
if(c1 != r2) {
printf("Error: %d != %d\n",c1,r2);
}
else {
printf("%d %d\n",r1,c2);
for(int i = 1; i <= r1; i++) {
for(int j = 1; j <= c2; j++) {
item = 0;
for(int k = 1; k <= c1; k++) {
item += A[i][k]*B[k][j];
}
if(j != 1) {
printf(" ");
}
printf("%d",item);
}
printf("\n");
}
}
return 0;
}
相关文章推荐
- PAT天梯赛Level2题目题解汇总
- C/C++ 笔试、面试题目大汇总(二)
- 编程题目: PAT 1020. 月饼 (25)
- 编程题目: PAT 1025. 反转链表 (25)
- C/C++ 笔试、面试题目大汇总
- 5-4 PAT排名汇总 (25分)
- PAT甲级 1003.Emergency(25) 题目翻译与答案
- PAT考试乙级1055(C++语言实现) (重点题目)(思路)
- C/C++ 笔试、面试题目大汇总
- PAT甲级 1009.Product of Polynomials(25) 题目翻译与答案
- Android常见面试题目小汇总(更新中...)
- 【Learning】Link-Cut Tree 题目汇总
- java面试题目汇总1
- NOIP2008题目汇总
- C/C++ 笔试、面试题目大汇总
- android面试题目汇总持续更新
- PAT练习基础编程题目之统计个位数字
- Java面试题目汇总
- [Contest] IOI2011 day1 题目汇总
- 线段树题目 汇总 讲解(by not only success)