写在前面:勤奋练习!
Problem A: 编写函数:第几个英文字母 (Append Code)
#include
#include
int chseq(char ch)
{
int n=0;
if(ch<='Z'&&ch>='A') ch+=32;
if(ch<='z'&&ch>='a') n+=ch-'0'-48;
return n;
}
output(char ch,int n)
{
if(n%10==1) printf("%c is %dst character.",ch,n);
else if(n%10==2) printf("%c is %dnd character.",ch,n);
else if(n%10==3) printf("%c is %drd character.",ch,n);
else printf("%c is %dth character.",ch,n);
}
int main()
{
char ch = getchar();
int n = chseq(ch);
output(ch, n);
}
Problem B: 编写函数:有多少个数字 (Append Code)
#include
#include
int get_nums(int *n)
{
int i=0;
while(scanf("%d",n)!=EOF)
{
i++;
}
return i;
}
int main()
{
int n;
get_nums(&n);
printf("%d\n", n);
}
Problem C: 编写函数:字符串的长度 (Append Code)
#include
#define MAX_STR_LEN 1001
int str_len(char s[])
{
int i;
int len=0;
for(i=0;s[i]!='\0';i++)
{
len++;
}
return len;
}
int main()
{
int n;
char s[MAX_STR_LEN];
while(gets(s) != NULL)
printf("%d\n", str_len(s));
return 0;
}
Problem D: 编写函数:几点几分几秒 (Append Code)
???结构体我还是看不懂题目???
#include
#include
struct time{
int hh; // 时
int mm; // 分
int ss; //秒
}tm;
struct time to_time(int n)
{
tm.hh=0,tm.mm=0,tm.ss=0;
for(int i=n;i>=1;i--)
{
tm.ss++;
if(tm.ss==60)
{
tm.ss=0;
tm.mm++;
}
if(tm.mm==60)
{
tm.mm=0;
tm.hh++;
}
}
return tm;
}
int put_time(struct time t)
{
printf("%0.2d:%0.2d:%0.2d\n",tm.hh,tm.mm,tm.ss);
}
int main()
{
int n;
struct time tm;
while(scanf("%d", &n) != EOF)
{
tm = to_time(n);
put_time(tm);
}
return 0;
}
Problem E: 编写函数:递归的字符串回文 (Append Code)
#include
#define MAX_STR_LEN 110
int palindrome(char s[],int begin,int end)
{
if(s[begin]!=s[end])
{
return 0;
}
else
{
palindrome(s,begin+1,end-1);
}
return 1;
}
int main()
{
int len;
char s[MAX_STR_LEN];
scanf("%d %s", &len, s);
if(palindrome(s, 0, len - 1))
puts("Yes");
else
puts("No");
return 0;
}
Problem F: 编写函数:数组的下标 (Append Code)
#include
#include
int get_array(int a[])
{
int num;
scanf("%d",&num);
for(int i=0;i=0)
{
printf("%d",a[k]);
}
else
{
printf("%d",a[n+k]);
}
}
int main()
{
int size, arr[1000], m, k;
size = get_array(arr);
while(scanf("%d", &k) != EOF)
printf("%d\n", array_value(arr, size, k));
return 0;
}
Problem G: 求平方根
Problem H: 有多少人?
#include
#include
int main()
{
int i;
int num;
scanf("%d",&num);
for(i=1; i<=num; i++)
{
if((i%3==1)&&(i%4==2)&&(i%5==3))
{
printf("%d\n",i);
}
}
return 0;
}
Problem I: 打印字母菱形图案
#include
#include
int main()
{
int n;
scanf("%d",&n);
int i,zsum = 64;
int kgs = (n+1)/2*2-1;
for(i = 1;i <= (n+1)/2;i++)
{
int sum = 2*i-1;
int k;
for(k = 0;k < (kgs - sum)/2;k++)
printf(" ");
int zzsum = zsum++;
for(k = 0;k < sum;k++)
{
zzsum++;
printf("%c",zzsum);
}
printf("\n");
}
zsum--;
zsum--;
for(i = i-2;i > 0;i--)
{
int sum = 2*i-1;
int k;
for(k = 0;k < (kgs - sum)/2;k++)
printf(" ");
int zzsum = zsum--;
for(k = 0;k < sum;k++)
{
zzsum++;
printf("%c",zzsum);
}
printf("\n");
}
return 0;
}
Problem L: 最简分式
#include
#include
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
int a,b,n=1,x;
double m=1;
while(scanf("%d/%d",&a,&b)!=EOF)
{
double n=a/b;
if(n>=1)
{
if(a%b==0) printf("%d\n",a/b);
else
{
if(gcd(a,b)==1) printf("%d/%d\n",a,b);
else
{
n=gcd(a,b);
a/=n;
b/=n;
m=a/b;
x=a-m*b;
printf("%.0lf+%d/%d\n",m,x,b);
}
}
}
else if(n<1)
{
if(gcd(a,b)==1)
printf("%d/%d\n",a,b);
else
{
int n=gcd(a,b);
a/=n;
b/=n;
printf("%d/%d\n",a,b);
}
}
}
}
Problem J: 矩阵的行列交换(WA)
#include
#include
int main()
{
int line,row,n,temp;
int linei,linej,rowi,rowj;
char way[6];
char lline[]="line";
char rrow[6]="row";
char a[1010][1010]={0};
scanf("%d %d",&line,&row);
for(int i=0;i
Problem K: 十六进制转二进制
#include
#include
#include
using namespace std;
typedef struct hex
{
string er_hex;
string sl_hex;
} Hex;
Hex flag[16] = {
{"0000","0"},{"0001","1"},{"0010","2"},{"0011","3"},
{"0100","4"},{"0101","5"},{"0110","6"},{"0111","7"},
{"1000","8"},{"1001","9"},{"1010","a"},{"1011","b"},
{"1100","c"},{"1101","d"},{"1110","e"},{"1111","f"}};
int main()
{
/*for (int i = 0; i < 16; i++)
{
cout << flag[i].er_hex << " " << flag[i].sl_hex << endl;
}*/
string str;
cin >> str;
for (int j = 0; j < str.length(); j++)
{
for (int i = 0; i < 16; i++)
{
if(str[j] == flag[i].sl_hex[0])
cout << flag[i].er_hex;
}
}
cout << endl;
return 0;
}
Problem M: 梅森素数
#include
#include
#include
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int flag=1,j=0;
int m=(pow(2,n))-1;
if(n==2||n==3||n==5||n==7||n==13||n==17||n==19||n==31)
{
flag=1;
}
else
{
flag=0;
}
if(flag==0)
{
printf("no\n");
}
else
{
printf("%d\n",m);
}
}
return 0;
}