例题3-1
import java.util.Scanner;
import org.omg.Messaging.SyncScopeHelper;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int count=0;
while (cin.hasNext()){
String str;
str=cin.nextLine();
char[] s = str.toCharArray();
for (int i=0;i<str.length();i++){
if (s[i]=='"') {
if (count%2==0) System.out.printf("``");
else System.out.print("''");
count++;
}
else System.out.print(s[i]);
}
System.out.println();
}
}
}
例题3-2
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
char ss[100]={"`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./"};
int main (){
int n=strlen(ss);
string str;
while (getline(cin,str)){
for (int i=0;i<str.size();i++){
if (str[i]==' '){
printf (" ");continue;
}
for (int j=0;j<n;j++){
if (ss[j]==str[i]){
printf ("%c",ss[j-1]);
break;
}
}
}
putchar('\n');
}
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
char str1[100]="A 3 HIL JM O 2TUVWXY5";
char str2[100]="1SE Z 8 ";
char str[1005];
int main (){
//FIN;
while (scanf("%s",str)!=EOF){
int flag1=0,flag2=0;
int n=strlen(str);
for (int i=0,j=n-1;i<=j;i++,j--){
if (str[i]!=str[j]) flag1=1;
if(str[i]>='A'&&str[i]<='Z'&&(str1[str[i]-'A']!=str[j]||str1[str[i]-'A']==' ')) flag2=1;
else if(str[i]>='1'&&str[i]<='9'&&(str2[str[i]-'1']!=str[j]||str2[str[i]-'1']==' ')) flag2=1;
}
if (flag1&&flag2) printf ("%s -- is not a palindrome.\n\n",str);
else if (!flag1&&!flag2) printf ("%s -- is a mirrored palindrome.\n\n",str);
else if (!flag1&&flag2) printf ("%s -- is a regular palindrome.\n\n",str);
else if (flag1&&!flag2) printf ("%s -- is a mirrored string.\n\n",str);
}
return 0;
}
例题3-4
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define mset(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
int a[1005];
int temp[1005];
int count0[20];
int countt[20];
int main (){
int n;
int cc=1;
//FIN;
while (scanf ("%d",&n)&&n){
mset(countt,0);
for (int i=0;i<n;i++) scanf ("%d",&a[i]),countt[a[i]]++;
printf ("Game %d:\n",cc++);
while (1){
for (int i=1;i<=9;i++) count0[i]=countt[i];
for (int i=0;i<n;i++) scanf ("%d",&temp[i]);
if (temp[0]==0) break;
int c1=0,c2=0;
for (int i=0;i<n;i++)
if (a[i]==temp[i]) c1++,count0[a[i]]--,temp[i]=0;
for (int i=0;i<n;i++)
if (count0[temp[i]]>0) c2++,count0[temp[i]]--;
printf (" (%d,%d)\n",c1,c2);
}
}
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define mset(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
int main (){
int t;
scanf ("%d",&t);
while(t--){
int n;
scanf ("%d",&n);
int flag=1;
for (int i=n-100;i<n;i++){
int temp=i;
int tt=i;
while (tt){
temp+=tt%10;
tt/=10;
}
if (temp==n){
printf ("%d\n",i);
flag=0;
break;
}
}
if (flag) printf ("0\n");
}
return 0;
}
例题3-6
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
int main (){
int t;
//freopen("data.txt","r",stdin);
scanf ("%d",&t);
while (t--){
char str[105];
scanf("%s",str);
int n=strlen(str);
char minn='a';
char temp[105];
int index;
for (int i=0;i<n;i++){
if (minn>str[i]){
minn=str[i];
index=i;
}
}
for (int i=0;i<n;i++) temp[i]=str[(i+index)%n];
for (int i=0;i<n;i++){
if (str[i]==minn){
int flag=0;
for (int k=0;k<n;k++){
if (str[(i+k)%n]<temp[k]){
flag=1;break;
}
else if (str[(i+k)%n]>temp[k]) break;
}
if(flag)
for (int k=0;k<n;k++) temp[k]=str[(i+k)%n];
}
}
for (int i=0;i<n;i++) printf ("%c",temp[i]);
putchar('\n');
}
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define mset(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
int main (){
int t;
scanf ("%d",&t);
while (t--){
char str[100];
scanf ("%s",str);
int count0=0;
int sum=0;
for (int i=0;i<strlen(str);i++){
if (str[i]=='O') count0++,sum+=count0;
else count0=0;
}
printf ("%d\n",sum);
}
return 0;
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define mset(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
double ss[100];
int main (){
ss['C']=12.01;ss['H']=1.008;ss['O']=16;ss['N']=14.01;
int t;
//FIN;
scanf ("%d",&t);
while (t--){
char str[100];
scanf ("%s",str);
int n=strlen(str);
int i=0;
double sum=0.0;
while (i<n){
if (str[i+1]!='C'&&str[i+1]!='H'&&str[i+1]!='O'&&str[i+1]!='N'&&i+1<n){
int t1=0;
double t2=ss[str[i]];
i++;
while (str[i]!='C'&&str[i]!='H'&&str[i]!='O'&&str[i]!='N'&&i<n){
t1=t1*10+str[i]-'0';i++;
}
sum+=t1*t2;
}
else sum+=ss[str[i]],i++;
}
printf ("%.3lf\n",sum);
}
return 0;
}
习题3-3
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include<list>
#include <bitset>
#include <climits>
#include <algorithm>
#define mset(a,x) memset(a,x,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
typedef long long LL;
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
using namespace std;
char str[100005];
int count0[10];
int main (){
int t;
scanf ("%d",&t);
while (t--){
int n;
scanf ("%d",&n);
mset(count0,0);
for (int i=1;i<=n;i++){
int t=i;
while (t){
count0[t%10]++;
t/=10;
}
}
for (int i=0;i<10;i++){
if (i) putchar(' ');
printf ("%d",count0[i]);
}
putchar('\n');
}
return 0;
}
习题3-4
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main (){
int t;
scanf ("%d",&t);
int cc=0;
while (t--){
if (cc)putchar('\n');
cc++;
char str[105];
scanf ("%s",str);
int n=strlen(str);
int ff=0;
for (int i=1;i<=n/2;i++){
if (n%i==0){
int step=i;
int countx=0;
int flag=0;
while (1){
countx++;
int k=countx*step;
if (k+step>n)break;
for (int j=0;j<step;j++){
if (str[j]!=str[j+k]) flag=1;
}
if (flag) break;
}
if (!flag) {
printf ("%d\n",step);ff=1;
break;
}
}
}
if (!ff) printf ("%d\n",n);
}
return 0;
}
习题3-5
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main (){
int cc=1;
//freopen("input","r",stdin);
while(1){
int x,y;
string mapx[10];
for (int i=0;i<5;i++){
getline(cin,mapx[i]);
if (mapx[0][0]=='Z'&&mapx[i].size()==1) goto FF;
for(int j=0;j<5;j++){
if (mapx[i][j]==' ') {
x=i;
y=j;
}
}
}
if(cc!=1) putchar('\n');
printf ("Puzzle #%d:\n",cc++);
int flag=0;
while (1){
char ch=getchar();
if (ch=='\n') continue;
if (ch=='0') break;
if (ch=='L'&&!flag){
int ty=y-1;
if (ty<0){
flag=1;continue;
}
swap(mapx[x][y],mapx[x][ty]);
y=ty;
}
else if (ch=='R'&&!flag){
int ty=y+1;
if (ty>=5){
flag=1;continue;
}
swap(mapx[x][y],mapx[x][ty]);
y=ty;
}
else if (ch=='A'&&!flag){
int tx=x-1;
if (tx<0){
flag=1;continue;
}
swap(mapx[tx][y],mapx[x][y]);
x=tx;
}
else if(ch=='B'&&!flag){
int tx=x+1;
if (tx>=5){
flag=1;continue;
}
swap(mapx[tx][y],mapx[x][y]);
x=tx;
}
else flag=1;
}
getchar();
if (!flag){
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
if (j) putchar(' ');
cout<<mapx[i][j];
}
putchar('\n');
}
}
else printf ("This puzzle has no final configuration.\n");
}
FF :;
return 0;
}
习题3-6
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
char str[50][50];
int vis[50][50];
int main (){
int n,m;
int cc=1;
//FIN;
while (scanf ("%d%d",&n,&m)&&n){
mset(vis,0);
if (cc!=1) putchar('\n');
printf ("puzzle #%d:\n",cc++);
for (int i=0;i<n;i++) scanf ("%s",str[i]);
printf ("Across\n");
int sum=0;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (str[i][j]!='*'){
if (i-1<0||str[i-1][j]=='*'||j-1<0||str[i][j-1]=='*'){
int k;sum++;
vis[i][j]=sum;
printf ("%3d.",sum);
for (k=j;k<m;k++){
if (str[i][k]!='*')
printf ("%c",str[i][k]);
else break;
if (i-1<0||str[i-1][k]=='*'||k-1<0||str[i][k-1]=='*') sum++,vis[i][k]=sum-1;
}
sum--;
putchar('\n');
j=k;
}
}
}
}
printf ("Down\n");
sum=0;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (str[i][j]!='*'){
if (i-1<0||str[i-1][j]=='*'||j-1<0||str[i][j-1]=='*'){
int k;
printf ("%3d.",vis[i][j]);
for (k=i;k<n;k++){
if (str[k][j]!='*'){
printf ("%c",str[k][j]);
str[k][j]='*';
}
else break;
}
putchar('\n');
}
}
}
}
}
return 0;
}
习题3-7
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
char str[55][1005];
int main (){
int t;
scanf ("%d",&t);
while (t--){
int n,m;
scanf ("%d%d",&m,&n);
for (int i=0;i<m;i++) scanf ("%s",str[i]);
char temp[1005];
int sum=0;
for (int i=0;i<n;i++){
int s[10];
mset(s,0);
for (int j=0;j<m;j++){
if (str[j][i]=='A'){
s[0]++;
}
else if (str[j][i]=='C'){
s[1]++;
}
else if (str[j][i]=='G'){
s[2]++;
}
else if (str[j][i]=='T'){
s[3]++;
}
}
int maxn=0;
int index=0;
for (int k=0;k<4;k++){
if (maxn<s[k]){
maxn=s[k];
index=k;
}
}
if (index==0) temp[i]='A';
else if (index==1) temp[i]='C';
else if (index==2) temp[i]='G';
else if (index==3) temp[i]='T';
sum+=m-maxn;
}
for (int i=0;i<n;i++) printf("%c",temp[i]);
putchar('\n');
printf ("%d\n",sum);
}
return 0;
}
习题3-8
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
char str[55][1005];
int main (){
int t;
scanf ("%d",&t);
while (t--){
int n,m;
scanf ("%d%d",&m,&n);
for (int i=0;i<m;i++) scanf ("%s",str[i]);
char temp[1005];
int sum=0;
for (int i=0;i<n;i++){
int s[10];
mset(s,0);
for (int j=0;j<m;j++){
if (str[j][i]=='A'){
s[0]++;
}
else if (str[j][i]=='C'){
s[1]++;
}
else if (str[j][i]=='G'){
s[2]++;
}
else if (str[j][i]=='T'){
s[3]++;
}
}
int maxn=0;
int index=0;
for (int k=0;k<4;k++){
if (maxn<s[k]){
maxn=s[k];
index=k;
}
}
if (index==0) temp[i]='A';
else if (index==1) temp[i]='C';
else if (index==2) temp[i]='G';
else if (index==3) temp[i]='T';
sum+=m-maxn;
}
for (int i=0;i<n;i++) printf("%c",temp[i]);
putchar('\n');
printf ("%d\n",sum);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str1[500005];
char str2[500005];
int main (){
while (scanf ("%s %s",str1,str2)!=EOF){
int n=strlen(str1);
int m=strlen(str2);
int k=0;
int sum=0;
for (int i=0;i<n;i++){
while (k<m){
if (str2[k]!=str1[i])k++;
else {
sum++;k++;break;
}
}
}
if (sum==n){
printf ("Yes\n");
}
else printf ("No\n");
}
return 0;
}
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
struct node{
int l,r;
}a[10];
int cmp(node x,node y){
if (x.l==y.l) return x.r<y.r;
return x.l<y.l;
}
int main (){
while (1){
for (int i=0;i<6;i++){
if (scanf ("%d%d",&a[i].l,&a[i].r)==EOF) goto FF;
if (a[i].l>a[i].r) swap(a[i].l,a[i].r);
}
sort(a,a+6,cmp);
if (a[0].l!=a[1].l||a[0].r!=a[1].r||a[2].l!=a[3].l||a[2].r!=a[3].r||a[4].l!=a[5].l||a[4].r!=a[5].r){
printf ("IMPOSSIBLE\n");continue;
}
if (a[0].l==a[2].l&&((a[0].r==a[4].r&&a[2].r==a[4].l)||(a[0].r==a[4].l&&a[2].r==a[4].r)))
printf ("POSSIBLE\n");
else if (a[0].r==a[2].r&&((a[0].l==a[4].r&&a[2].l==a[4].l)||(a[0].l==a[4].l&&a[2].l==a[4].r)))
printf ("POSSIBLE\n");
else if (a[0].r==a[2].l&&((a[0].l==a[4].r&&a[2].r==a[4].l)||(a[0].l==a[4].l&&a[2].r==a[4].r)))
printf ("POSSIBLE\n");
else if(a[0].l==a[2].r&&((a[0].r==a[4].r&&a[2].l==a[4].l)||(a[0].r==a[4].l&&a[2].l==a[4].r)))
printf ("POSSIBLE\n");
else printf ("IMPOSSIBLE\n");
}
FF: ;
return 0;
}
#include <bits/stdc++.h>
#define gcd(a,b) __gcd(a,b)
#define mset(a,x) memset(a,x,sizeof(a))
#define FIN freopen("input","r",stdin)
#define FOUT freopen("output","w",stdout)
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAX=1e5+10;
typedef long long LL;
const LL mod=1e9+7;
using namespace std;
char str1[605];
char str2[605];
int main (){
while(scanf ("%s%s",str1+200,str2)!=EOF){
int len1=strlen(str1+200);
int len2=strlen(str2);
for (int i=0;i<200;i++) str1[i]='1';
for (int j=len1+200;j<600;j++) str1[j]='1';
int start=200-len2;
int end=200+len1;
int minn=INF;
for (int i=start;i<end;i++){
int flag=0;
for (int j=0;j<len2;j++){
if (str2[j]=='2'&&str1[i+j]=='2'){
flag=1;break;
}
}
if (!flag){
if (len2+i<=start+len1+len2&&i<=start+len2) minn=min(minn,start+len1+len2-i);
else if (len2+i<=start+len1+len2&&i>=start+len2&&i<=start+len1+len2) minn=min(minn,len1);
else if (len2+i>=start+len1+len2&&i>=start+len2&&i<=start+len2+len1) minn=min(minn,len2+i-start-len2);
else if (len2+i>=start+len1+len2&&i<=start+len2) minn=min(minn,len2);
}
}
printf ("%d\n",minn);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
char str[1005];
void cal(double &temp){
int n=strlen(str);
int i=0;
while (str[i]!='.'){
temp*=10;
temp+=(str[i]-'0');
i++;
}
i++;
int countx=1;
while (str[i]!='e'){
temp+=pow(10,-countx)*(str[i]-'0');
i++;countx++;
}
i++;
temp=log(temp);
int ttt=0;
while (i<n){
ttt*=10;
ttt+=(str[i]-'0');
i++;
}
temp=temp+log(10)*ttt;
}
int main (){
//freopen("input.txt","r",stdin);
while (scanf("%s",str)){
if (strcmp(str,"0e0")==0) break;
double temp=0;cal(temp);
for (int i=0;i<=9;i++){
for (int j=1;j<=30;j++){
double tt=0;
for (int k=0;k<i+1;k++){
tt+=pow(2,-(k+1));
}
double tt2=pow(2,j)-1;
double temp2=log(tt)+tt2*log(2);
if (fabs(temp-temp2)<1e-5){
printf ("%d %d\n",i,j); goto FF;
}
}
}
FF:;
}
return 0;
}