UVa-10317 - Equating Equations

來源:互聯網
上載者:User

標籤:

一個即將高一的網友問的我的題。汗,我怎麼大一才接觸編程!

用STL寫的,當時UVa掛了,沒有去測試。之後發現TLE了,把vector改成數組依然如此。一時沒想到怎麼解決,先這樣吧,以後再看看。

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<sstream> 6 using namespace std; 7 const int maxx=20; 8 int main() 9 {10     //freopen("in.txt","r",stdin);11     //freopen("out.txt","w",stdout);12     string s;13     while(getline(cin,s))14     {15         stringstream ss(s);16         int v1[maxx]={};17         char v2[maxx]={};18         int p1=0,p2=0;19         bool flag=0;20         int sum=0;21         while(1)22         {23             flag=!flag;24             if(flag)25             {26                 int t;27                 ss>>t;28                 if(ss.fail()) break;29                 v1[p1++]=t;30                 sum+=t;31             }32             else33             {34                 char t;35                 ss>>t;36                 if(ss.fail()) break;37                 v2[p2++]=t;38             }39         }40         while(1)41         {42             int num=0;43             bool f1=0,f2=0;44             int i=0,j=0;45             num+=v1[i++];46             for(;i<p1;i++,j++)47             {48                 if(v2[j]==‘+‘) f2=0;49                 else f2=1;50                 if(v2[j]==‘=‘)51                 {52                     f1=1;53                     f2=0;54                 }55                 if(f1) f2=!f2;56                 if(!f2) num+=v1[i];57                 else num-=v1[i];58             }59             if(num==0)60             {61                 i=0;62                 j=0;63                 printf("%d",v1[i++]);64                 for(;i<p1;i++,j++)65                     printf(" %c %d",v2[j],v1[i]);66                 break;67             }68             else69             {70                 if(!next_permutation(v1,v1+p1))71                 {72                     printf("no solution");73                     break;74                 }75             }76         }77         printf("\n");78     }79 }

 

UVa-10317 - Equating Equations

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.