Problem J: 矩阵的行列交换
Problem F: 编写函数:数组的下标 (Append Code)
Problem D: 编写函数:几点几分几秒 (Append Code)
Problem B: 编写函数:有多少个数字 (Append Code)
Problem A: 编写函数:第几个英文字母 (Append Code)
Problem M: 梅森素数
Problem J: 矩阵的行列交换:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include<stdlib.h>
int main()
{
int a[102][102],t[101];
char b[9]; /定义一个字符串,用来输入'line''row'
int m,n,i,j,k,p;
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
while(scanf("%s",b)!=EOF)
{
scanf("%d",&k);
scanf("%d",&p);
if(b[0]=='l') /这是字符串的首字符,判断这一个就行了
{
for(j=0;j<n;j++)
{
t[j]=a[k][j];
a[k][j]=a[p][j];
a[p][j]=t[j];
}
}
else
{
for(i=0;i<m;i++)
{
t[i]=a[i][k];
a[i][k]=a[i][p];
a[i][p]=t[i];
}
}
for(i=0;i<m;i++)
{
printf("%d",a[i][0]);
for(j=1;j<n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
printf("\n");
}
}
Problem F: 编写函数:数组的下标 (Append Code):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define MAX_STR_LEN 101
int get_array(int a[])
{
int i,n,sum=0;
scanf("%d",&n); /先输入一个n,仔细审题,
/这样i赋值的时候会很省事
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); /再输入之后的数组
}
return n;
}
array_value(int a[], int n, int k)
{
int i,j,p;
if(k>=0)
return a[k];
else return a[n+k]; /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 E: 编写函数:递归的字符串回文 (Append Code)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define MAX_STR_LEN 101
int palindrome (char s[], int begin, int end)
{
if(end==1)
{
if(s[begin]==s[end])
return 1;
else return 0;
}
else if(begin==end||begin==end-1)
/两种情况,为什么begin==end-1,就行
return 1;
else if((s[begin]==s[end])&&begin<end)
{
if(palindrome(s,begin+1,end-1)) /递归
return 1;
else return 0;
}
else return 0;
}
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;
}
/第二种方法 较容易理解!!
#include <stdio.h>
#define MAX_STR_LEN 101
int palindrome(char s[], int begin, int end)
{
if (begin < end) {
if (s[begin] == s[end])
return palindrome(s, begin + 1, end - 1);
/直接返回
else
return 0;
} else
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 D: 编写函数:几点几分几秒 (Append Code)
数据库的应用
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
struct time
{
int h,m,s;
};
struct time to_time(int n) /函数名字:to_time
/函数类型 struct time
/把参数n换算为用结构体time表示的时间。
{
struct time tm; 定义可以是成员
tm.h=n/3600;
tm.m=(n%3600)/60;
tm.s=(n%3600)%60;
return tm;
};
int put_time(struct time t)
{
printf("%02d:%02d:%02d\n",t.h,t.m,t.s);
}
int main()
{
int n;
struct time tm;
while(scanf("%d", &n) != EOF)
{
tm = to_time(n);
put_time(tm);
}
return 0;
}
Problem B: 编写函数:有多少个数字 (Append Code)
解析:
int a[5]={1,2,3,4,5};
int *p = a;
*p++ 先取bai指针dup指向的zhi值(数组第一个元dao素zhuan1),再将指针p自增1;
cout << *p++; // 结果为 1
cout <<(*p++); // 1
(*p)++ 先取指针p指向的值(数组第一个元素1),再将该值自增1(数组第一个元素变为2
cout << (*p)++; // 1
cout <<((*p)++) //2
++p 先将指针p自增1(此时指向数组第二个元素)shu, 操作再取出该值
cout << ++p; // 2
cout <<(++p) //2
++*p 先取指针p指向的值(数组第一个元素1),再将该值自增1(数组第一个元素变为2)
cout <<++*p; // 2
cout <<(++*p) //2
注意:上面的每条cout输出,要单独输出才能得到后面的结果。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define SWAP(a,b,t) t=a; a=b; b=t;
int get_nums(int *sum)
{
int a,b;
*sum=0;
while(scanf("%d",&a)!=EOF)
{
(*sum)++; /++*sum也可;
/关键是*sum++是*(sum++),
/不能将sum值加一,而是加的地址
++运算符 等级高
}
return sum;
}
int main()
{
int n;
get_nums(&n);
printf("%d\n", n);
}
Problem A: 编写函数:第几个英文字母 (Append Code)
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
#include<stdio.h>
#include<stdlib.h>
int chseq(char ch)
{
int p;
if(ch>='a'&&ch<='z')
{
p=ch-'a'+1;
}
else if(ch>='A'&&ch<='Z')
{
p=ch-'A'+1; /注意要+1,才是正常值;
/这是char类型和int类型的转换
/(char)-‘0’=(int)
}
return p;
}
void output(char ch, int n)
{
if(n%10==1)
printf("%c is %dst character.",ch,n);
/是结尾为1的,所以要%10,而且不能输出1,输出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 M: 梅森素数
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
#define MAX_STR_LEN 1001
int main()
{
int p,i;
unsigned long long mp,j;
while(scanf("%d",&p)!=EOF)
{
if(p==61)
{
printf("2305843009213693951\n"); /这是一个特殊情况
continue;
}
mp=1;
for(i=0;i<p;i++)
{
mp*=2;
}
mp-=1;
for(j=2;j<=sqrt(mp);j++)
{
if(mp%j==0)
{
break;
}
}
if(j>sqrt(mp))
{
printf("%llu",mp);
}
else
printf("no");
printf("\n");
}
return 0;
}