第一题
#include <stdio.h>
double Factorial(int n) {
double sum = 1;
for (int i = 1; i < n; i++) {
sum *= i;
}
return sum;
}
double Array_Factorial(int* s, int n) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += Factorial(s[i]);
}
return sum;
}
int main()
{
int a[10];
double c=10;
for (int i = 0; i < 10; i++) {
a[i] = i+1;
}
c = Array_Factorial(a,10);
printf("结果是:%f",c);
}
第二题 //判断是否是回文数
bool isPalindrome(int n) {
int n1, n2, n3, n4;
n1 = n / 1000;
n2 = (n % 1000) / 100;
n3 = (n % 100) / 10;
n4 = n % 10;
int m;
if (n1 != 0) {
m = n4 * 1000 + n3 * 100 + n2 * 10 + n1;
}
else
{
m = n4 * 100 + n3 * 10 + n2;
}
if (n == m)
return true;
else
{
return false;
}
}
int main()
{
for (int i = 200; i <= 3000; i++) {
if (isPalindrome(i))
printf("%d\n", i);
}
}
第三题
#include<stdio.h>
int main(){
int a[10],b[10];
for (int i = 0; i < 10; i++)
scanf("%d",&a[i]);
for (int i = 0; i < 10; i++)
scanf("%d",&b[i]);
int sum=0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if(a[i]==b[j])
{
printf("%d", a[i]);
break;
}
}
}
return 0;
}
第四题
#include<stdio.h>
int main(){
int c1 = 0, c2 = 0, num = 0, space = 0, other = 0;
char ch;
while ((ch = getchar()) != '\n') {
if (ch <= 'z' && ch >= 'a') {
c1++;
}
else if(ch<='9'&&ch>='0') {
num++;
}
else if (ch <= 'Z' && ch >= 'A') {
c2++;
}
else if (ch==' ') {
space++;
}
else {
other++;
}
}
printf("%d %d %d %d %d",c1,c2,num,space,other);
}
第五题,不是最优解,很容易越界
#include<stdio.h>
int isNum(int n) {
while (n) {
if (n % 10 > 1)
return 0;
else n /= 10;
}
return 1;
}
int main(){
int N;
int M;
scanf("%d", &N);
for (int i = 2;; i++) {
if (isNum(i * N)) {
M = i * N;
printf("%d\n", M);
break;
}
}
}
第六题
#include<stdio.h>
int MaxSubsequence(int* a) {
int sum = a[0];
int maxsum = a[0];
for (int i = 1; i < 11; i++) {
sum += a[i];
printf("%d\n", sum);
if (sum < 0) {
sum = 0;
}
else if (sum > maxsum)
{
maxsum = sum;
}
}
return maxsum;
}
int main() {
int a[11];
for (int i = 0; i < 11; i++) {
scanf("%d",&a[i]);
}
for (int i = 0; i < 11; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
|