今天閒着沒事,就把浙大的上機題目整理做了下。。
第一題:
A + B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 462 Accepted Submission(s): 290
Problem Description
需要注意的是:A和B的每一位數字由對應的英文單詞給出.
three four + five six =
zero seven + eight nine =
zero + zero =
90
96
using namespace std;
string strnum[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int strToint(string str)
{
int i;
for(i=0;i<10;i++)
if(str==strnum[i])
return i;
}
int sum;
bool Input()
{
string first,second;
int num1=0,num2=0;
cin>>first;
cin>>second;
if(second!="+")
{
num1=strToint(first)*10+strToint(second);
cin>>first;
}else num1=strToint(first);
cin>>first;
cin>>second;
if(second!="=")
{
num2=strToint(first)*10+strToint(second);
cin>>second;
}else num2=strToint(first);
sum=num1+num2;
if(!num1&&!num2)return false;
else return true;
}
int main()
{
while(Input())
cout<<sum<<endl;
return 0;
}
第二題:
還是A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 799 Accepted Submission(s): 403
Problem Description
1 2 1 11 21 1 108 8 2 36 64 3 0 0 1
Sample Output
3 -1 -1 100
using namespace std;
int a,b,k;
int dive;
int main()
{
while(1)
{
dive=1;
cin>>a>>b>>k;
if(!a&&!b)break;
while(k--)
dive*=10;
if(a%dive==b%dive)
cout<<-1<<endl;
else
cout<<a+b<<endl;
}
return 0;
}
第三題:
火星A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 286 Accepted Submission(s): 117
Problem Description
1,0 2,1 4,2,0 1,2,0 1 10,6,4,2,1 0 0
Sample Output
1,0,1 1,1,1,0 1,0,0,0,0,0
#include <vector>
#include <sstream>
#include <string>
using namespace std;
vector <int> vec,tmp,fir,sec;
bool IsPrime(int n)
{
if(n==2)return true;
if(n%2==0)return false;
int i;
for(i=3;i*i<=n;i+=2)
if(n%i==0)return false;
return true;
}
void change(string str)
{
istringstream in(str);
tmp.clear();
int a;
char ch;
while(in>>a)
{
in>>ch;
tmp.push_back(a);
// cout<<a<<" ";
}
}
bool Input()
{
string first,second;
cin>>first>>second;
if(first=="0"&&second=="0")return false;
fir.clear();
sec.clear();
tmp.clear();
int len1=first.length(),i;
int len2=second.length();
change(first);
for(i=tmp.size()-1;i>=0;i--)
fir.push_back(tmp[i]);
// for(i=0;i<fir.size();i++)
// cout<<fir[i]<<" ";
change(second);
for(i=tmp.size()-1;i>=0;i--)
sec.push_back(tmp[i]);
len1=fir.size();
len2=sec.size();
if(len1>len2)
for(i=len2;i<len1;i++)
sec.push_back(0);
else
for(i=len1;i<len2;i++)
fir.push_back(0);
}
void Init()
{int i,k=0;
for(i=2;k<=30;i++)
if(IsPrime(i))
{
k++;
vec.push_back(i);
// cout<<i<<endl;
}
}
void Solve()
{
int i;
int len=fir.size();
for(i=0;i<len;i++)
fir[i]+=sec[i];
fir.push_back(0);
for(i=0;i<len;i++)
if(fir[i]>=vec[i])
{fir[i]%=vec[i];
fir[i+1]++;
}
if(fir[len])
cout<<fir[len]<<",";
for(i=len-1;i>0;i--)
cout<<fir[i]<<",";
cout<<fir[0]<<endl;
}
int main()
{
Init();
while(Input())
Solve();
return 0;
}
第四題:
最大連續子序列
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 545 Accepted Submission(s): 234
Problem Description
Nj },其中 1 <= i <= j <= K。最大連續子序列是所有連續子序列中元素和最大的一個,
例如給定序列{ -2, 11, -4, 13, -5, -2 },其最大連續子序列爲{ 11, -4, 13 },最大和
爲20。
在今年的數據結構考卷中,要求編寫程序得到最大和,現在增加一個要求,即還需要輸出該
子序列的第一個和最後一個元素。
素,中間用空格分隔。如果最大連續子序列不唯一,則輸出序號i和j最小的那個(如輸入樣例的第2、3組)。若所有K個元素都是負數,則定義其最大和爲0,輸出整個序列的首尾元素。
6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0
#include <cstdio>
using namespace std;
int num[10005];
int flag[10005];
int n;
bool Input()
{
//cin>>n;
scanf("%d",&n);
if(!n)return false;
int i;
for(i=0;i<n;i++)
//cin>>num[i];
scanf("%d",num+i);
return true;
}
void Solve()
{
flag[0]=num[0];
int max=flag[0];
int i,end=0,j,begin,sum=0;
for(i=1;i<n;i++)
{
if(flag[i-1]>=0)
flag[i]=flag[i-1]+num[i];
else
flag[i]=num[i];
if(flag[i]>max)
{
max=flag[i];
end=i;
}
}
if(max<0)
{
cout<<0<<" "<<num[0]<<" "<<num[n-1]<<endl;
return ;
}
else
{
for(j=end;j>=0;j--)
if((sum+=num[j])==max)
begin=j;
cout<<max<<" "<<num[begin]<<" "<<num[end]<<endl;
}
}
int main()
{
while(Input())
Solve();
return 0;
第五題:
暢通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 711 Accepted Submission(s): 315
注意:兩個城市之間可以有多條道路相通,也就是說
3 3
1 2
1 2
2 1
這種輸入也是合法的
當N爲0時,輸入結束,該用例不被處理。
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
1 0 2 998
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int n,m;
vector<int> road[1024];
int flag[1024];
bool Input()
{
scanf("%d",&n);
if(!n)return false;
int i,head,rear,tmp;
for(i=1;i<=n;i++)
road[i].clear();
memset(flag,0,sizeof(flag));
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d%d",&head,&rear);
road[head].push_back(rear);
road[rear].push_back(head);
}
return true;
}
void go(int i)
{
int len=road[i].size();
int j;
for(j=0;j<len;j++)
{if(flag[road[i][j]])continue;
flag[road[i][j]]=1;
go(road[i][j]);
}
}
void Solve()
{
int i;
int times=0,j;
for(i=1;i<=n;i++)
{
if(flag[i])continue;
flag[i]=1;
times++;
int len=road[i].size();
for(j=0;j<len;j++)
{
if(flag[road[i][j]]) continue;
flag[road[i][j]]=1;
go(road[i][j]);
}
}
cout<<times-1<<endl;
}
int main()
{
while(Input())
Solve();
return 0;
}