L1-001 Hello World (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第1张图片](http://img.e-com-net.com/image/info8/d1a483bc268f4bb88e7844b52ba87f41.jpg)
#include
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
system("pause");
}
L1-002 打印沙漏 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第2张图片](http://img.e-com-net.com/image/info8/2faa8be0b9b241d88aa00bbef019efa7.jpg)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第3张图片](http://img.e-com-net.com/image/info8/60d072202f5d4d43a1865e506fcd6f78.jpg)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第4张图片](http://img.e-com-net.com/image/info8/6901097b64a445028dd3da152c26709b.jpg)
#include
using namespace std;
int main()
{
int N,n=0,i=1,r=0;
char c;
cin>>N>>c;
while(n<=N)
{
n=2*i*i-1;
i++;
}
i=i-2;
r=N-2*i*i+1;
int j,k;
for(j=0;j<i-1;j++)
{
for(k=0;k<j;k++)
cout<<" ";
for(k=0;k<2*(i-1-j)+1;k++)
cout<<c;
cout<<endl;
}
for(j=0;j<i;j++)
{
for(k=0;k<i-j-1;k++)
cout<<" ";
for(k=0;k<2*j+1;k++)
cout<<c;
cout<<endl;
}
cout<<r;
return 0;
}
L1-003 个位数统计 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第5张图片](http://img.e-com-net.com/image/info8/2b52da087545451d8453f58297971534.jpg)
#include
#include
using namespace std;
int main()
{
string number;
int a[10]={0};
int i;
cin>>number;
for(i=0;i<number.size();i++)
a[number[i]-'0']++;
for(i=0;i<10;i++)
if(a[i]) cout<<i<<":"<<a[i]<<endl;
return 0;
}
L1-004 计算摄氏温度 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第6张图片](http://img.e-com-net.com/image/info8/0deea864b8ab427e99139603c6408560.jpg)
#include
using namespace std;
int main()
{
int C;
int F;
cin>>F;
cout<<"Celsius = "<<5*(F-32)/9;
return 0;
}
L1-005 考试座位号 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第7张图片](http://img.e-com-net.com/image/info8/e48b66267f34474e93f640b64d23dbe1.jpg)
#include
using namespace std;
struct person{
long long int ip;
int num;
int sit;
}a[1025];
int main()
{
int N,i,j;
cin>>N;
for(i=0;i<N;i++)
{
cin>>a[i].ip>>a[i].num>>a[i].sit;
}
int M,d;
cin>>M;
for(i=0;i<M;i++)
{
cin>>d;
for(j=0;j<N;j++)
{
if(a[j].num==d)
cout<<a[j].ip<<" "<<a[j].sit<<endl;
}
}
}
L1-006 连续因子 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第8张图片](http://img.e-com-net.com/image/info8/02db443698744555947492e976381836.jpg)
#include
#include
using namespace std;
int IsPrime(int number)
{
int i;
if (number == 2) return 1;
else
{
for (i = 2; i <= sqrt(number); i++)
if (number%i == 0) return 0;
return 1;
}
}
int main()
{
int fact = 0, i, j, N,len=0,start=0;
cin >> N;
if (IsPrime(N))
{
cout << "1" << endl;
cout << N;
}
else
{
for (i = 2; i <= sqrt(N); i++)
{
fact = 1;
for (j = i; j*fact <= N; j++)
{
fact *= j;
if (N%fact == 0 && j - i + 1 > len)
{
len = j - i + 1;
start = i;
}
}
}
cout << len<< endl;
for (i = start; i < start + len; i++)
{
cout << i;
if (i < start + len - 1)
cout << "*";
}
}
}
L1-007 念数字 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第9张图片](http://img.e-com-net.com/image/info8/b0e8c66d32ce4aafb23077c9220ce968.jpg)
#include
#include
#include
int main()
{
int number,n=0,k;
char a[10][5];
strcpy(a[0],"ling");
strcpy(a[1],"yi");
strcpy(a[2],"er");
strcpy(a[3],"san");
strcpy(a[4],"si");
strcpy(a[5],"wu");
strcpy(a[6],"liu");
strcpy(a[7],"qi");
strcpy(a[8],"ba");
strcpy(a[9],"jiu");
scanf("%d",&number);
k=number;
if(number<0){
number=-number;
printf("fu");}
while(number)
{
number/=10;
n++;
}
for(int i=n-1;i>=0;i--){
if(k<10) printf("%s",a[k]);
else{
printf("%s",a[k/(int)pow(10,i)]);
k=k-(k/(int)pow(10,i))*(int)pow(10,i);}
}
return 0;
}
L1-008 求整数段和 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第10张图片](http://img.e-com-net.com/image/info8/2a14224be8f1485b973ffc42019cd989.jpg)
#include
int main()
{
int A,B,i,count=0,Sum=0;
scanf("%d%d",&A,&B);
for(i=A;i<=B;i++)
{
printf("%5d",i);
Sum+=i;
count++;
if(count%5==0||i==B) printf("\n");
}
printf("Sum = %d",Sum);
return 0;
}
L1-009 N个数求和 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第11张图片](http://img.e-com-net.com/image/info8/83e0f61526294c369ed8bd61cc1fa934.jpg)
#include
using namespace std;
int gcd(int q,int p)
{
if (q%p == 0) return p;
else
return gcd(p, q%p);
}
int main()
{
int sum1 = 0, sum2 = 1,m=0;
int a, b;
char c = '/';
int N;
cin >> N;
while (N--)
{
cin >> a >>c>> b;
sum1 *= b;
sum1 += a * sum2;
sum2 = sum2 * b;
m = gcd(sum1, sum2);
sum1 /= m;
sum2 /= m;
}
if (sum1 > sum2&&sum1%sum2!=0) cout << sum1 / sum2 << " " << sum1 % sum2 << c << sum2;
else if (sum1%sum2 == 0)cout << sum1 / sum2;
else if (sum1 < sum2) cout<< sum1 << c << sum2;
}
L1-010 比较大小 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第12张图片](http://img.e-com-net.com/image/info8/e975098a5ea24011800110c99d8797e5.jpg)
#include
using namespace std;
int main()
{
int a[3],temp;
int min = 0;
int i, j;
for (i = 0; i < 3; i++)
cin >> a[i];
for (i = 0; i < 2; i++)
{
min = i;
for (j = i+1; j < 3; j++)
{
if (a[min]> a[j])
min = j;
}
if (min != i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
for (i = 0; i < 3; i++)
{
cout << a[i];
if (i < 2)cout << "->";
}
}
L1-011 A-B (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第13张图片](http://img.e-com-net.com/image/info8/cd717465979148d79f7e6f235b68d79f.jpg)
#include
#include
using namespace std;
int main()
{
string a;
string b;
getline(cin, a);
getline(cin, b);
int i, j,flag=0;
for (i = 0; i < a.size(); i++)
{
flag = 0;
for (j = 0; j < b.size(); j++)
{
if (a[i] == b[j])
flag = 1;
}
if (flag == 0)cout << a[i];
}
}
L1-012 计算指数 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第14张图片](http://img.e-com-net.com/image/info8/fb3c850e2e2948fc872695c774353c2a.jpg)
#include
#include
int main()
{
int n,sum=0;
scanf("%d",&n);
sum=pow(2,n);
printf("2^%d = %d",n,sum);
return 0;
}
L1-013 计算阶乘和 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第15张图片](http://img.e-com-net.com/image/info8/31c709f62a3442669275e32baf849e90.jpg)
#include
int main()
{
int n,N=0,sum=1,i,j,add=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
sum=1;
for(j=1;j<=i;j++)
sum*=j;
add+=sum;
}
printf("%d",add);
}
L1-014 简单题 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第16张图片](http://img.e-com-net.com/image/info8/12f88701025848adaf1c509ef54c5588.jpg)
#include
int main()
{
printf("This is a simple problem.");
}
L1-015 跟奥巴马一起画方块 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第17张图片](http://img.e-com-net.com/image/info8/24665d2b4c7e4ce1b70d249969d13480.jpg)
#include
int main()
{
int N,i,j,t;
char a;
scanf("%d %c",&N,&a);
if(N%2) t=N/2+1;
else t=N/2;
for(i=0;i<t;i++)
{
for(j=0;j<N;j++)
printf("%c",a);
printf("\n");
}
return 0;
}
L1-016 查验身份证 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第18张图片](http://img.e-com-net.com/image/info8/ccbad394595544f5b7f45d6cd0d526e4.jpg)
#include
#include
using namespace std;
int main()
{
int power[20] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
string check = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
string str;
int N,i,j,flag=1;
cin >> N;
for (i = 0; i < N; i++)
{
cin >> str;
int sum = 0, sign = 1;
for (j = 0; j < 17; j++) {
if (str[j] >= '0'&&str[j] <= '9')
{
sum += (str[j] - '0')*power[j];
}
else
{
cout << str << endl;
sign = 0;
flag = 0;
break;
}
}
if (str[17] != check[sum % 11]&&sign)
{
cout << str << endl;
flag = 0;
}
}
if (flag)
cout << "All passed";
return 0;
}
L1-017 到底有多二 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第19张图片](http://img.e-com-net.com/image/info8/58c2547bb23a4cc0bf05216fa165eda1.jpg)
#include
#include
using namespace std;
int main()
{
string num;
int n,i,count=0,flag=0;
double x;
cin >> num;
if (num[0] == '-')
{
n = num.size() - 1;
for (i = 1; i <= n; i++)
{
if (num[i] == '2')
count++;
}
if ((num[num.size() - 1] -'0')% 2 == 0)flag = 2;
else flag = 1;
x = count*1.0 / n*1.5*flag*100;
printf("%.2f%%", x);
}
else
{
n = num.size();
for (i = 0; i < n; i++)
{
if (num[i] == '2')
count++;
}
if ((num[num.size() - 1] - '0') % 2 == 0)flag = 2;
else flag = 1;
x = count * 1.0 / n * 1*flag * 100;
printf("%.2f%%", x);
}
return 0;
}
L1-018 大笨钟 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第20张图片](http://img.e-com-net.com/image/info8/8428b680a40d464cb5bc2873695fab59.jpg)
#include
#include
#include
using namespace std;
int main()
{
int hh, mm,count=0,i;
char c = ':';
cin >> hh >> c >> mm;
if (hh < 12)
printf("Only %02d:%02d. Too early to Dang.", hh, mm);
else if (hh > 12)
{
if (mm != 0)
count = hh + 1-12;
else count = hh-12;
for (i = 0; i < count; i++)
cout << "Dang";
}
else if (hh == 12)
{
if(mm==0)printf("Only %02d:%02d. Too early to Dang.", hh, mm);
else if (mm != 0)printf("Dang");
}
}
L1-019 谁先倒 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第21张图片](http://img.e-com-net.com/image/info8/0fe52b7606ff4b08bc93eedf781cfa0d.jpg)
#include
using namespace std;
int main()
{
int x, y,c,d;
cin >> x >> y;
c=x;
d=y;
int N;
cin >> N;
int a1, a2, b1, b2;
while (N--)
{
cin >> a1 >> a2 >> b1 >> b2;
if (a2 == a1 + b1 && b2 != b1 + a1)
x--;
if (b2 == b1 + a1 && a2 != a1 + b1)
y--;
if (x < 0)
{
cout << 'A' << endl;
cout << d-y;
break;
}
else if (y <0)
{
cout << 'B' << endl;
cout << c-x;
break;
}
}
}
L1-020 帅到没朋友 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第22张图片](http://img.e-com-net.com/image/info8/24af0984363c46a69bb80fcd42f26324.jpg)
#include
using namespace std;
int main()
{
int N;
cin >> N;
int a[122555] = { 0 };
int i,j,x,num ;
for (i = 0; i < N; i++)
{
cin >> x;
for (j = 0; j < x; j++)
{
cin >> num;
if(x!=1)
a[num] = 1;
}
}
int n,flag=0,sign=0;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> num;
if (a[num] == 0)
{
if(flag)
cout << " ";
flag++;
printf("%05d",num);
a[num] = 2;
sign = 1;
}
}
if (sign == 0)
cout << "No one is handsome";
return 0;
}
L1-021 重要的话说三遍 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第23张图片](http://img.e-com-net.com/image/info8/bb23df1f5c4f4357b4e9a4a978891c1e.jpg)
#include
int main()
{
printf("I'm gonna WIN!\n");
printf("I'm gonna WIN!\n");
printf("I'm gonna WIN!\n");
return 0;
}
L1-022 奇偶分家 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第24张图片](http://img.e-com-net.com/image/info8/5f07f40e876e4830a2255a1953114513.jpg)
#include
int main()
{
int n,i,k=0,l=0;
scanf("%d\n",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{if(a[i]%2==0) k++;
else l++;
}
printf("%d %d",l,k);
return 0;
}
L1-023 输出GPLT (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第25张图片](http://img.e-com-net.com/image/info8/aeef398441f54385a6f1c7c49367ebce.jpg)
#include
#include
using namespace std;
int main()
{
string s;
cin >> s;
int i, j,g=0,p=0,l=0,t=0;
for (i = 0; i < s.size(); i++)
{
if (s[i] == 'G' || s[i] == 'g')
g++;
if (s[i] == 'P' || s[i] == 'p')
p++;
if (s[i] == 'L' || s[i] == 'l')
l++;
if (s[i] == 'T' || s[i] == 't')
t++;
}
int n = g + p + l + t;
while (n--)
{
if (g > 0)
{
cout << 'G';
g--;
}
if (p > 0)
{
cout << 'P';
p--;
}
if (l > 0)
{
cout << 'L';
l--;
}
if (t > 0)
{
cout << 'T';
t--;
}
}
}
L1-024 后天 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第26张图片](http://img.e-com-net.com/image/info8/e9f61b5869094ce1a9ccc73fcfcbf0e5.jpg)
#include
int main()
{
int D,h;
scanf("%d",&D);
h=D+2;
if(h>7)
printf("%d",h%7);
else
printf("%d",h);
return 0;
}
L1-025 正整数A+B (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第27张图片](http://img.e-com-net.com/image/info8/6b52d8d93dc04fd1873c251228e78f8a.jpg)
#include
#include
using namespace std;
int main()
{
string a;
string b;
int m = 0, n = 0;
cin >> a;
getline(cin, b);
int i, flag = 0, sign = 0;
for (i = 0; i < a.size(); i++)
{
if (a[i] < '0' || a[i] >'9')
{
flag = 1;
}
}
for (i = 1; i < b.size() - 1; i++)
{
if (b[i] < '0' || b[i] >'9')
{
sign = 1;
}
}
for (i = 0; i < a.size(); i++)
{
m = m * 10 + a[i] - '0';
}
for (i = 1; i < b.size(); i++)
{
n = n * 10 + b[i] - '0';
}
if (m > 1000||a[0]=='0')flag = 1;
if (n > 1000||b[1]=='0')sign = 1;
if (flag) a = "?";
if (sign) b = " ?";
if (flag == 0 && sign == 0)
{
cout << a << " " << '+' << b << " " << '=' << " " << m + n;
}
else if (flag || sign)
{
cout << a << " " << '+' << b << " " << '=' << " " << '?';
}
}
L1-026 I Love GPLT (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第28张图片](http://img.e-com-net.com/image/info8/f0287174d0294d7e85952493c1575de1.jpg)
#include"stdio.h"
#include"stdlib.h"
int main()
{
char a[12]={"I Love GPLT"};
int i;
for(i=0;i<=11;i++)
printf("%c\n",a[i]);
return 0;
}
L1-027 出租 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第29张图片](http://img.e-com-net.com/image/info8/0b725fbc0afb4d829f2dfc58edbee459.jpg)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第30张图片](http://img.e-com-net.com/image/info8/cd90b4ab791b40cc8638ce4c75fdcdee.jpg)
#include
#include
#include
int main()
{
char a[12];
char id[12];
char c[12];
int i,j,max,temp,sign=0,flag=0,k=0;
scanf("%s",id);
strcpy(a,id);
for(i=0;i<10;i++)
{
max=i;
for(j=i;j<11;j++)
{
if(id[max]<id[j])
max=j;
}
if(max!=i)
{
temp=id[i];
id[i]=id[max];
id[max]=temp;
}
}
for(i=0;i<11-sign;i++)
{
if(id[i]==id[i+1])
{
for(j=i;j<11-sign;j++)
id[j]=id[j+1];
sign++;
i--;
}
}
printf("int[] arr = new int[]{");
for(i=0;i<strlen(id);i++)
{
printf("%c",id[i]);
flag++;
if(flag<strlen(id))
printf(",");
}
printf("};\n");
for(i=0;i<11;i++)
{
for(j=0;j<strlen(id);j++)
{
if(a[i]==id[j])
c[k++]=j;
}
}
flag=0;
printf("int[] index = new int[]{");
for(i=0;i<k;i++)
{
printf("%d",c[i]);
flag++;
if(flag<k)
printf(",");
}
printf("};");
return 0;
}
L1-028 判断素数 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第31张图片](http://img.e-com-net.com/image/info8/d0b35350f10c4448a7640569f7604e5c.jpg)
#include
#include
int main()
{
int N,i,j,sign=0;
scanf("%d\n",&N);
long long int a[N];
for(i=0;i<N;i++)
scanf("%lld",&a[i]);
for(i=0;i<N;i++)
{
sign=0;
if(a[i]==2)
printf("Yes\n");
if(a[i]==1)
printf("No\n");
else if(a[i]>2)
{
for(j=2;j<=sqrt(a[i]);++j)
{
if(a[i]%j==0)
{
sign=1;
}
}
if(sign==0)
printf("Yes\n");
if(sign)
printf("No\n");
}
}
return 0;
}
L1-029 是不是太胖了 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第32张图片](http://img.e-com-net.com/image/info8/1443d080ad6d4873bc23aea4f2dd092e.jpg)
#include
int main()
{
int H;
double T;
scanf("%d",&H);
T=((H-100)*0.9)*2;
printf("%.1f",T);
return 0;
}
L1-030 一帮一 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第33张图片](http://img.e-com-net.com/image/info8/cc245ff7d2d444b3a16da8176fa8832d.jpg)
#include
#include
using namespace std;
struct person
{
int sex;
string name;
int flag = 0;
}a[55];
int main()
{
int N;
cin >> N;
int i,j;
for (i = 0; i < N; i++)
{
cin >> a[i].sex >> a[i].name;
}
for (i = 0; i < N; i++)
{
for (j=N-1; j >= 0; j--)
{
if (a[i].sex != a[j].sex&&a[j].flag == 0)
{
cout << a[i].name << " " << a[j].name << endl;
a[j].flag = 1;
a[i].flag = 1;
break;
}
}
}
}
L1-031 到底是不是太胖了 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第34张图片](http://img.e-com-net.com/image/info8/7423a2b0b64e4d6ba1e415677399a723.jpg)
#include
#include
int main() {
double s,x,a,b;
int n; scanf("%d",&n); while(n--) {
scanf("%lf%lf",&a,&b);
s=(a-100.0)*0.9;
x=b/2.0;
if(x>(s-s*0.1)&&x<(s+s*0.1))
printf("You are wan mei!\n");
if(x>=(s+s*0.1))
printf("You are tai pang le!\n");
if(x<=(s-s*0.1))
printf("You are tai shou le!\n");
}
return 0; }
L1-032 Left-pad (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第35张图片](http://img.e-com-net.com/image/info8/79564fcd297747feb2b6bd77a89f5bb7.jpg)
#include
#include
using namespace std;
int main()
{
string a;
char c;
int N,i;
cin >> N >> c;
getchar();
getline(cin, a);
if (a.size() > N)
{
for(i=a.size()-N;i<a.size();i++)
cout<<a[i];
}
else
{
a.insert(0,N-a.size(), c);
cout << a;
}
return 0;
}
L1-033 出生年 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第36张图片](http://img.e-com-net.com/image/info8/245ff77121cd4d0e85cbaea4387c2124.jpg)
#include
#include
#include
using namespace std;
int main()
{
int year, N,i;
cin >> year >> N;
int year1 = year;
int year2 = year;
while (1)
{
year1 = year;
set<int>s;
for (i = 0; i < 4; i++)
{
s.insert(year1 % 10);
year1 /= 10;
}
if (N == s.size())
{
printf("%d %04d", year - year2, year);
break;
}
else year++;
}
}
L1-034 点赞 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第37张图片](http://img.e-com-net.com/image/info8/4ee7e2772c38433f8bc3bbe906baccda.jpg)
#include
#include
using namespace std;
int main()
{
int N,d,x;
cin >> N;
int i, j;
int a[1001] = { 0 };
for (i = 0; i < N; i++)
{
cin >> d;
for (j = 0; j < d; j++)
{
cin >> x;
a[x]++;
}
}
int max=0;
for (j = 0; j < 1001; j++)
{
if (a[max] <= a[j])
{
a[max] = a[j];
max = j;
}
}
cout << max << " " << a[max];
}
L1-035 情人节 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第38张图片](http://img.e-com-net.com/image/info8/5c66eddc060b486d88a4d8c81ec779c2.jpg)
#include
#include
using namespace std;
struct person {
string name;
}a[1001];
int main()
{
int n = 0, i;
char c = 'c';
int sign = 0;
while (1)
{
cin >> a[n].name;
if (n < 14 && n >= 2)sign = 1;
else if (n >= 14)sign = 2;
if (a[n].name == ".")break;
n++;
}
if (sign == 0) cout << "Momo... No one is for you ...";
if (sign == 1)
cout << a[1].name << " is the only one for you...";
if (sign == 2)
cout << a[1].name << " and " << a[13].name << " are inviting you to dinner...";
}
L1-036 A乘以B (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第39张图片](http://img.e-com-net.com/image/info8/7e9f356de182459aa02872bd2f77a75c.jpg)
#include
int main()
{
int A,B,i,sum=1;
scanf("%d%d",&A,&B);
sum=A*B;
printf("%d",sum);
return 0;
}
L1-037 A除以B (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第40张图片](http://img.e-com-net.com/image/info8/0fb1ab1d751f4a25b57a799c3fe807b9.jpg)
#include
int main()
{
double A,B;
scanf("%lf%lf",&A,&B);
if(B<0)
printf("%.0f/(%.0f)=%.2f",A,B,A/B);
if(B>0)
printf("%.0f/%.0f=%.2f",A,B,A/B);
if(B==0)
printf("%.0f/0=Error",A);
return 0;
}
L1-038 新世界 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第41张图片](http://img.e-com-net.com/image/info8/ab5282abcb444c28a79d8557ad3b0be4.jpg)
#include
int main()
{
printf("Hello World\n");
printf("Hello New World");
return 0;
}
L1-039 古风排版 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第42张图片](http://img.e-com-net.com/image/info8/643ede6045d0493191435ab0e0114228.jpg)
#include
#include
using namespace std;
int main()
{
int n;
string s;
cin >> n;
getchar();
getline(cin, s);
int i, j, k = 0;
char a[1001][1001];
int lie = 0;
if (s.size() % n) lie = s.size() / n + 1;
else lie = s.size() / n;
for (i = lie - 1; i >= 0; i--)
{
for (j = 0; j < n; j++)
{
if(k<s.size())
a[j][i] = s[k++];
else a[j][i] = ' ';
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < lie; j++)
{
cout << a[i][j];
}
cout << endl;
}
}
L1-040 最佳情侣身高差 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第43张图片](http://img.e-com-net.com/image/info8/cecfcff4f36545fa991910428acde6c5.jpg)
#include
struct person{
char sex;
double high;
};
int main()
{
int n;
double h;
scanf("%d\n",&n);
struct person a[99];
for(int i=0;i<n;i++)
scanf("%c %lf\n",&a[i].sex,&a[i].high);
for(int i=0;i<n;i++)
{
if(a[i].sex=='M')
h=a[i].high/1.09;
if(a[i].sex=='F')
h=a[i].high*1.09;
printf("%.2f\n",h);
}
return 0;
}
L1-041 寻找250 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第44张图片](http://img.e-com-net.com/image/info8/1fbce423959544eb84329bd1c680d648.jpg)
#include
int main()
{
int a[9999],i=0;
while(1)
{
scanf("%d",&a[i]);
if(a[i]==250) break;
i++;
}
printf("%d\n",i+1);
return 0;
}
L1-042 日期格式化 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第45张图片](http://img.e-com-net.com/image/info8/9ba6e98103dd45e7abe72dea69b723fd.jpg)
#include
int main()
{
int m,d,y;
scanf("%d-%d-%d",&m,&d,&y);
printf("%04d-%02d-%02d",y,m,d);
return 0;
}
L1-043 阅览室 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第46张图片](http://img.e-com-net.com/image/info8/9ebd1a0ff62945839a80294f83d23c07.jpg)
#include
#include
typedef struct n
{
int hour;
int m;
}time;
int solve(time a,int b,int c)
{
return (b-a.hour)*60+c-a.m;
}
main()
{
int n,num,a,b,flag[1002],sum,count;
char c;
time d[1005];
scanf("%d",&n);
for(int i=0;i<n;)
{ sum=0;
count =0;
for(int j=0;j<1002;j++)flag[j]=0;
for(;scanf("%d %c %d:%d",&num,&c,&a,&b)&&num!=0;)
{
if(c=='E')
{
if(flag[num]==1)
{
sum+=solve(d[num],a,b);
count++;
flag[num]=0;
}
}
else {
flag[num]=1;
d[num].hour=a;
d[num].m=b;
}
}
i++;
if(count!=0)
printf("%d %d\n",count,(int)(1.0*sum/count+0.5));
else printf("0 0\n");
}
}
L1-044 稳赢 (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第47张图片](http://img.e-com-net.com/image/info8/ad9235570e1b452097a58188162b098a.jpg)
#include
#include
int main()
{
char str[10];
char s[]="Bu";
char z[]="JianDao";
char w[]="ChuiZi";
int k,n=0;
scanf("%d",&k);
while(n<(k+1))
{ scanf("%s",str);
if(strcmp(str,"End")==0)
break;
else
{
if(n<k)
{
{ if(str[0]=='C')
{puts(s);
}
else
if(str[0]=='J')
{puts(w);
}
else
if(str[0]=='B')
{ puts(z);
}
}
n++;
}
else
{
puts(str);
n=0;
}
}
}
return 0;
}
L1-045 宇宙无敌大招呼 (5 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第48张图片](http://img.e-com-net.com/image/info8/3213bed034c147639e18350619c72b16.jpg)
#include
int main()
{
char s[10];
scanf("%s",s);
printf("Hello %s",s);
return 0;
}
L1-046 整除光棍 (20 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第49张图片](http://img.e-com-net.com/image/info8/e8dcc12699a443b9abdd39bd78530356.jpg)
#include
int main()
{
int x,s=0,n=0;
scanf("%d",&x);
while(s<x)
{
s=s*10+1;
n++;
}
while(1)
{
printf("%d",s/x);
s%=x;
if(s==0) break;
s=s*10+1;
n++ ;
}
printf(" %d\n",n);
return 0;
}
L1-047 装睡 (10 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第50张图片](http://img.e-com-net.com/image/info8/28e405093a5443daaae453bc316db5b7.jpg)
#include
struct person{
char name[10];
int huxi;
int maibo;
int flag;
};
int main()
{
struct person a[11];
int N,i;
scanf("%d\n",&N);
for(i=0;i<N;i++)
a[i].flag=0;
for(i=0;i<N;i++)
{
scanf("%s%d%d",a[i].name,&a[i].huxi,&a[i].maibo);
if(a[i].huxi<15||a[i].huxi>20||a[i].maibo<50||a[i].maibo>70)
a[i].flag=1;
}
for(i=0;i<N;i++)
if(a[i].flag==1) printf("%s\n",a[i].name);
return 0;
}
L1-048 矩阵A乘以B (15 分)
![PTA团体程序设计天梯赛-练习集(L1-001~L1-048)_第51张图片](http://img.e-com-net.com/image/info8/fa5a003cf1c34330b8324810a9712303.jpg)
#include
using namespace std;
int main()
{
int x1, y1;
cin >> x1 >> y1;
int num1[100][100];
int num2[100][100];
int i, j,k;
int sum[100][100] = {0};
for (i = 0; i < x1; i++)
for (j = 0; j < y1; j++)
cin >> num1[i][j];
int x2, y2;
cin >> x2 >> y2;
int flag=0;
for (i = 0; i < x2; i++)
for (j = 0; j < y2; j++)
cin >> num2[i][j];
if (y1 != x2)printf("Error: %d != %d",y1,x2);
else
{
cout << x1 << " " << y2 << endl;
for (k = 0; k < x1; k++)
{
flag=0;
for (i = 0; i < y2; i++)
{
sum[k][i] = 0;
for (j = 0; j < x2; j++)
{
sum[k][i] += num1[k][j] * num2[j][i];
}
if(flag)
cout<<" ";
cout<<sum[k][i];
flag++;
}
cout << endl;
}
}
}