1.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int n;
printf("请输入行列:\n");
scanf("%d",&n);
int i, j, arr[n][n];
printf("打印%d行杨辉三角\n", n);
for(i = 0; i < n; i++)
{
for (j = 0; j < n - i -1; j++)
{
printf(" ");
}
for(j = 0; j <= i; j++)
{
if( j == 0 || i == j)
{
arr[i][j] = 1;
}
else
{
arr[i][j]=arr[i - 1][j - 1] + arr[i - 1][j];
}
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
结果
2.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char n;
printf("请输入大写字母:\n");
n = getchar();
for(char i = 'A';i <= n;i++)
{
for(char j = 'A';j <= i;j++)
{
printf("%c", i);
}
printf("\n");
}
printf("\n");
for(char i = n;i >= 'A';i--)
{
for(char j = n;j >= n - i + 'A';j--)
{
printf("%c", i);
}
printf("\n");
}
return 0;
}
结果
3.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char killer;
for(killer = 'A'; killer <= 'D'; killer++)
{
if((killer != 'A') + 2 * (killer != 'B' && killer == 'C') + (killer == 'A' || killer == 'D') == 2)
printf("凶手是%c\n", killer);
}
return 0;
}
结果
4.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int num;
printf("请输入一个整数:\n");
scanf("%d", &num);
if(num % 3 == 0 && num % 5 == 0 && num % 7 == 0)
{
printf ("3 5 7\n");
}
else if(num % 3 == 0 && num % 5 == 0)
{
printf ("3 5 \n");
}
else if(num % 3 == 0 && num % 7 == 0)
{
printf ("3 7 \n");
}
else if(num % 7 == 0 && num % 5 == 0)
{
printf ("5 7 \n");
}
else if(num % 3 == 0)
{
printf ("3\n");
}
else if(num % 5 == 0)
{
printf ("5\n");
}
else if(num % 7 == 0)
{
printf ("7\n");
}
else
{
printf ("n\n");
}
return 0;
}
结果
5.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int i = 0;
int j;
char str[100];
gets(str);
j = strlen(str) - 1;
while(i < j)
{
char t = str[i];
str[i] = str[j];
str[j] = t;
i++;
j--;
}
puts(str);
i = 0;
j = 0;
while(str[i] != '\0')
{
while (str[j] != ' ' && str[j] != '\0')
{
j++;
}
int k = j - 1;
while(i < k)
{
char t = str[i];
str[i] = str[k];
str[k] = t;
i++;
k--;
}
while(str[j] == ' ')
{
j++;
}
i = j;
}
puts(str);
return 0;
}
结果
6.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
char str[100];
gets(str);
int i = 0;
int sum = 0;
while (str[i])
{
int n = str[i] - '0';
sum = sum *10 +n;
i++;
}
printf("转换成整数数字为:\n");
printf("%d\n", sum);
return 0;
}
结果
7.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int i = 0, j = 0;
char str[100];
printf("请输入字符串:\n");
gets(str);
for (i = 0; i < strlen(str); i++)
{
if(str[i] != ' ')
str[j++] = str[i];
}
str[j] = '\0';
printf("字符串删除空格为:\n");
puts(str);
return 0;
}
结果
8.代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int count = 0;
char str1[100];
int i;
int j;
printf("计算字符串长度:\n");
printf("请输入字符串:\n");
gets(str1);
for(i = 0; str1[i] != '\0'; i++)
count++;
printf("字符串长度为:%d\n", count);
str1[0] = 0;
printf("\n");
printf("拷贝字符串:\n");
char str2[100];
printf("请输入第一个字符串:\n");
gets(str1);
printf("请输入第二个字符串:\n");
gets(str2);
for(i = 0; str2[i]; i++)
str1[i] = str2[i];
str1[i] = 0;
printf("str1 = %s, str2 = %s\n", str1, str2);
str1[0] = 0;
str2[0] = 0;
printf("\n");
printf("连接字符串:\n");
printf("请输入第一个字符串:\n");
gets(str1);
printf("请输入第二个字符串:\n");
gets(str2);
for(i = 0; str1[i]; i++);
for(j = 0; str2[j]; j++)
{
str1[i] = str2[j];
i++;
}
str1[i] = 0;
printf("str1 = %s, str2 = %s\n", str1, str2);
str1[0] = 0;
str2[0] = 0;
i = 0;
j = 0;
printf("\n");
printf("比较字符串:\n");
printf("请输入第一个字符串:\n");
gets(str1);
printf("请输入第二个字符串:\n");
gets(str2);
while(str1[i] == str2[i])
{
if(str1[i] == 0)
break;
i++;
}
if(str1[i] > str2[j])
printf("str1>str2\n");
else if(str1[i]<str2[j])
printf("str1 < str2\n");
else
printf("str1 = str2\b");
return 0;
}
结果
思维导图