第三章数组和字符串下(๑•ᴗ•๑)
Uva1368 DNA Consensus String
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
int it[5 ];
char its[5 ]="ACGT" ;
int main(){
int T,n,m;
scanf ("%d" ,&T);
while (T--){
int errors = 0 ;
int num_A=0 ,num_C=0 ,num_G=0 ,num_T=0 ;
scanf ("%d%d" ,&n,&m);
char a[n][m];
char b[m];
for (int i = 0 ; i < n; i++)
scanf ("%s" ,a[i]);
for (int i = 0 ; i < m; i++){
memset (it, 0 , sizeof (it));
for (int j = 0 ; j < n; j++)
for (int q = 0 ; q < 4 ; q++)
if (its[q] == a[j][i]) {it[q]++;break ;}
if (it[0 ] >= max(max(it[1 ],it[2 ]),it[3 ]) ) b[i] = its[0 ];
else if (it[1 ] >= max(it[2 ],it[3 ]) ) b[i] = its[1 ];
else if (it[2 ] >= it[3 ] ) b[i] = its[2 ];
else b[i] = its[3 ];
for (int j = 0 ; j < n; j++)
if (a[j][i] != b[i]) errors++;
cout << b[i];
}
cout << endl << errors << endl;
}
return 0 ;
}
UVa202 Repeating Decimals
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std ;
int yu[4000 ];
int chu[4000 ];
int main(){
int n,m,i,k,l;
while (~scanf ("%d%d" ,&n,&m)){
bool flag = 1 ;
memset (yu, 0 , sizeof (yu));
memset (chu, 0 , sizeof (chu));
chu[0 ] = n/m;
yu[0 ] = n%m;
printf ("%d/%d = %d." ,n,m,chu[0 ]);
for (i = 1 ;i < 4000 ; i++){
yu[i] = yu[i-1 ]*10 % m;
chu[i] = yu[i-1 ]*10 / m;
if (yu[i] == 0 ){break ;}
}
for (k = 1 ; flag; k++)
for (l = k+1 ; l <= k+m; l++){
if (chu[k] == chu[l] && yu[k] == yu[l]){
flag = 0 ;
break ;
}
}
if (k==1 )cout <<'(' ;
for (int j = 1 ;; j++){
if (j == l) {cout << ')' <<endl ; break ;}
else if (j == k-1 ) {cout << '(' ;}
if (j <= 50 )cout <<chu[j];
else {cout << "...)" <<endl;break ;}
}
printf (" %d = number of digits in repeating cycle\n\n" ,l-k+1 );
}
return 0 ;
}
UVa 10340 All in All
#include<cstdio>
#include<iostream>
#include<string>
using namespace std ;
int main(){
string s,t;
int i,j;
while (cin >> s >> t){
for (i = 0 ,j = 0 ; s[i] && t[j];){
if (s[i]==t[j]){i++;j++;}
else j++;
}
if (s[i])printf ("No\n" );
else printf ("Yes\n" );
}
return 0 ;
}
UVa 1587 Box
//抄来的,这个头文件好清爽。。。
#include<bits/stdc++.h>
using namespace std
struct face{
int x,y
}a [6 ]
bool cmp(face a , face b){
if (a .x == b.x) return a .y > b.y
else return a .x > b.x
}
bool judge(){
if (memcmp(a ,a +1 ,sizeof(face)) || memcmp(a +2 ,a +3 ,sizeof(face)) ||
memcmp(a +4 ,a +5 ,sizeof(face))) return 0
if (a [1 ].x!=a [2 ].x||a [1 ].y!=a [4 ].x||a [2 ].y!=a [4 ].y) return false
return true
}
int main(){
while (cin>>a [0 ].x>>a [0 ].y>>a [1 ].x>>a [1 ].y>>a [2 ].x>>a [2 ].y>>a [3 ].x>>a [3 ].y>>a [4 ].x>>a [4 ].y>>a [5 ].x>>a [5 ].y){
for(int i=0
if (a [i].x<a [i].y)
swap(a [i].x,a [i].y)
sort(a ,a +6 ,cmp)
printf("%s\n" ,judge()?"POSSIBLE" :"IMPOSSIBLE" )
}
return 0
}
UVa 1588 Kickdown
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
using namespace std ;
int main(){
string a,b;
while (cin >> a >> b){
int n[300 ]={0 },i;
int n2[300 ]={0 };
bool flag = 1 ;
int maxn = 0 ;
for (i = 0 ; flag && a[i];i++){
for (int j = 0 ;b[j]; j++){
if (!a[i+j]&&b[j]){flag = 0 ;break ;}
n[i]++;
if (a[i+j]+b[j]>'1' +'2' ){n[i]=0 ;break ;}
}
maxn=max(maxn,n[i]);
}
flag = 1 ;
for (i = 0 ; flag && b[i];i++){
for (int j = 0 ;a[j]; j++){
if (!b[i+j]&&a[j]){flag = 0 ;break ;}
n2[i]++;
if (b[i+j]+a[j]>'1' +'2' ){n2[i]=0 ;break ;}
}
maxn=max(maxn,n2[i]);
}
cout << a.length()+b.length()-maxn <<endl;
}
return 0 ;
}
UVa 11809 Floating-Point Numbers
#include<cstdio>
#include<iostream>
#include<cmath>
#include<string>
using namespace std ;
long long E[20 ][40 ];
double M[20 ][40 ];
int main(){
int e=1 ;
for (int i = 0 ; i <=9 ; i++)for (int j = 1 ; j <= 30 ; j++){
double m = 1 - pow (0.5 ,i+1 ),e = pow (2 , j) - 1 ;
double t = log10 (m) + e*log10 (2 );
E[i][j] = (long long )t, M[i][j] = pow (10 ,t-E[i][j]);
}
string it;
while (cin >> it && it!="0e0" ){
double Man=0 ;
int Expo=0 ;
int i=0 ,j;
while (it[i] != '.' )
Man = Man*10 + it[i++]-'0' ;
j=i+1 ;
while (it[j] != 'e' )
Man += (double )pow (0.1 ,j-i)*(it[j++]-'0' );
while (it[++j])
Expo= Expo*10 +it[j]-'0' ;
while (Man<1 )Man*=10 ,Expo-=1 ;
for (int i = 0 ; i <= 9 ; i++)for (int j = 1 ; j <= 30 ; j++){
if (Expo == E[i][j])
if (fabs (Man - M[i][j]) < 1e-4 )
cout << i << " " << j << endl;
}
}
return 0 ;
}