第三章 数组和字符串(上)(๑•ᴗ•๑)
小白第三章,入门水题。题目可以在vj上搜
( Written by huchi)
UVa 272 Tex Quotes
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
bool flag =0;
char ch;
while((ch = getchar()) && ch != EOF)
{
if ('"' == ch)
{
flag = !flag;
if (flag) printf("``");
else printf("''");
}
else printf("%c",ch);
}
return 0;
}
UVa 10082 WERTYU
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char a[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";//注意转义字符
int main()
{
int i;
char ch;
while ((ch = getchar()) != EOF){
for (i =1 ; a[i] && a[i]!=ch; i++);
if (a[i]) cout<<a[i-1];
else cout<<ch;
}
return 0;
}
UVa 401 Palindromes
/*
Print a blank line after each test case - even after the last one.
Printing a blank line only in between each test case
will result in a verdict of wrong answer on UVa.
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char input[10000];
const char* ans[] = {"is not a palindrome.","is a regular palindrome.","is a mirrored string.","is a mirrored palindrome."};
const char mirr[40] = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
bool isPalindrome(char *a,int len)
{
bool flag1=1;
for(int i = 0; i < len>>1; i++)
if (a[i] != a[len-i-1])
flag1 = 0;
return flag1;
}
bool isMirrored(char *a, int len)
{
bool flag2 = 1;
for(int i = 0; i <= len>>1; i++)
{
if(a[i]>='A' && a[i]<='Z' && a[len-1-i]!=mirr[a[i]-'A'+0])
flag2 = 0;
if(a[i]>'0' && a[i] <='9' && a[len-1-i]!=mirr[a[i]-'1'+26])
flag2 = 0;
}
return flag2;
}
int main()
{
bool flag1,flag2;
while(scanf("%s",input)!=EOF){
int len = strlen(input);
flag1 = isPalindrome(input, len);
flag2 = isMirrored(input, len);
printf("%s -- %s\n\n", input, ans[flag2*2+flag1]);
}return 0;
}
UVa 340 Master-Mind Hints
//问题关键在于不同位置的情况下。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
int orign[100000];
int guess[100000];
using namespace std;
int main()
{
int len,T=0;//长度,次数
while(scanf("%d",&len)!=EOF && len != 0)
{
printf("Game %d:\n",++T);
for(int i = 0; i < len; i++)
scanf("%d",&orign[i]);//源码
while(true)
{
int sum = 0,A = 0,B = 0;//是否全为0,位置相同个数,不考虑位置个数
for(int i = 0; i < len; i++)
{
scanf("%d",&guess[i]);
sum+=guess[i];
if(guess[i] == orign[i]) A++;
}
if(!sum)break;
for(int i = 1; i <= 9; i++)
{
int c1 = 0, c2 = 0;//统计数字出现次数
for(int j = 0; j < len; j++)
{
if(orign[j] == i) c1++;
if(guess[j] == i) c2++;
}
B += min(c1,c2);
}
printf(" (%d,%d)\n",A,B-A);
}
}
return 0;
}
UVa 1583 Digit Generator
//打表就可以防止超时
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int biao[110000]={0};
int dict(int x)
{
int new_X = x;
while(x)
{
new_X += x%10;
x /= 10;
}
return new_X;
}
int main()
{
for(int i = 100000;i >= 0; i--)//打个表倒着打
biao[dict(i)] = i;
int T,num;
scanf("%d",&T);
for(int i = 0; i < T; i++)
{
scanf("%d",&num);
printf("%d\n",biao[num]);
}
return 0;
}
UVa 1584 Circular Sequence
//暴力更新(*^__^*)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int compare_and(const char *a,int st1, int st2)
{
int length_a = strlen(a);
for(int i = 0; i < length_a; i++)
if(a[ (st1 + i) % length_a ] != a[ (st2 + i) % length_a ])
return a[ (st1 + i) % length_a ] > a[ (st2 + i) % length_a ];
return 0;
}
int main()
{
int len;
scanf("%d",&len);
for(int i = 0; i < len; i++)
{
char a[200];
scanf("%s",a);
int ans = 0;
int length_a = strlen(a);
for(int j = 0; j < length_a; j++)
if(compare_and(a, ans, j)) ans = j;
for(int j = 0; j < length_a; j++)
printf("%c",a[(ans+j)%length_a]);
printf("\n");
}
return 0;
}
UVa 1585 Score
//立个小flag
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
for (int i = 0; i < T; i++)
{
char a[1000];
scanf("%s",a);
int flag = 0, lens, sum = 0;
lens = strlen(a);
for (int i = 0; i < lens; i++)
{
if (a[i]=='O')
sum += ++flag;
else if (a[i]=='X')
flag=0;
}
printf("%d\n",sum);
}
return 0;
}
UVa 1586 Molar mass
//写的有点乱。。。但总体思路是很显然的。。。。(我觉得)(๑•ᴗ•๑)
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
double mass_biao(char x)
{
double mass=0;
if(x == 'C') mass =12.01;
if(x == 'H') mass =1.008;
if(x == 'O') mass =16.00;
if(x == 'N') mass =14.01;
return mass;
}
int main()
{
int T;
scanf("%d",&T);
for(int j = 0; j < T; j++)
{
char compound[150];
scanf("%s",compound);
int len = strlen(compound);
double mass_sum = 0;
for(int i = 0; i < len -1 ;i++)
{
if(compound[i]>='A')
{
if(compound[i+1]>='A')
mass_sum += mass_biao(compound[i]);//后面没有数字
else//后面有数字的
{
int dishu = 0;
for(int k = 0; k < len-i-1;k++)
{
if(compound[i+1+k]<='9') dishu = dishu*10 + (compound[i+1+k]-'0');
else break;
}
mass_sum += mass_biao(compound[i])*dishu;
}
}
}
if(compound[len-1]>='A') mass_sum += mass_biao(compound[len-1]);
printf("%.3lf\n",mass_sum);
}
return 0;
}
UVa 1225 Digit Counting
//常规打表;-)
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int F[11000][10] = {0};
int dict(int i, int j)
{
int ans=0;
while(i)
{
if(j == i%10) ans+=1;
i /= 10;
}
return ans;
}
int main()
{
for (int i = 1; i < 10000; i++ )
{
for ( int j = 0; j < 10; j++)
{
F[i][j] = F[i-1][j] + dict(i, j);
}
}
int T;
scanf("%d",&T);
for (int i = 0; i < T; i++)
{
int a;
scanf("%d",&a);
for (int j = 0; j < 9; j++)
printf("%d ",F[a][j]);
printf("%d\n",F[a][9]);
}
return 0;
}
UVa 455 Periodic Strings
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int period(char *a)
{
int T = strlen(a);
int changdu = T;
for(int i = T; i > 0; i--)
if(T % i == 0)
{
bool flag = 1;
for(int k = 0; k < T; k++)
if(a[k%i] != a[k])
flag = 0;
if (flag) changdu = i;
}
return changdu;
}
int main()
{
int N,T;
scanf("%d",&N);
for (int i = 0; i < N; i++)
{
char a[100];
scanf("%s",a);
T = period(a);
printf("%d\n",T);
if(N-i!=1)printf("\n");
}
return 0;
}
UVa 227 Puzzle
//经过协商,我可能下了假的CB,ac的代码在我这里都不能过样例。就把这个垃圾代码贴出来把。。。。。⊙﹏⊙
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int turn=0;
while(1)
{
turn++;
//读入与判断
char puzzle[5][7];
int x=0,y=0,flag=0;
gets(puzzle[0]);
if(puzzle[0][0] == 'Z')break;
for(int i = 1; i < 5; i++)
gets(puzzle[i]);
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
if(puzzle[i][j] == ' ')
{
x=i;y=j;break;
}
//移动
char behavior[100];
int length;
for(length=0;;length++)
{
scanf("%c",&behavior[length]);
if(behavior[length] == '0')break;
}
getchar();
for(int i = 0; i < length; i++)
{
int temp;
int a, b;
if(behavior[i] == 'A')
{
a = x - 1; b = y;
}
if(behavior[i] == 'B')
{
a = x + 1; b = y;
}
if(behavior[i] == 'L')
{
a = x; b = y - 1;
}
if(behavior[i] == 'R')
{
a = x; b = y + 1;
}
if(a<0 || a>4 || b <0 || b>4)
{
flag = 1;
break;
}
else
{
temp = puzzle[x][y];
puzzle[x][y] = puzzle[a][b];
puzzle[a][b] = temp;
x = a; y = b;
}
}
//输出
if(turn >1)printf("\n");
printf("Puzzle #%d:\n",turn);
if(flag) {printf("This puzzle has no final configuration.\n");continue;}
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j < 4 ; j++)
printf("%c ",puzzle[i][j]);
printf("%c\n",puzzle[i][4]);
}
}
}
Uva 232 Crossword Answers
//PE毁一生,(>﹏<);
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct pos{
int x;
int y;
};
struct bo{
char a;
bool flag_a = false;
bool flag_b = false;
};
int main(){
int n,m;
int time=1;
while(~scanf("%d",&n) && n){
if(time>1) cout << endl;
cout << "puzzle #" << time++ << ":" << endl;
pos it[121];
bo mp[11][11];
scanf("%d",&m);
int num = 1;
for (int i = 1; i <= n; i++){
getchar();
for (int j = 1; j<= m; j++){
scanf("%c",&mp[i][j].a);
if(mp[i][j].a != '*'){
if(i==1 || j==1) {
it[num].x=i;
it[num++].y=j;
}
else if(mp[i-1][j].a == '*' || mp[i][j-1].a == '*'){
it[num].x=i;
it[num++].y=j;
}
}
}
}
//测试
//for (int i = 1; i < num; i++)
// cout << it[i].x << ' ' << it[i].y << endl;
//across
cout << "Across" << endl;
for(int i = 1; i < num; i++){
if (mp[it[i].x][it[i].y].flag_a)continue;
printf("%3d.",i);
for(int j = 0; it[i].y + j <= m; j++){
if(mp[it[i].x][it[i].y + j].a == '*')
break;
cout << mp[it[i].x][it[i].y + j].a;
mp[it[i].x][it[i].y + j].flag_a = 1;
}
cout << endl;
}
cout << "Down" << endl;
for(int i = 1; i < num; i++){
if (mp[it[i].x][it[i].y].flag_b)continue;
printf("%3d.",i);
for(int j = 0; it[i].x + j <= n; j++){
if(mp[it[i].x + j][it[i].y].a == '*')
break;
cout << mp[it[i].x + j][it[i].y].a;
mp[it[i].x + j][it[i].y].flag_b = 1;
}
cout << endl;
}
}
}
:)等下期。