团体程序设计天梯赛-练习集L1 1-15(除了L1-009)

1.水

2.

 1 #include<bits/stdc++.h>//PTA打印沙漏 
 2 using namespace std;
 3 int main()
 4 {
 5     int N,s=1,n=2;
 6     char c;
 7     cin>>N>>c;
 8     while(N-s>=0)
 9     {               
10         s+=2*(2*n-1);
11         n++;
12     }
13     s-=2*(2*(n-1)-1);
14     n-=2;
15     //cout<<n<<" "<<N-s;
16     int a=0;
17     for(int i=n;i>=1;i--)
18     {
19         a++;
20         for(int j=1;j<a;j++)
21             cout<<" ";
22         for(int k=1;k<=2*i-1;k++)
23             cout<<c;
24         cout<<endl;
25     }
26     for(int i=2;i<=n;i++)
27     {
28         for(int j=1;j<=n-i;j++)
29             cout<<" ";
30         for(int k=1;k<=2*i-1;k++)
31             cout<<c;
32         cout<<endl;
33     }
34     cout<<N-s;
35     return 0;
36 }

3.

 1 #include<bits/stdc++.h>//PTA个位数统计
 2 using namespace std;
 3 int a[1010];
 4 char ch[1010];
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     cin>>ch;
 9     int n=strlen(ch);
10     for(register int i=0;i<n;i++)
11     {
12         int temp=ch[i]-'0';
13         a[temp]++;
14     }
15     for(register int i=0;i<10;i++)
16     {
17         if(a[i]!=0)
18         {
19             cout<<i<<":"<<a[i]<<endl;
20         }
21     }
22     return 0;
23 }

4.水

5.

 1 #include<bits/stdc++.h>//pta 考试座位号
 2 using namespace std;
 3 int n;
 4 struct node
 5 {
 6     long long num;
 7     int tel;
 8     int txt;
 9 }a[1010];
10 int b[1010];
11 int m;
12 int main()
13 {
14     std::ios::sync_with_stdio(false);
15     cin>>n;
16     for(register int i=1;i<=n;i++)
17     cin>>a[i].num>>a[i].tel>>a[i].txt;
18     cin>>m;
19     for(register int j=1;j<=m;j++)
20     cin>>b[j];
21     for(register int i=1;i<=m;i++)
22     {
23         for(register int j=1;j<=n;j++)
24         {
25             if(b[i]==a[j].tel)
26         {
27             cout<<a[j].num<<' '<<a[j].txt<<endl; 
28         }
29         }
30     }
31     return 0;
32 }

6.

 1 #include<bits/stdc++.h>//pta连续因子 
 2 using namespace std;
 3 int main() 
 4 {
 5     int N,temp;
 6     int count=0,mxc=0,start=0;
 7     cin>>N;
 8     for(int i=2;i<=sqrt(N);i++){
 9         temp=N;
10         count=0;
11         int j=i;
12         while(temp%j==0){
13                 temp/=j++;
14                 count++;            
15     }
16         if(count>mxc){ 
17             
18             mxc=count;
19             start=i;
20         }
21     }
22     if(mxc){
23 
24         cout<<mxc<<endl;
25         for(int i=0;i<mxc;i++){
26             cout<<start+i;
27             if(i!=mxc-1)
28                 cout<<"*";
29         }
30     }
31     else
32         cout<<"1"<<endl<<N;
33     return 0;
34 }

7.

 1 #include<bits/stdc++.h>//pta念数字 
 2 using namespace std;
 3 char ch[1000];
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     cin>>ch;
 8     int n=strlen(ch);
 9     for(register int i=0;i<n;i++)
10     {
11         switch(ch[i])
12         {
13             case'-':cout<<"fu";break;
14             case'0':cout<<"ling";break;
15             case'1':cout<<"yi";break;
16             case'2':cout<<"er";break;
17             case'3':cout<<"san";break;
18             case'4':cout<<"si";break;
19             case'5':cout<<"wu";break;
20             case'6':cout<<"liu";break;
21             case'7':cout<<"qi";break;
22             case'8':cout<<"ba";break;
23             case'9':cout<<"jiu";break; 
24         }
25         if(i<n-1)
26         cout<<' ';
27     }
28     return 0;    
29 } 

8.

 1 #include<bits/stdc++.h>//pta求整段数的和 
 2 using namespace std;
 3 int a;
 4 int b;
 5 int sum;
 6 int cnt;
 7 int main()
 8 {
 9     std::ios::sync_with_stdio(false);
10     cin>>a>>b;
11     for(register int i=a;i<=b;i++)
12     {
13         cnt++;
14         if(cnt%5==0&&i!=b)
15         cout<<right<<setw(5)<<i<<endl;
16         else
17         cout<<right<<setw(5)<<i;
18         sum+=i;
19     }
20     cout<<endl;
21     cout<<"Sum"<<' '<<"="<<' '<<sum<<endl;
22     return 0;
23 }

9.不太会

10.水了

11.

上来做错了一次,应该是考虑的不太周到;

错误:

 1 #include<bits/stdc++.h>pta A-B待修改版 
 2 using namespace std;
 3 string s1;
 4 string s2;
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     getline(cin,s1);
 9     getline(cin,s2);
10     for(register int i=0;i<s2.size();i++)
11     {
12         for(register int j=0;j<s1.size();j++)
13         {
14             if(s1[j]==s2[i])
15             {
16                 s1.erase(s1.begin()+j);
17             }
18             else
19             continue;
20         }
21     }
22     cout<<s1;
23     return 0;
24 }

正确AC:

 1 #include<bits/stdc++.h>//pta A-B 
 2 using namespace std;
 3 string s1,s2;
 4 int main()
 5 {
 6     std::ios::sync_with_stdio(false);
 7     getline(cin,s1);
 8     getline(cin,s2);
 9     int pos=0;
10     for(register int i=0;i<s2.length();i++)
11     {
12         while((pos=s1.find(s2[i]))!=-1)//如果查找成功则输出查找到的第一个位置,否则返回-1;
13         {
14             s1.erase(pos,1);//删除s1中出现重复的字符
15             //erase(pos,1);//删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
16         }
17     }
18     cout<<s1;
19     return 0;
20 }

12.水了

13.其实也挺水

 1 #include<bits/stdc++.h>//pta 计算阶乘和
 2 using namespace std;
 3 int n;
 4 long long sum;
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     cin>>n;
 9     for(register int i=1;i<=n;i++)
10     {    
11         long long res=1;
12         for(register int j=1;j<=i;j++)
13         {
14             res=res*j;
15         }
16         sum+=res;
17     }
18     cout<<sum;
19     return 0;
20 } 

14.大水题

15.

 1 #include<bits/stdc++.h>//pta 跟奥巴马一起画方块
 2 using namespace std;
 3 int n;
 4 char ch;
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     cin>>n>>ch;
 9     if(n%2==0)
10     {
11         for(register int i=1;i<=n/2;i++)
12     {
13         for(register int j=1;j<=n;j++)
14         {
15             cout<<ch; 
16             if(j==n)
17             if(i<n/2)
18             cout<<endl;
19         }
20     }
21     }
22     else
23     {
24         for(register int i=1;i<=n/2+1;i++)
25     {
26         for(register int j=1;j<=n;j++)
27         {
28             cout<<ch; 
29             if(j==n)
30             if(i<n/2+1)
31             cout<<endl; 
32         }
33     }
34     }
35     return 0;    
36 } 

 

posted @ 2022-04-11 17:06  江上舟摇  阅读(28)  评论(0编辑  收藏  举报