Board logo

標題: e307. 請讓我留在你的回憶裡 [打印本頁]

作者: 李知易    時間: 2025-2-22 11:35     標題: e307. 請讓我留在你的回憶裡

[attach]20672[/attach]
mmap解法參考:
  1. #include<bits/stdc++.h>
  2. #include<sys/mman.h>
  3. #include<sys/stat.h>
  4. #include<fcntl.h>
  5. #include<unistd.h>
  6. using namespace std;
  7. int main()
  8. {
  9.        
  10.         int fd = fileno(stdin);
  11.         struct stat sb;
  12.         fstat(fd, &sb);
  13.         size_t length = sb.st_size;//以上是獲取檔案描述符
  14.        
  15.         char *buffer = (char *)mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, 0);
  16.        
  17.         int c = 0;
  18.         vector<char> ans;
  19.         for (int i = 0; i < length; i++) {
  20.             if (buffer[i] != ' ') {
  21.                     if(c % 2 == 1)
  22.                             ans.push_back(' ');
  23.                 ans.push_back(buffer[i]);
  24.                 c = 0;
  25.             } else {
  26.                 c++;
  27.             }
  28.         }
  29.         cout.write(ans.data(), ans.size());
  30.         cout << '\n';
  31.         return 0;
  32. }
複製代碼

作者: 高鋐鈞    時間: 2025-2-22 11:56

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string str;
  6.     getline(cin,str);
  7.     int a=0;
  8.     for(int i=0;i<str.length();i++){
  9.         char c=str[i];

  10.         if(c==' '){
  11.             a++;
  12.         }else{
  13.             if(a%2==0){

  14.             }else{
  15.                 cout<<" ";
  16.             }
  17.             cout<<c;
  18.             a=0;
  19.         }
  20.     }
  21. }
複製代碼

作者: 洪承廷    時間: 2025-2-22 11:56

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string a;
  6.     getline(cin,a);
  7.     int sum=0;
  8.     for(int i=0;i<a.length();i++)
  9.     {
  10.         if(a[i]==' ')
  11.         {
  12.             sum++;
  13.             if(a[i+1]!=' ')
  14.             {
  15.                 if(sum%2==1)
  16.                     cout<<" ";
  17.                 sum=0;
  18.             }
  19.             else
  20.                 continue;
  21.         }
  22.         else
  23.             cout<<a[i];
  24.     }
  25.     return 0;
  26. }
複製代碼

作者: 楊惇翔    時間: 2025-2-22 12:00

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string str,ans;
  4. int main()
  5. {
  6.     getline(cin,str);
  7.     int cnt=0;
  8.     for(int i=0;i<str.length();i++)
  9.     {
  10.         if(str[i]!=' ')
  11.         {
  12.             if(cnt%2!=0)
  13.                 ans+=' ';
  14.             ans+=str[i];
  15.             cnt=0;
  16.         }
  17.         if(str[i]==' ')
  18.             cnt++;
  19.     }
  20.     cout<<ans;
  21.     return 0;
  22. }
複製代碼

作者: 孫子傑    時間: 2025-2-22 12:02

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     string str;
  5.     getline(cin,str);
  6.     int space=0;
  7.     for(int i=0;i<str.length();i++)
  8.     {
  9.         if(str[i]==' ')
  10.         {
  11.            space++;
  12.         }
  13.         if(str[i]!=' ')
  14.         {
  15.             if(space%2!=0)
  16.             {
  17.                 cout<<" ";
  18.             }
  19.             cout<<str[i];
  20.             space=0;
  21.         }
  22.     }
  23.     return 0;
  24. }
複製代碼

作者: 張駿霖    時間: 2025-2-22 12:03

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string str,ans;
  4. int main()
  5. {
  6.     getline(cin,str);
  7.     int c=0;
  8.     for(int i=0;i<str.length();i++)
  9.     {
  10.         if(str[i]!=' ')
  11.         {
  12.             if(c%2!=0)
  13.                 ans+=' ';
  14.             ans+=str[i];
  15.             c=0;
  16.         }
  17.         if(str[i]==' ')
  18.             c++;
  19.     }
  20.     cout<<ans;
  21.     return 0;
  22. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2