C++题目
函数
int isalpha ( int c );
int isalpha ( int c );
isalpha() 函数用来检测一个字符是否是字母
int isdigit ( int c );
int isdigit ( int c );
isdigit() 用来检测一个字符是否是十进制数字。
十进制数字包括:0 1 2 3 4 5 6 7 8 9
int tolower ( int c );
tolower() 函数用来将大写字母转换为小写字母。
只有当参数 c 是一个大写字母,并且存在对应的小写字母时,这种转换才会发生。
int toupper ( int c );
toupper() 函数用来将小写字母转换为大写字母。
只有当参数 c 是一个小写字母,并且存在对应的大写字母时,这种转换才会发生。
题目
字符串
输入数据的每行包括若干行以空格分割开来的整数,输出每行中所有整数之和
#include <iostream>
#include<sstream>
#include<string>
using namespace std;
void test02()
{
string s;
while (getline(cin, s))
{
int sum = 0, x;
stringstream ss(s);
while (ss >> x)sum += x;
cout << sum << endl;
}
}
int main()
{
test02();
system("pause");
system("cls");
return 0;
}
结构体
#include <iostream>
using namespace std;
struct Point
{
int x, y;
Point(int x=0,int y=0):x(x),y(y){}
};
Point operator+(Point& a, Point& b)
{
return Point(a.x + b.x, a.y + b.y);
}
ostream& operator<<(ostream& out, Point p)
{
out << "(" << p.x << "," << p.y << ")";
return out;
}
void test02()
{
Point a, b(1);
a.x = 5;
b.y = 2;
cout << a + b << endl;
}
int main()
{
test02();
system("pause");
system("cls");
return 0;
}
Tex中的引用 UVa
在TeX中,左双引号的" `` “(1左边的),右双引号是” ‘’ "(回车左边的).输入一篇包含双引号的文章,你的任务是把它转换成TeX的格式。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cstdlib>
void test02()
{
int c,q=1;
while ((c = getchar()) != EOF)
{
if (c == '"')
{
printf("%s", q ? "``" : "''");
q = !q;
}
else putchar(c);
}
}
int main()
{
test02();
system("pause");
return 0;
}
竖式问题
char *strchr(const char *s,char c);
sprintf,printf,fprintf 这三个函数是“亲兄弟”,printf输出到屏幕,fprintf输出到文件,sprintf输出到字符串。多数情况下,屏幕总是可以输出的,文件一般也可以写,但字符串就不一定了,应该保证写入的字符串有足够的空间
sprintf(puff, “%d%d%d%d%d”, abc, de, x, y, z);
#include<stdio.h>
#include<string.h>
#include<cstdlib>
void test02()
{
int count = 0;
char s[20], puff[99];
scanf("%s", s);
for (int abc = 111; abc <= 999; abc++)
{
for (int de = 11; de <= 99; de++)
{
int x = abc * (de % 10);
int y = abc * (de / 10);
int z = abc * de;
sprintf(puff, "%d%d%d%d%d", abc, de, x, y, z);
int ok = 1;
for (int i = 0; i < strlen(puff); i++)
{
if (strchr(s, puff[i]) == NULL) ok = 0;
}
if (ok)
{
printf("<%d>\n", ++count);
printf("%6d\n", abc);
printf("X%5d\n", de);
printf("------\n");
printf("%6d\n", x);
printf("%5d \n", y);
printf("------\n");
printf("%6d\n", z);
}
}
}
}
int main()
{
test02();
system("pause");
return 0;
}
WERTYU
把手放在键盘上面,稍微不注意就会往右错一位。这样,输入Q就会变成输入W,输入J会变成输入K
输入一个错位后的字符串,,输出打字员本来想打出的句子
#include<stdio.h>
#include<string.h>
#include<cstdlib>
void test02()
{
char s[]{ "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./" };
int i, c;
while ((c = getchar()) != EOF)
{
for (i = 1; s[i] && s[i] != c; i++);
if (s[i])putchar(s[i - 1]);
else putchar(c);
}
}
int main()
{
test02();
system("pause");
return 0;
}
生成元
如果x加上x的各个数字之和得到y,就说x是y的生成元。给出n(1<=n<=100000),求出最小生成元。
例如,n=216,121,2005时的解分别为198,0,1979.
#include<stdio.h>
#include<string.h>
#include<cstdlib>
#define maxn 100005
int a[maxn];
void test02()
{
int t, n;
for (int i = 1; i < maxn; i++)
{
int x = i, j = i;
while (j)
{
x += j % 10;
j /= 10;
}
if (a[x] == 0 || i < a[x]) a[x] = i;
}
scanf("%d", &n);
while (n--)
{
scanf("%d", &t);
printf("%d\n", a[t]);
}
}
int main()
{
test02();
system("pause");
return 0;
}
环状序列
长度为n的环状串有n种表示法,分别从某个位置开始顺时针得到。字典序最小的称为“最小表示”
输入一个长度为n(n<=100)的环状DNA串(只包含A、C、G、T这四种字符)的一种表示法,你的任务是输出该环状串的最小表示法。例如CTCC的最小表示法是CCCT,CGAGTCAGCT的最小表示为AGCTCGACT
#include<stdio.h>
#include<string.h>
#include<cstdlib>
#define maxn 105
char a[maxn];
int less( int p, int q)
{
int n = strlen(a);
for (int i = 0; i < n; i++)
{
if (a[(i + p) % n] !=a[(i + q) % n])
return a[(i + p) % n] < a[(i + q) % n];
}
return 0;
}
void test02()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s", a);
int ans = 0;
int n = strlen(a);
for (int i = 1; i < n; i++)
{
if (less(i, ans)) ans = i;
}
for (int i = 0; i < n; i++)
{
putchar(a[(ans + i) % n]);
}
putchar('\n');
}
}
int main()
{
test02();
system("pause");
return 0;
}
字符串( UVa455)
如果一个字符串可以由某个长度为kk 的字符串重复多次得到,则称该串以kk 为周期。例如,abcabcabcabcabcabcabcabc 以33 为周期(注意,它也以66 和1212 为周期)。
输入一个长度不超过8080 的字符串,输出其最小周期。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cstdlib>
#define maxn 85
char a[maxn];
void test02()
{
int t;
scanf("%d", &t);
getchar();
while(t--)
{
scanf("%s", a);
int n = strlen(a);
for (int i = 1,x; i <=n; i++)
//此处必为 i<n,
//因为周期可能就是字符串本身长度,如果是i<n,不会打印出周期是本身的情况
{
if (n % i == 0)
{
for (x = i; x < n; x++)
{
if (a[x] != a[x % i])
break;
}
if (x == n)
{
printf("%d\n", i);
break;
}
}
}
if (t)putchar('\n');
}
}
int main()
{
test02();
system("pause");
return 0;
}
分子量 (UVa1586)
现给你若干个分子式需要你求分子量。
例如:C6H5OH的分子量为12.016+1.0085+16.00+1.008=94.108g/mol。
(ps:数字为数字前面元素的下标,所有分子式均无括号例如:2OH=2*16.00+1.008)
输入输出格式
输入:
输入n(2<=n<=99)
接下来n行为分子式
输出:
输出每个分子的分子式
#include<stdio.h>
#include<string.h>
#include<cstdlib>
#include<cctype>
char a[1000000];
double vex(char c)
{
switch (c)
{
case 'C': return 12.01;
case 'H': return 1.008;
case 'O': return 16.00;
case 'N': return 14.01;
}
}
void test02()
{
int n;
scanf("%d", &n);
getchar();
while (n--)
{
scanf("%s", a);
int len = strlen(a);
double k = 0;
double sum = 0;
for (int i = 0; i < len; i++)
{
k = 0;
if (isalpha(a[i]))
{
if (isdigit(a[i + 1]))
{
for (int j = i+1; j < len; j++)
{
if (isdigit(a[j]))
{
k = k * 10 + a[j] - '0';
}
else break;
}
sum += k*vex(a[i]);
}
else sum += vex(a[i]);
}
}
printf("%.3lf\n", sum);
}
}
int main()
{
test02();
system("pause");
return 0;
}
救济金发放 (UVa133)
题目描述 n(n<20)个人站成一圈,逆时针编号为 1~n。有两个官员,A从1开始逆时针数,B从n开始顺时针数。在每一轮中,官员A数k个就停下来,官员B数m个就停下来(两个官员有可能能停在同一个人上)。接下来被官员选中的1个或2个人离开队伍。
输入格式 输入n ,k ,m ,可能有多组数据,以 0 0 0结尾。
输出格式 输出每轮里被选中的人的编号(如果有两个人,先输出被A选中的)。输出的每个数应正好占3列。样例中的“ ␣ ”代表一个空格。
#include<stdio.h>
#include<string.h>
#include<cstdlib>
#include<cctype>
#define maxn 25
int n, k, m;
int a[maxn];
int go(int p, int d, int k)
{
while (k--)
{
do
{
p = (p + d + n - 1) % n + 1;
} while (a[p] == 0);
}
return p;
}
void test02()
{
while (scanf("%d%d%d", &n, &k, &m) == 3 && n)
{
int left = n;
for (int i = 1; i <= n; i++)
a[i] = i;
int p1 = n, p2 = 1;
while (left)
{
p1 = go(p1, 1, k);
p2 = go(p2, -1,m);
printf("%3d", p1);
left--;
if (p1 != p2)
{
printf("%3d", p2);
left--;
}
a[p1] = a[p2] = 0;
if (left)putchar(',');
}
putchar('\n');
}
}
int main()
{
test02();
system("pause");
return 0;
}