还有一会儿要去剑道社训练。。。就整理整理好了。。。
习题3-1 得分 UVA1585
代码:
#include <iostream>
using namespace std;
int main(){
int n,i,score;
cin>>n;
char answer[500];
for(i=1;i<=n;i++){
score=0;
int O=1;
cin>>answer;
char *p;
p=answer;
while(*p!='\0'){
if(*p=='O'){
score=score+O;
O++;
}else{
O=1;
}
p++;
}
cout<<score<<endl;
}
return 0;
}
代码:
#include <iostream>
#include <stdio.h>
using namespace std;
double judge(char a){
double weight;
if(a=='C'){
weight=12.01;
}else if(a=='H'){
weight=1.008;
}else if(a=='O'){
weight=16.00;
}else{
weight=14.01;
}
return weight;
}
int main(){
int n,i,j,digit;
double total;
char a[500],b[1],c[10];
cin>>n;
for(i=1;i<=n;i++){
j=0;
total=0;
cin>>a;
char *p;
p=a;
while(*p!='\0'){
if(isalpha(*p)){
if(isalpha(*(p+1))||*(p+1)=='\0'){
total=total+judge(*p);
p++;
}else{
p++;
}
}else{
b[0]=*(p-1);
//cout<<"&"<<b[0]<<"&"<<endl;
j=0;
while(isalpha(*p)==0&&*p!='\0'){
c[j]=*p;
j++;
p++;
}
sscanf(c,"%d",&digit);
total=total+judge(b[0])*digit;
//cout<<"&"<<judge(b[0])<<" "<<sscanf(c,"%d",&digit)<<"&"<<endl;
}
}
printf("%.3f\n",total);
}
return 0;
}
3-3 数数字 UVA1225
代码:
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
int judge(char n){
int find[10]={0,1,2,3,4,5,6,7,8,9};
return find[n-48];
}
int main(){
int i,j,k,l,n;
int a;
cin>>n;
for(i=1;i<=n;i++){
cin>>a;
string father;
string son;
int result[10]={0,0,0,0,0,0,0,0,0,0};
for(j=1;j<=a;j++){
char t[10];
for(j=1;j<=a;j++){
sprintf(t,"%d",j);
son=t;
father=father+son;
}
for(j=0;j<father.length();j++){
result[judge(father[j])]++;
}
}
for(j=0;j<10;j++){
cout<<result[j];
if(j<9){
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
3-4 周期串 UVA455
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int main(){
string a;
int i,j,k,n;
cin>>n;
for(i=1;i<=n;i++){
cin>>a;
string b;
for(j=0;j<a.length();j++){
if(a.length()%(j+1)==0){
for(k=1;k<=a.length()/(j+1);k++){
b=b+a.substr(0,j+1);
}
if(a==b){
cout<<j+1;
cout<<endl;
if(i<n){
cout<<endl;
}
break;
}
}
b.clear();
}
}
}
3-5 谜题 UVA227
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int main(){
int i,j,k,puzzle=1,j0,k0,mark=0,mark1;
int god;
char a[6][100];
while(1){
for(j=0;j<5;j++){
for(k=0;k<5;k++){
a[j][k]='0';
}
}
for(j=0;j<100;j++){
a[5][j]='\0';
}
mark1=0;
mark=0;
god=1;
char kill;
for(j=0;j<5;j++){
i=0;
if(puzzle>1&&j==0){
kill=getchar();
}
while((kill=getchar())!='\n'){
if(kill=='Z'){
mark1++;
break;
}else if(kill==' '){
j0=j,k0=i;
}
a[j][i]=kill;
i++;
}
if(mark1==1)break;
}
for(j=0;j<5;j++){
for(k=0;k<5;k++){
if(a[j][k]=='0'){
j0=j,k0=k;
}
}
}
if(mark1==0){
i=0;
while((kill=getchar())!='0'){
if(kill!='\n'){
a[5][i]=kill;
i++;}
}
char *p;
p=a[5];
while(*p!='\0'){
char t;
if(*p=='A'){
if(j0-1<0){
mark++;
goto out;
}else{
t=a[j0-1][k0];
a[j0-1][k0]=' ';
a[j0][k0]=t;
j0=j0-1;
}
}else if(*p=='B'){
if(j0+1>4){
mark++;
goto out;
}else{
t=a[j0+1][k0];
a[j0+1][k0]=' ';
a[j0][k0]=t;
j0=j0+1;
}
}else if(*p=='L'){
if(k0-1<0){
mark++;
goto out;
}else{
t=a[j0][k0-1];
a[j0][k0-1]=' ';
a[j0][k0]=t;
k0=k0-1;
}
}else if(*p=='R'){
if(k0+1>4){
mark++;
goto out;
}else{
t=a[j0][k0+1];
a[j0][k0+1]=' ';
a[j0][k0]=t;
k0=k0+1;
}
}
p++;
}
if(puzzle>1){
cout<<"\n";
}
cout<<"Puzzle #"<<puzzle<<":"<<endl;
puzzle++;
for(j=0;j<5;j++){
for(k=0;k<5;k++){
cout<<a[j][k];
if(k<4) cout<<" ";
}
cout<<endl;
}
out:{if(mark==1){
if(puzzle>1){
cout<<"\n";
}
cout<<"Puzzle #"<<puzzle<<":"<<endl;
cout<<"This puzzle has no final configuration."<<endl;
puzzle++;
}
}
}
if(mark1!=0) break;
}
return 0;
}
代码:
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
struct use{
int number;
string line;
};
struct in{
int number;
char ch;
};
int main(){
int i,j,k,puzzle=1,i0,j0,across,down;
while(1){
use result[100];
in a[100][100];
for(i=0;i<100;i++){
for(j=0;j<100;j++){
a[i][j].ch='\0';
}
}
char son[100];
cin>>i0;
if(i0==0)break;
cin>>j0;
for(i=0;i<i0;i++){
for(j=0;j<j0;j++){
cin>>a[i][j].ch;
}
}
k=1;
for(i=0;i<i0;i++){
for(j=0;j<j0;j++){
if((i-1<0||j-1<0||a[i-1][j].ch=='*'||a[i][j-1].ch=='*')&&a[i][j].ch!='*'){
a[i][j].number=k;
k++;
}
}
}
across=0;
for(i=0;i<i0;i++){
char *p;
p=&a[i][0].ch;
k=0;
while(*p!='\0'){
for(j=0;j<100;j++) son[j]='\0';
j=0;
while(*p!='*'&&*p!='\0'){
son[j]=*p;
j++;
k++;
p=&a[i][k].ch;
}
if(son[0]!='\0'){
result[across].line=son;
result[across].number=a[i][k-result[across].line.length()].number;
across++;
}
k++;
p=&a[i][k].ch;
}
}
down=across++;
for(i=0;i<j0;i++){
char *p;
p=&a[0][i].ch;
k=0;
while(*p!='\0'){
for(j=0;j<100;j++) son[j]='\0';
j=0;
while(*p!='*'&&*p!='\0'){
son[j]=*p;
j++;
k++;
p=&a[k][i].ch;
}
if(son[0]!='\0'){
result[down].line=son;
result[down].number=a[k-result[down].line.length()][i].number;
//cout<<result[down].line<<" "<<result[down].number<<endl;
down++;
}
k++;
p=&a[k][i].ch;
}
}
if(puzzle>1){
cout<<endl;
}
cout<<"puzzle #"<<puzzle<<":"<<endl;
cout<<"Across"<<endl;
for(i=0;i<across-1;i++){
printf("%3d.", result[i].number);
cout<<result[i].line<<endl;
}
cout<<"Down"<<endl;
use t;
for(i=across-1;i<down;i++){
use *p;
p=&result[i];
for(j=i+1;j<down;j++){
if(result[i].number>result[j].number){
t=result[i];
result[i]=result[j];
result[j]=t;
}
}
}
for(i=across-1;i<down;i++){
printf("%3d.", result[i].number);
cout<<result[i].line<<endl;
}
puzzle++;
}
return 0;
}
代码:
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
char judge(int a,int g,int c,int t){
if(a>=g&&a>=c&&a>=t){
return 'A';
}else if(c>=g&&c>=t&&c>a){
return 'C';
}else if(g>=t&&g>a&&g>c){
return 'G';
}else{
return 'T';
}
}
int main(){
int s,i,j,k,m,n,l,different;
char result[1005];
char ch[55][1005];
cin>>s;
for(i=1;i<=s;i++){
for(j=0;j<1005;j++){
result[j]='\0';
}
for(j=0;j<55;j++){
for(k=0;k<1005;k++){
ch[j][k]='\0';
}
}
int a,g,c,t;
different=0;
cin>>n>>m;
for(j=0;j<n;j++){
for(k=0;k<m;k++){
cin>>ch[j][k];
}
}
l=0;
for(j=0;j<m;j++){
a=0,g=0,c=0,t=0;
for(k=0;k<n;k++){
if(ch[k][j]=='A'){
a++;
}else if(ch[k][j]=='G'){
g++;
}else if(ch[k][j]=='C'){
c++;
}else{
t++;
}
}
result[l]=judge(a,g,c,t);
l++;
}
for(j=0;j<n;j++){
for(k=0;k<m;k++){
if(result[k]!=ch[j][k]){
different++;
}
}
}
cout<<result<<endl;
cout<<different<<endl;
}
return 0;
}
3-8 循环小数 UVA202
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <stdio.h>
#include <cstring>
const int MAX=10000;
using namespace std;
int main(){
unsigned i,j,k,a,b,t;
char save[MAX],bf[MAX],af[MAX];
unsigned inter[MAX];
while(~scanf("%d%d",&a,&b)){
i=0,j=0,k=0,t=0;
cout<<a<<"/"<<b<<" = "<<a/b<<".";
if(a/b>0){
a=a-b*(a/b);
}
for(i=0;i<MAX;i++){
a=a*10;
inter[i]=a;
save[i]=a/b+48;
a=a-b*(a/b);
}
for(i=0;i<MAX;i++){
for(j=i+1;j<MAX;j++){
if(inter[i]==inter[j]){
goto here;
}
}
}
here:
if(j-i>50){
for(k=0;k<i;k++){
af[k]=save[k];
}
for(k=i;k<50;k++){
bf[k]=save[k];
}
for(k=0;k<3;k++){
bf[50+k]='.';
}
}else{
t=0;
for(k=0;k<i;k++){
af[k]=save[k];
}
for(k=i;k<j;k++){
bf[t]=save[k];
t++;
}
}
cout<<af<<"("<<bf<<")"<<endl<<" "<<j-i<<" = number of digits in repeating cycle"<<endl<<endl;
memset(save,0,sizeof(save));
memset(af, 0, sizeof(af));
memset(bf, 0, sizeof(bf));
memset(inter, 0, sizeof(inter));
}
return 0;
}
3-9 子序列 UVA10340
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
char a[100000],b[100000];
int main(){
int i,j,k,l;
while(~scanf("%s%s",a,b)){
l=0;
k=0;
for(i=0;i<strlen(a);i++){
for(j=i+l;j<strlen(b);j++){
if(a[i]==b[j]){
k++;
l=j-i;
break;
}
}
}
if(k==strlen(a)){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}
3-10 盒子 UVA1587
代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct side{
int x;
int y;
};
side box[6];
int compare(const void *element1,const void *element2){
side *p1,*p2;
p1=(side*)element1;
p2=(side*)element2;
if(p1->x==p2->x){
return p1->y-p2->y;
}else{
return p1->x-p2->x;
}
}
int main(){
int i,j,k,t;
while(~scanf("%d%d",&box[0].x,&box[0].y)){
if(box[0].x>box[0].y){
t=box[0].x;
box[0].x=box[0].y;
box[0].y=t;
}
for(i=1;i<6;i++){
cin>>box[i].x>>box[i].y;
if(box[i].x>box[i].y){
t=box[i].x;
box[i].x=box[i].y;
box[i].y=t;
}
}
qsort(box,6,sizeof(side),compare);
if (box[0].x==box[1].x&&box[0].y==box[1].y&&box[2].x==box[3].x&&box[2].y==box[3].y&&box[4].x==box[5].x
&&box[4].y==box[5].y&&box[0].x==box[2].x&&box[0].y==box[4].x&&box[2].y==box[5].y) {
cout<<"POSSIBLE"<<endl;
}else{
cout<<"IMPOSSIBLE"<<endl;
}
}
return 0;
}