1 数的整除
Time Limit:1000MS Memory Limit:65536K
题型: 编程题 语言: G++;GCC
描述
由键盘输入5个整数,逐个判断它们能否被27整除,能的输出“YES”,不能的输出“NO”(注意,输出时,一个判断结果占一行,5个数的判断共占5行)。
输入格式
用空格分隔
输出格式
一行一个判断
输入样例
8 27 17577 325 54
输出样例
NO
YES
YES
NO
YES
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n;
for(int i=0;i<5;i++)
{
scanf("%d",&n);
if(n%27==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
2 判断点是否在圆上
Time Limit:1000MS Memory Limit:65536K
题型: 编程题 语言: G++;GCC
描述
由键盘输入一个点的坐标, 要求编程判断这个点是否在单位圆(圆心在坐标0,0)上,点在圆上输出Y, 不在圆上输出N。
使用小数点后3位精度进行判断。
输入样例
0.707,0.707
输出样例
Y
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x,y;
scanf("%lf,%lf",&x,&y);
if(x*x+y*y>=0.999&&x*x+y*y<=1.001)
printf("Y");
else
printf("N");
return 0;
}
3 字符变换
Time Limit:1000MS Memory Limit:65536K
题型: 编程题 语言: G++;GCC
描述
由键盘输入一个句子(字符个数不定,最多不超过80个,以’\n’结束),将其中的大写字符变成小写(其它类型的字符不变),
最后输出变换后的句子。
输入样例
ThiS IS My fIrSt C ProgrAm!
输出样例
this is my first c program!
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char a[85];
gets(a);
for(int i=0;i<strlen(a);i++){
if(a[i]<='Z'&&a[i]>='A')
a[i]+=32;
}
printf("%s",a);
return 0;
}
4 回文串
Time Limit:1000MS Memory Limit:65536K
题型: 编程题 语言: G++;GCC
描述
读入一行字符串(不多于80个字符,以回车结束),判断该字符串是否为回文串(即从左向右拼写与从
右向左拼写是一样的),是则输出Y,不是则输出N。
输入格式
一行字符串
输出格式
是则输出Y,不是则输出N
输入样例
abba
输出样例
Y
提示
或
input:
abcba
output:
Y
或
input:
abc
output:
N
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char a[85];
int count=0;
gets(a);
for(int i=0;i<strlen(a)/2;i++){
if(a[i]==a[strlen(a)-1-i])
count++;
else
break;
}
if(count==strlen(a)/2)
printf("Y");
else
printf("N");
return 0;
}
5 统计单词个数
Time Limit:1000MS Memory Limit:65536K
题型: 编程题 语言: G++;GCC
描述
写一个函数实现:输入一行字符,以空格分割单词,回车结束输入,输出单词的个数
输入样例
There are many students and many trees!
输出样例
7
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char ch;
int mark=1;
int count=0;
while((ch=getchar())!='\n'){
if(ch!=' ')
mark=0;
else if(ch==' '&&mark==0){
mark=1;
count++;
}
}
printf("%d",count+1);
return 0;
}
6 大于平均分
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
输入10个整数,计算它们的平均值,并统计有多少个数比平均值大。
输入格式
10个整数
输出格式
比平均值在的数的个数
输入样例
0 1 2 3 4 5 6 7 8 9
输出样例
5
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int a[10];
int count=0,num=0;
for(int i=0;i<10;i++){
scanf("%d",&a[i]);
count+=a[i];
}
for(int i=0;i<10;i++){
if(a[i]>count/10)
num++;
}
printf("%d",num);
return 0;
}
7 字母统计
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
输入三行字符串(每行以换行回车为结束符),每行不超过80个字符。统计并输出其有多少个大写字母。
输入格式
三行字符串
输出格式
大写字母个数
输入样例
A-1 123
ABC abc
G
输出样例
5
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char a[10];
int count=0,num=0;
for(int i=0;i<3;i++){
gets(a);
for(int j=0;j<strlen(a);j++){
if(a[j]<='Z'&&a[j]>='A')
count++;
}
}
printf("%d",count);
return 0;
}
8 数的交换
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: G++;GCC;VC
描述
输入10个整数,把其中最小的数与第一个数交换,最大的数与最后一个数交换。使用3个函数解决问题:
(1) 输入10个整数的函数
(2) 进行交换处理的函数
(3) 输出10个数的函数
#include <stdio.h>
void input(int a[])
{
_______________________
}
void swap(int a[])
{
_______________________
}
void display(int a[])
{
int i;
for(i=0; i<10; i++)
printf("%d\n", a[i]);
}
int main()
{
int a[10];
input(a);
printf("input done\n");
swap(a);
printf("swap done\n");
display(a);
printf("display done\n");
return 0;
}
输入格式
输入10个整数
输出格式
输出结果,一行一个数字
输入样例
2 1 3 4 5 6 7 8 9 0
输出样例
input done
swap done
0
1
3
4
5
6
7
8
2
9
display done
int i;
for(i=0; i<10; i++)
scanf("%d",&a[i]);
int max=0,min=0;
for(int i=0;i<10;i++){
if(a[max]<a[i])
max=i;
if(a[min]>a[i])
min=i;
}
int temp;
temp=a[0];
a[0]=a[min];
a[min]=temp;
temp=a[9];
a[9]=a[max];
a[max]=temp;
10 元音字母
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: G++;GCC;VC
描述
#include "stdio.h"
void yuan(char *s,char *s2)
{
_______________________
}
main()
{
char str[81], str2[81];
gets(str);
yuan(str,str2);
printf("%s", str2);
}
编写一个函数,挑选一个字符串中的所有元音字母构成并返回一个新的字符串
int count=0,i;
for(i=0;i<strlen(s);i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='0'||s[i]=='U'||s[i]=='I'){
s2[count]=s[i];
count++;
}
}
s2[count]='\0';
函数不能调用函数?(OJ过不了)
int count=0,i;
for(i=0;i<81;i++){
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='0'||s[i]=='U'||s[i]=='I'){
s2[count]=s[i];
count++;
}
}
s2[count]='\0';
11 文本文件操作_读取与选择显示
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: GCC
描述
在当前目录中存在文件名为"case1.in"的文本文件,现要求打开该文件,读出里面的所有字符,只将其中的数字字符按先后顺序显示在屏幕上。
(如case1.in内容如下)
13 cats and 22 bikes
(在屏幕上输出结果如下)
1322
#include "stdio.h"
main()
{
FILE *fp;
char ch;
if((_______________________)==NULL)
return 0;
while(_______________________)
{
_______________________
}
fclose(fp);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
if((fp=fopen("case1.in","r"))==NULL)
return 0;
while((ch=fgetc(fp))!=EOF)
{
if(ch>='0'&&ch<='9')
putchar(ch);
}
fclose(fp);
}
1107 文本文件操作_单词的排序
时间限制:1000MS 代码长度限制:10KB
提交次数:4897 通过次数:1269
题型: 填空题 语言: GCC
Description
在当前目录有文件“case1.in”,文件里存放有多个(总个数不超过10000个)英文单词(每个英文单词不会超过10个字文字符), 每行一个,单词未排序。现要求,将文件中的所有单词按字典顺序排序,然后将排序好的单词写入新建的文件answer.txt中(注:文件存放于当前目录)。 请完成程序,实现该功能,(注意,填空题,请不要使用return 0结束,否则会影响评判而判错)
(如case1.in文件中原内容如下)
hello
bye
yes
(程序执行后,在文件answer.txt中内容如下)
bye
hello
yes
#include "stdio.h"
#include "string.h"
main()
{
_______________________
}
#include "stdio.h"
#include "string.h"
int main()
{
int i,j,n=0;
char w[10000][10],temp[10];
FILE *fp;
if((fp=fopen("case1.in","r"))==NULL) return 1;
while((fscanf(fp,"%s",w[n]))>0) n++;
fclose(fp);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(w[j],w[j+1])>0)
{
strcpy(temp,w[j]);
strcpy(w[j],w[j+1]);
strcpy(w[j+1],temp);
}
}
}
if((fp=fopen("answer.txt","w"))==NULL) return 1;
for(i=0;i<n;i++)
{
fprintf(fp,"%s\n",w[i]);
}
fclose(fp);
return 0;
}
12 链表创建与插入结点(填空)
Time Limit:1000MS Memory Limit:65535K
题型: 填空题 语言: G++;GCC;VC
描述
代码实现先创新一个链表,然后显示该链表,之后插入一个结点,再显示插入结点的链表。
请填空,完成该代码
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student *create(int n)
{
struct student *head=NULL,*p1=NULL,*p2=NULL;
int i;
for(i=1;i<=n;i++)
{ p1=(struct student *)malloc(LEN);
scanf("%ld",&p1->num);
scanf("%d",&p1->score);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
printf("%ld\t%d",p->num,p->score);
p=p->next;
printf("\n");
}
}
struct student *insert(struct student *head, struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head; p0=stud;
if(head==NULL)
{head=p0;}
else
{
while( (p0->num > p1->num) && (p1->next!=NULL) )
{
p2=p1;
p1=p1->next;;
}
if( p0->num <= p1->num )
{
if( head==p1 ) head=p0;
else p2->next=p0;
p0->next=p1;
}
else { p1->next=p0;}
}
return(head);
}
int main()
{
struct student *head,*stu;
int n;
scanf("%d",&n);
head=create(n);
print(head);
stu=(struct student *)malloc(LEN);
scanf("%ld",&stu->num);
scanf("%d",&stu->score);
stu->next = NULL;
head=insert(head,stu);
print(head);
return 0;
}
13 数据加密
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
n位的正整数(0<n<10),加密规则为:每位数字加6,然后用和除以8的余数再加1,代替该数字,即为密文数据。
现在由键盘输入正整数n,请编程输出密文数据。
输入格式
n位的正整数(0<n<10)
输出格式
密文数据
输入样例
123
输出样例
812
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char a[11];
int b[11];
gets(a);
int len=strlen(a);
for(int i=0;i<len;i++){
a[i]-='0';
b[i]=(a[i]+6)%8+1;
}
for(int i=0;i<len;i++){
printf("%d",b[i]);
}
return 0;
}
13 最长相同数字段
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
已知一个已经从小到大排序的数列,数列中连续出现的相同数字称为一段。
例如,一数列中有10个数:1,2,2,3,3,3,3,5,5,6,有5个段1,2-2,3-3-3-3,5-5,6。
编写一个程序,给定n个数的数列,把这个数列最长的段找出来。在上面的例子中最长的段由四个3构成。
如果最长的段不止一个,就输出数字最大的那一个。
输入格式
第一行有一个整数n(1<=n<=20),为数列元素的个数。第二行有n个整数,整数之间以一个空格分开。
输出格式
输出最长的段的数字,以及构成该段的数字个数
输入样例
10
1 2 2 3 3 3 3 5 5 6
输出样例
3 4
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int i,j,n,a[100],num,count=0,maxcount=0,maxnum,mark=1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i;j<n;j++){
mark=j;
while(a[mark++]==a[i]){
count++;
}
if(maxcount<=count){
maxcount=count;
maxnum=i;
}
count=0;
}
}
printf("%d %d",a[maxnum],maxcount);
return 0;
}
14 王教授的密码
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
王教授是一位数学家,他在上网时,总喜欢用4个数字给自己的帐号设置密码,
为了便于记忆密码,王教授选择的4个数字正好构成等差数列,例如下面四个数字
4 8 12 16
当每一次出现忘记4个密码数字中的某一个数字时,王教授都可以通过另外3个数推算出忘记
的数字,现在轮到你来算出被忘记的数字。按数列顺序给定还记得的3个数,问忘记的数是多少?
如果满足条件的数字不止一个,输出最小的那一个。
输入格式
第一行三个整数
输出格式
输出被忘记的数字(如果满足条件的数字不止一个,输出最小的那一个)
输入样例
2 4 8
输出样例
6
提示
密码是2 4 6 8这四个数。
#include <stdio.h>
int main() {
int a, b, c,d;
scanf("%d %d %d", &a, &b, &c); // 输入已知的3个数字
if(c-b==b-a){
if(a>b)
d=c-a+b;
else
d=a+b-c;
}
else if(c-b>b-a){
d=(c+b)/2;
}
else{
d=(a+b)/2;
}
printf("%d\n", d);
return 0;
}
}
14 求数列和
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
有下列数列:
![[b5eff71c44ff1bdb109b8cf88c2aaa3.jpg]]
请计算并输出该数列的前n项和,结果显示4位小数,4位之后的小数四舍五入。
注:请使用双精度浮点数,以确保所需的精度。
输入格式
一个正整数n(n<=100)
输出格式
数列前n项和
输入样例
3
输出样例
0.3368
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int n,a=1;
double sum=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
sum+=pow(-1,i+1)*a/(a+4);
a+=4;
}
printf("%.4lf",sum);
return 0;
}
15 真质数
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
找出正整数M和N之间(N不小于M)的所有真质数。
真质数的定义:如果一个正整数P为质数,且其反序也为质数,那么P就是真质数。
例如,11,13均为真质数,因为11的反序还是为11,13的反序为31也为质数。
输入格式
输入两个数M和N,空格间隔,1≤M≤N≤1000。
输出格式
按从小到大输出M和N之间(包括M和N)的真质数,每行一个。
如果之间没有真质数,则输出NO。
输入样例
10 35
输出样例
11
13
17
31
#include <stdio.h>
// 判断一个数是否为质数
int isPrime(int num) {
if (num <= 1) {
return 0; // 不是质数
}
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0; // 不是质数
}
}
return 1; // 是质数
}
// 反转一个整数
int reverse(int num) {
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
int main() {
int M, N;
scanf("%d %d", &M, &N); // 输入两个整数 M 和 N
int hasPrimes = 0; // 标记是否存在真质数
// 遍历 M 到 N 之间的所有数字
for (int i = M; i <= N; ++i) {
if (isPrime(i) && isPrime(reverse(i))) {
printf("%d\n", i);
hasPrimes = 1; // 存在真质数
}
}
// 如果不存在真质数,则输出 NO
if (!hasPrimes) {
printf("NO\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
int main(){
int a,b,n,m,mark1=1,mark2=1,mark3=1,j;
scanf("%d %d",&a,&b);
for(int i=a;i<=b;i++){
n=i;m=0;mark1=1;mark2=1;
while(n>0){
m=m*10+n%10;
n/=10;
}
for(j=2;j<=sqrt(i);j++){
if(i%j==0){
mark1=0;
break;
}
}
for(j=2;j<=sqrt(m);j++){
if(m%j==0){
mark2=0;
break;
}
}
if(mark1==1&&mark2==1){
printf("%d\n",i);
mark3=0;
}
}
if(mark3==1)
printf("NO");
return 0;
}
15 回文质数的个数
Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: G++;GCC
描述
求11到n之间(包括n),既是质数又是回文数的整数有多少个。
提示: 回文数指左右对称的数,如:11,131。
输入格式
一个正整数n(11<n<=100000)
输出格式
11到n(包括11和n)之间的回文质数的个数
输入样例
456
输出样例
10
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
// 判断一个数是否为质数
int P(int num) {
if (num < 2) {
return 0;
}
for (int i = 2; i <= sqrt(num); ++i) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
// 判断一个数是否为回文数
int h(int num) {
int num1 = num;
int num2 = 0;
while (num > 0) {
int t = num % 10;
num2 = num2 * 10 +t;
num /= 10;
}
return num1 == num2;
}
// 统计回文质数的个数
int countnum(int n) {
int count = 0;
for (int i = 11; i <= n; ++i) {
if (P(i) && h(i)) {
++count;
}
}
return count;
}
int main() {
int n;
scanf("%d", &n);
int result = countnum(n);
printf("%d\n",result);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
int n,mark=1,count=0;
scanf("%d",&n);
for(int i=11;i<=n;i++){
mark=1;
for(int j=2;j<=sqrt(i);j++){
if(i%j==0){
mark=0;
break;
}
}
if(mark){
int num1=i,num2=0;
while(num1>0){
num2=num1%10+num2*10;
num1/=10;
}
if(i==num2)
count++;
}
}
printf("%d",count);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
int main(){
int b,n,m,mark1=1,j,count=0;
scanf("%d",&b);
for(int i=11;i<=b;i++){
n=i;m=0;mark1=1;
while(n>0){
m=m*10+n%10;
n/=10;
}
for(j=2;j<=sqrt(i);j++){
if(i%j==0){
mark1=0;
break;
}
}
if(mark1==1&&m==i){
count++;
}
}
printf("%d",count);
return 0;
}
16 旋转菱形
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: G++;GCC
描述
给定一个N行的数字字符菱形。
实现将数字菱形顺时针旋转W度,W是90度的倍数。
输入格式
第一行是数字N,N小于等于10
之后是N行的数字菱形,数字字符无分隔
最后一行是一个非负整数,表示旋转W度,W是90的倍数
输出格式
旋转后的菱形,数字之间不分隔
输入样例
5
1
234
56789
321
3
90
输出样例
5
362
32731
184
9
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 旋转90度的函数
void r(int n, char diamond[][1000]) {
char temp[1000][1000]; // 初始化为空格
for (int i = 0; i <n; ++i) {
for (int j = 0; j<n; ++j) {
temp[i][j]=' ';
}
}
// 将原始菱形的每个元素顺时针旋转90度存放到临时数组中
for (int i = 0; i <n; ++i) {
for (int j = 0; j <n; ++j) {
temp[j][n - 1 - i] = diamond[i][j];
}
}
// 将临时数组的内容复制回原始菱形数组
for (int i = 0; i <n; ++i) {
for (int j = 0; j<n; ++j) {
diamond[i][j] = temp[i][j];
}
}
}
int main() {
//读取菱形
int N;
scanf("%d",&N);
getchar();//冲掉换行\n
char diamond[1000][1000];
for(int i=0;i<N;i++){
fgets(diamond[i],N+2,stdin);
int len=strlen(diamond[i]);
for(int j=len-1;j<=N;j++){
diamond[i][j]=' ';
}
}
// 计算旋转次数
int d;
scanf("%d", &d);
int rotations = d/90%4;
// 执行旋转操作
for (int i = 0; i < rotations; ++i) {
r(N, diamond);
}
// 输出旋转后的菱形
for (int i = 0; i <N; ++i) {
printf("%s\n", diamond[i]);
}
return 0;
}