1 #include <stdio.h>
#include <string.h>
int count(const char str[], const char key[]);
int main()
{
char str[1000], key[1000];
gets(str);
gets(key);
printf(“子字串共出現 %d 次”, count(str, key));
return 0;
}
int count(const char str[], const char key[])
{
int len=strlen(key),i=0;
char *a=strstr(str,key);
while(a!=NULL)
{
i++;
a++;
a=strstr(a,key);
}
return i;
}
2 #include<stdio.h>
int main()
{
int x ,y , n ,m[100][100]={0},k=1;
scanf("%d",&n);
while(k<=n*n)
{
if(k==1)
{
x=n/2;
y=0;
}
else{
if(m[(y-1+n)%n][(x+1)%n]==0)
{
y=(y-1+n)%n;
x=(x+1)%n;
}
else{y=(y+1)%n;
}
}
m[y][x]=k;
k++;
}
for(y=0;y<n;y++){
for(x=0;x<n;x++)
{
printf("%d ",m[y][x]);
}
printf("\n");}
}
3 #include<stdio.h>
int main(){
int a[100][100]={0};
int b[100][3]={0};
int i,j,k,x,y,c=0;
scanf("%d%d",&y,&x);
for(i=0;i<y;i++){
for(j=0;j<x;j++){
scanf("%d",&a[y][x]);
if(a[y][x]!=0){
c++;
b[c][0]=i;
b[c][1]=j;
b[c][2]=a[y][x];
}
}
}
b[0][0]=y;
b[0][1]=x;
b[0][2]=c;
for(k=0;k<c+1;k++){
printf("%d %d %d\n",b[k][0],b[k][1],b[k][2]);
}
}
4 #include<stdio.h>
int main()
{
int ans,i,b=0,c=0;
char a[1000]={0};
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(i%2==0)
b+=a[i]-'0';
else
c+= a[i]-'0';
}
if(b>c)
ans=b-c;
else
ans=c-b;
if(ans%11==0)
printf("YES");
else
printf("NO");
return 0;
}
5 #include<stdio.h>
int main() {
int arr[1000];
int n, i, j, max_length=0, max_length_num, current_num, current_num_length=0, temp;
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for (i=0; i<n-1; i++)
{
for (j =i+1; j<n; j++)
{
if (arr[i] < arr[j])
{
temp =arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
current_num = arr[0];
current_num_length = 1;
for (i=1; i<n-1; i++)
{
if (current_num == arr[i])
{
current_num_length ++;
if (current_num_length > max_length)
{
max_length = current_num_length;
max_length_num = current_num;
}
}
else
{
current_num_length = 1;
}
current_num = arr[i];
}
printf("%d %d %d", arr[0], arr[n-1], max_length_num);
return 0;
}
6 #include<stdio.h>
int count(const char str[]);
int main()
{
char str[1000];
gets(str);
printf("共%d個字母",count(str));
return 0;
}
int count(const char str[])
{
char c[26]={0};
int i,sum=0;
for(i=0;str[i]!=0;i++)
{
if(str[i]>='A'&&str[i]<='Z')
c[str[i]-'A']=1;
else if(str[i]>='a'&&str[i]<='z')
c[str[i]-'a']=1;
}
for(i=0;i<26;i++)
sum+=c[i];
return sum;
}
7 #include<stdio.h>
int main()
{
int n, np;
int j, k;
scanf("%d", &n);
for(j = 1, k = 0;j <= n;j ++)
{
if(n % j == 0)
k ++;
}
if(k == 2)
{
printf("%d is ", n);
np = 0;
while(n > 0)
{
k = n % 10;
n = n / 10;
np = np * 10 + k;
}
for(j = 1, k = 0;j <= np;j ++)
{
if(np % j == 0)
k ++;
}
if(k == 2)
{
printf("emirp.");
}
else
{
printf("prime.");
}
}
else
{
printf("%d is not prime.", n);
}
}
8 #include<stdio.h>
int main()
{
int n;
int i, j;
int count;
int c[100],store[100];
int flag, a, temp;
scanf("%d", &n);
for(i=0;i<n;i++) {
a=0;
scanf("%d",&count);
for(j=0;j<count;j++) {
scanf("%d",&c[j]);
}
flag = 1;
while(flag==1) {
flag = 0;
for(j=0;j<count-1;j++) {
if(c[j]>c[j+1]) {
temp = c[j];
c[j] =c[j+1];
c[j+1] = temp;
flag =1;
a++;
}
}
}
store[i] = a;
}
printf("Optimal train swapping takes %d swaps.",store[0]);
for(i=1;i<n;i++) {
printf("\nOptimal train swapping takes %d swaps.", store[i]);
}
return 0;
}
9 #include <stdio.h>
int dice[6]={1,6,2,5,3,4};
void N(){
int t=dice[0];
dice[0]=dice[3];
dice[3]=dice[1];
dice[1]=dice[2];
dice[2]=t;
}
void S(){
int t=dice[0];
dice[0]=dice[2];
dice[2]=dice[1];
dice[1]=dice[3];
dice[3]=t;
}
void W(){
int t=dice[0];
dice[0]=dice[5];
dice[5]=dice[1];
dice[1]=dice[4];
dice[4]=t;
}
void E(){
int t=dice[0];
dice[0]=dice[4];
dice[4]=dice[1];
dice[1]=dice[5];
dice[5]=t;
}
int main(){
int n,i,j=0,str[100]={0};
char ch[10];
scanf("%d",&n);
do{
dice[0]=1;
dice[1]=6;
dice[2]=2;
dice[3]=5;
dice[4]=3;
dice[5]=4;
for(i=0;i<n;i++){
scanf("%s",ch);
switch(ch[0]){
case 'n':N();
break;
case 's':S();
break;
case 'w':W();
break;
case 'e':E();
break;
}
}
str[j]=dice[0];
j++;
scanf("%d",&n);
}while(n!=0);
for(i=0;i<j;i++){
printf("%d",str[i]);
if(i!=j-1){
printf("\n");
}
}
}
10 #include<stdio.h>
#include<string.h>
int main(){
char country[100][100];
int count[100]={0},n,i,j,k,now=0;
scanf("%d\n",&n);
for(i=0;i<n;i++){
char input[100];
fgets(input,100,stdin);
char *p;
int find=0;
p=strtok(input," ");
if(now!=0){
for(j=0;j<now;j++){
if(strcmp(p,country[j])==0){
count[j]++;
find=1;
}
}
if(!find){
strcpy(country[now],p);
count[now]++;
now++;
find=0;
}
}
else{
strcpy(country[now],p);
count[now]++;
now++;
}
for(j=0;j<now-1;j++){
for(k=j+1;k<now;k++){
if(strcmp(country[j],country[k])>0){
char t[100];
strcpy(t,country[j]);
strcpy(country[j],country[k]);
strcpy(country[k],t);
int temp=count[j];
count[j]=count[k];
count[k]=temp;
}
}
}
}
for(i=0;i<now;i++){
printf("%s %d",country[i],count[i]);
if(i!=now-1){
printf("\n");
}
}
}