当前位置:网站首页>#PAT#day10
#PAT#day10
2022-07-06 02:43:00 【Mianmian fist beats the back 5r/ time】
List of articles
Introduction (2)—— The algorithm is preliminary (4.4 greedy )
1067 Sort with Swap(0, i)
My code (19/25)
#include<stdio.h>
//#include<algorithm>
//using namespace std;
int a[100010];
void swap(int &a,int &b){
int temp=a;
a=b;
b=temp;
}
int main(){
int n,change=0,flag=-1,pos_0,pos_x,t=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]==0) pos_0=i;
}
while(flag==-1){
if(a[0]!=0){
for(pos_x=0;pos_x<n;pos_x++){
if(a[pos_x]==pos_0) break;
}
swap(a[pos_0],a[pos_x]);
pos_0=pos_x;
change++;
}
else if(a[0]==0){
for(t++;t<n;t++){
if(a[t]!=t){
swap(a[0],a[t]);
pos_0=t;
change++;
break;
}
}
if(t==n) flag=1;// Orderly
}
}
printf("%d",change);
}
notes
- The reason for running timeout is that the time complexity is too high . I fixed the position and moved the element , So every time I look for
if(a[pos_x]==pos_0)
Need to be for loop . You can actually fix elements , Subscript with elements , And the storage location , So every time I lookif(a[pos_x]==pos_0)
No need for loop , And can directly locate !( Thinking is too fixed )
Modified code
#include<stdio.h>
//#include<algorithm>
//using namespace std;
int a[100010];
//int a[100];
void swap(int &a,int &b){
int temp=a;
a=b;
b=temp;
}
int main(){
int n,change=0,flag=-1,t=0,temp;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&temp);
a[temp]=i;
//if(temp==0) pos_0=i;
}
while(flag==-1){
if(a[0]!=0){
swap(a[0],a[a[0]]);
//pos_0=pos_x;
change++;
}
else if(a[0]==0){
for(t++;t<n;t++){
if(a[t]!=t){
swap(a[0],a[t]);
//pos_0=t;
change++;
break;
}
}
if(t==n) flag=1;// Orderly
}
}
printf("%d",change);
}
1038 Recover the Smallest Number
My code
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct Segment{
char it[17];
}segment[10010];
//
bool cmp(Segment a,Segment b){
strcat(a.it,b.it);
strcat(b.it,a.it);
return strcmp(a.it,b.it)<0;
}
int main(){
int n,total=0;
scanf("%d",&n);
while(n--){
char temp[9];
scanf("%s",temp);
int len_temp=strlen(temp),flag=-1;
for(int i=0;i<len_temp;i++){
if(temp[i]!='0'){
flag=1;
break;
}
}
if(flag==1){
strcpy(segment[total++].it,temp);
}
}
if(total==0) printf("0\n");
else{
sort(segment,segment+total,cmp);
int len0=strlen(segment[0].it);
int first_no_zero;
for(first_no_zero=0;first_no_zero<len0;first_no_zero++){
if(segment[0].it[first_no_zero]!='0'){
break;
}
}
for(int i=first_no_zero;i<len0;i++){
printf("%c",segment[0].it[i]);
}
for(int i=1;i<total;i++){
printf("%s",segment[i].it);
}
}
}
notes
- At first I didn't notice if all the inputs were ’000’ character string , Then the output should have a ’0’, This boundary situation .
- However, the last test point still failed after the modification :
- This problem uses a standard template library STL Inside string The type will be better , Maybe the first 6 One test point can pass .
边栏推荐
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- 会员积分营销系统操作的时候怎样提升消费者的积极性?
- DDoS "fire drill" service urges companies to be prepared
- 大厂镜像库
- 解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
- 2022.02.13
- What should we pay attention to when using the built-in tool to check the health status in gbase 8C database?
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
- MySQL winter vacation self-study 2022 11 (9)
猜你喜欢
High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis
Pat grade a 1033 to fill or not to fill
微服务间通信
[matlab] access of variables and files
Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
Redis delete policy
Reset nodejs of the system
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
Network Security Learning - Web vulnerabilities (Part 1)
PMP每日一练 | 考试不迷路-7.5
随机推荐
Zero basic self-study STM32 wildfire review of GPIO use absolute address to operate GPIO
在GBase 8c数据库中使用自带工具检查健康状态时,需要注意什么?
Day 50 - install vsftpd on ceontos6.8
2.13 simulation summary
如何精准识别主数据?
Black high-end responsive website dream weaving template (adaptive mobile terminal)
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Template_ Quick sort_ Double pointer
2022 China eye Expo, Shandong vision prevention and control exhibition, myopia, China myopia correction Exhibition
原型图设计
Pure QT version of Chinese chess: realize two-man, man-machine and network games
力扣今日题-729. 我的日程安排表 I
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
Redis installation
Deeply analyze the chain 2+1 mode, and subvert the traditional thinking of selling goods?
主数据管理(MDM)的成熟度
Force buckle 146 LRU cache
MySQL (IV) - transactions