SNNU_CCCC训练赛 解题报告
题目链接:https://www.patest.cn/contests/gplt
L1-001 Hello World
签到题,用来学习如何提交代码的。
代码如下:
1 #include<iostream> 2 using namespace std ; 3 4 int main() 5 { 6 cout << "Hello World!"<<endl; 7 return 0 ; 8 }
L1-002 打印沙漏
题意: 给定一个整数n和一个符号,用这个符号打印一个沙漏(每行都是奇数个符号,依次递减2,再依次递增2) 然后再输出有几个符号没有被用到?
做法:
大模拟题,硬刚就是了。注意就算所有符号都用到了,最后也要输出0.
这道题自己变量定义太乱了,东一榔头西一棒子的。以后这方面要注意思路清晰,尽量结构化函数化。这次暴露了自己思路不清晰,变量定义太乱,想到什么定义什么的毛病。
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<cmath> 6 #include<map> 7 #include<vector> 8 #include<set> 9 using namespace std ; 10 11 int main() 12 { 13 int n ; 14 char a ; 15 cin >> n >> a ; 16 int temp = 1 , cnt = 3 ; 17 while( temp + cnt + cnt <= n ) 18 { 19 temp+=cnt+cnt; 20 cnt+=2; 21 } 22 23 cnt -= 2 ; 24 int cntt = cnt ; 25 //´òÓ¡cnt¸ö* , 26 while( cnt != 1 ) 27 { 28 for( int j = 1 ; j <= (cntt - cnt ) / 2 ; j++) 29 cout <<' ' ; 30 for( int i = 1 ; i <= cnt ; i++) 31 cout << a ; 32 cout << endl ; 33 cnt-=2; 34 35 } 36 int cnttt = cntt ; 37 for( int i = 1 ; i <= (cnttt - 1 ) / 2 ; i++) 38 cout <<" "; 39 cout << a << endl ; 40 for( int i = 3 ; i <= cntt ; i+=2 ) 41 { 42 for( int k = 1 ; k <= (cnttt - i) / 2 ; k++) 43 cout <<" " ; 44 45 for( int j = 1 ; j <= i ; j++) 46 cout << a ; 47 cout << endl ; 48 } 49 cout << n - temp << endl ; 50 return 0 ; 51 52 }
L1-003 个位数统计
题意:给一个1000位的字符串,问你 0 - 9 依次有几个?
做法:利用字符串读入,然后注意ASCII码表 ,用 s[i] - '0' 得到实际数字后记录即可。
代码:
1 #include<iostream> 2 #include<cstring> 3 using namespace std ; 4 5 int main() 6 { 7 string n ; 8 cin >> n ; 9 int a[10]; 10 memset(a,0,sizeof(a)); 11 for( int i = 0 ; i < n.length() ; i++) 12 { 13 a[ n[i] - '0' ]++; 14 } 15 16 for( int i = 0 ; i <= 9 ; i++) 17 { 18 if( a[i] != 0 ) 19 cout << i <<":" << a[i] << endl ; 20 } 21 22 return 0 ; 23 }
L1-004 摄氏度温度
题意:给定一个华氏度,求摄氏度(保证输入输出均为整数)
做法:直接刚。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 using namespace std ; 5 int main() 6 { 7 int F ; 8 cin >> F ; 9 cout << "Celsius = " << 5*(F-32)/9 << endl ; 10 return 0 ; 11 }
L1-005 考试座位号
题意:给出学号a,试机位b,考试位c,n次查询,每次给出b,询问对应a和c是多少?(保证每个人座位唯一)
做法: 直接查找 然后输出
代码:
1 #include<iostream> 2 using namespace std ; 3 4 struct { 5 string s ; 6 int a , b ; 7 } a[1010]; 8 9 int main() 10 { 11 int n ; 12 cin >> n ; 13 for( int i = 1 ; i <= n ; i++) 14 cin >> a[i].s >> a[i].a >> a[i].b ; 15 int m ; 16 cin >> m ; 17 for( int i = 1 ; i <= m ; i++) 18 { 19 int temp ; 20 cin >> temp ; 21 for( int i = 1 ; i<= n ; i++) 22 { 23 if( a[i].a == temp ) 24 { 25 cout << a[i].s << ' ' << a[i].b <<endl ; 26 break ; 27 } 28 } 29 } 30 return 0 ; 31 }
L1-006 连续因子
题意:给出一个整数N ( 1 - 2^31) ,求最多的连续因子有几位?输出乘积最小的最多连续因子
做法: 本来我以为是数论题,在思考数论相关做法,比如质因数分解啊 公约数相关之类。后来发现14!就差不多是2^31了,因为求最小连续因子,很容易猜测出就在20以内,因为如果数字大了会包含小的连续因子(我的猜测)。
于是我先判断是否是素数,如果不是,就枚举20以内(第一次枚举14以内挂了一个点)
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std ; 7 8 bool is_prime( long long int n ) 9 { 10 if( n == 2 ) 11 return true ; 12 int temp = sqrt(n); 13 for( int j = 2 ; j <= temp ; j++) 14 if( n % j == 0 ) 15 return false ; 16 return true ; 17 } 18 int main() 19 { 20 long long n ; 21 cin >> n ; 22 int l = 0 , r = 0 , cnt = 0 ; 23 if( is_prime(n)) 24 { 25 cout << 1 << endl ; 26 cout << n << endl ; 27 return 0 ; 28 } 29 for( int i = 2 ; i <= 30 ; i++) 30 { 31 int temp = i; 32 int temp_cnt = 0 ; 33 int temp_chengfa = 1 ; 34 long long int nn = n ; 35 while( nn % temp == 0 ) 36 { 37 temp_chengfa *= temp ; 38 nn /= temp ; 39 temp_cnt++ ; 40 temp++; 41 42 43 } 44 if( temp_cnt > cnt && ( n % temp_chengfa == 0 ) ) 45 cnt = temp_cnt , l = i , r = temp-1 ; 46 continue ; 47 48 } 49 50 cout << cnt << endl ; 51 for( int i = l ; i <= r ; i++) 52 { 53 if( i == l ) 54 cout << i ; 55 else 56 cout << "*" << i ; 57 } 58 cout << endl ; 59 return 0 ; 60 }
L1-007 念数字
题意:给定一个数字,写出他们的汉语拼音。
做法:用一个字符数组映射一下就好了。
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<iostream> 5 using namespace std ; 6 const string NAME[] ={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; 7 8 int main() 9 { 10 string s ; 11 cin >> s ; 12 for( int i = 0 ; i < s.length() ; i++) 13 { 14 if( s[i] == '-' ) 15 cout << "fu"; 16 else 17 cout << NAME[s[i] -'0'] ; 18 if( i != s.length() - 1) 19 cout << ' ' ; 20 } 21 cout << endl; 22 return 0 ; 23 }
L1-008 求整数段和
题意:给定A ,B两个数,从小到大输出A和B之间所有数字(每五个一换行,数字占五格) , 并输出A到B之间的总和。
做法: 注意A和B可能A大B小需要交换,其他的直接搞起就好。
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<algorithm> 5 using namespace std ; 6 7 int main() 8 { 9 int a , b ; 10 cin >> a >> b ; 11 if( a > b ) 12 swap(a,b); 13 int sum = 0 ; 14 int cnt = 0 ; 15 for( int i = a ; i <= b ; i++) 16 { 17 printf("%5d",i); 18 cnt++; 19 if(cnt == 5 || i == b ) 20 { 21 cnt = 0 ; 22 printf("\n"); 23 } 24 25 sum+=i; 26 } 27 28 cout << "Sum = " << sum << endl ; 29 return 0 ; 30 31 }
L2-003 月饼
题意:给定N种月饼,每种月饼总价为Si,需求量D。求收益如何最大化?
做法: 贪心,计算月饼单价,排序,每次取月饼单价最高的,取完即可。注意需求量D可能大于月饼总和(在这里RE了一发)
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std ; 7 8 struct Point 9 { 10 double kucun , zongshoujia ; 11 double xingjiabi; 12 } a[10100]; 13 14 bool cmp( struct Point a , struct Point b) 15 { 16 return a.xingjiabi > b.xingjiabi; 17 } 18 int main() 19 { 20 int n , d ; 21 cin >> n >> d ; 22 for( int i = 1 ; i <= n ; i++) 23 cin >> a[i].kucun ; 24 for( int i = 1 ; i <= n ; i++) 25 cin >> a[i].zongshoujia ; 26 for( int i = 1 ; i <= n ; i++) 27 a[i].xingjiabi = (double) a[i].zongshoujia / a[i].kucun ; 28 sort(a+1,a+n+1,cmp); 29 double ans = 0 ; 30 31 int i = 1 ; 32 while( d != 0 ) 33 { 34 if( a[i].kucun <= d ) 35 { 36 ans+=a[i].xingjiabi * a[i].kucun ; 37 d = d - a[i].kucun ; 38 a[i].kucun = 0 ; 39 } 40 else 41 { 42 ans+= a[i].xingjiabi * d ; 43 a[i].kucun -= d; 44 d = 0 ; 45 } 46 i++; 47 if( i > n ) 48 break ; 49 } 50 printf("%.2lf\n",ans); 51 return 0 ; 52 }
L2-002 链表去重
应该是构造一个链表直接模拟,拿数组模拟不太熟练,我认为有更简单方法的……后来时间不太够,自己又太弱,没A。
L2-004 二叉搜索树
并不会做,输出一个NO骗了点分 (L2 - 001 002 也想这么骗分没骗到)
L3-012 水果忍者
被学长告知看这道题,感觉冥冥中就是很简单,毕竟输出只要输出任意两个符合要求的点就可以。
然而没看出来。有思路了
过段时间再补题吧,太困了- -#
得分情况:
L1-001 :5
L1-002 : 20
L1-003:15
L1-004:5
L1-005:15
L1-006:20
L1-007:10
L1-008:10
L2-003:25
L2-004:3
L3-012:1
Total:129
——————
自己还是太弱了,代码混乱,风格不清,符号变量瞎定义,想到啥是啥。
膜各路GG
滚粗了。
浙公网安备 33010602011771号