信息学奥赛第五页(C语言版)

/*1142:单词的长度
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
int main()
{
    char str[MAX_LEN];
    int len, i;
    fgets(str, MAX_LEN, stdin);
    len = strlen(str);
    for (i = 0; i < len; i++) {
        if (str[i] == ' ') {
            continue;
        }
        int word_len = 0;
        while(str[i] != ' ' && i < len){
            word_len++;
            i++;
            if (str[i] == '\n'){
            	word_len--;
			}
        }
		if(i < len - 1){
			printf("%d,", word_len);
		}else{
			printf("%d", word_len);
		}
        
    }
    printf("\n");
    return 0;
}

1143:最长最短单词
法一:(错误)
#include<stdio.h>
#include<string.h>
#define WORD_NUM 200
#define WORD_LENGTH 100
int main(){
	int i, j, length[WORD_NUM], count = 0, word_count = 0, m = 0;
	char str[WORD_NUM*WORD_LENGTH];
	fgets(str, WORD_NUM*WORD_LENGTH, stdin);
	int len = strlen(str);
	for (i = 0; i < len; i++){
		if (str[i] != ' ' && str[i] != ',' && str[i] != '\n'){
			count++;
		}
		if (str[i] == ' ' || str[i] == ',' || str[i] == '\n'){
			length[word_count] = count;
			word_count++;
			count = 0;
		}
	}
	char word[word_count][WORD_LENGTH];
	for (i = 0; i < word_count; i++){
		for (j = 0; j < length[i]; j++){
			char ch = str[m];
			if (ch != ' ' && ch != ',' && ch != '\n'){
				word[i][j] = ch;
				m++;
			}
			if (ch == ' ' || ch == ','){
				break;
			}
		}
		m = m + 1;
	}
	int location_max = 0, location_min = 0;
	for (i = 0; i < word_count; i++){
		if (length[location_max] < length[i]){
			location_max = i;
		}
		if (length[location_min] > length[i]){
			location_min = i;
		}
	}
	for (i = 0; i < length[location_max]; i++){
		printf("%c", word[location_max][i]);
	}
	printf("\n");
	for (i = 0; i < length[location_min]; i++){
		printf("%c", word[location_min][i]);
	}
}
法二:
#include <iostream>
#include <string>
using namespace std;
int main()
{
  string str1;
  getline(cin,str1);
  int n=0;
  string MaxArr="";
  string MinArr=str1;
  string arr="";
  for(int i=0;i<str1.length();i++){
    if(str1[i]==' ' or str1[i]==','){
      while (str1[i+1]==' ' or str1[i+1]==',')
      {
        i++;
      }
      if(n>MaxArr.length()){
        MaxArr=arr; // 更新最长的单词
      }
      if(n<MinArr.length()){
        MinArr=arr; // 更新最短的单词
      }
      arr="";
      n=0;
    }else{
      arr.append(1,str1[i]); // 逐字符构建当前的单词
      n++;
    }
  }
  if(n>MaxArr.length()){
      MaxArr=arr; // 更新最长的单词(处理最后一个单词情况)
  }
  if(n<MinArr.length()){
      MinArr=arr; // 更新最短的单词(处理最后一个单词情况)
  }
  cout<<MaxArr<<endl; // 输出最长的单词
  cout<<MinArr<<endl; // 输出最短的单词
}
1144:单词翻转
//法一:
#include<stdio.h>
#include<string.h>
#define MAX_LEN 500
int main(){
	int n = 0, i = 0, j = 0;
	char str[MAX_LEN];
	fgets(str,MAX_LEN,stdin);
	while(str[i]){
        if(str[i] == ' '){
        	for(j = i-1; j > i - n - 1; j--){
        		printf("%c", str[j]);
			}
			i++;
			printf(" ");
			n = 0;
		}
		else{
            n++;
            i++;
		}
		if (str[i] == '\n'){
			for(j=i-1;j>i-n-1;j--){
			printf("%c",str[j]);
			}
		}
	}
	return 0;
}
//法二:
#include <stdio.h>
#include <string.h>
#define MAX_LEN 500
int main() {
    char str[MAX_LEN];
    int i, j, len, count_char = 0, count = 0, num[MAX_LEN], m = 0, flag = 0;
    fgets(str, MAX_LEN, stdin);
    len = strlen(str);
	for (i = 0; i < len; i++){
		if (str[i] != ' ' && str[i] != '\n'){
			count_char++;
		}
		if (str[i] == ' ' || str[i] =='\n'){
			num[count] = count_char;
			count++;
			count_char = 0;
		}
	}
	char word[count][MAX_LEN];
	for (i = 0; i < count; i++){
		for (j = 0; j < num[i]; j++){
			char ch = str[m];
			if (ch != ' '){
				word[i][j] = ch;
				m++;
			}else{
				break;
			}
		}
		m = m + 1;
	}
	for (i = 0; i < count; i++){
		for (j = num[i]; j > 0; j--){
			printf("%c", word[i][j - 1]);
		}
		printf(" ");
	}
    return 0;
}
1145:字符串p型编码
#include<stdio.h>
#include<string.h>
int main(){
	int i, j, count;
	char str[1000];
	fgets(str, 1000, stdin);
	int len = strlen(str);
	for (i = 0; i < len - 1; i++){
		count = 0;
		for (j = i; j < len; j++){
			if (str[i] == str[j]){
				count++;
			}else{
				i = j - 1;
				break;
			}
		}
		printf("%d%c", count, str[j-1]);
	}
	return 0;
}
1146:判断字符串是否为回文
#include<stdio.h>
#include<string.h>
int main(){
	char str[100], change[100];
	fgets(str, 100, stdin);
	int i, len = strlen(str), count = 0;
	for (i = len - 2; i >= 0; i--){
		change[len - 2 - i] = str[i];
	}
	int flag = 0;
	for (i = 0; i < len - 1; i++){
		if (str[i] != change[i]){
			flag = 1;
		}
	}
	if (flag == 1){
		printf("no");
	}else{
		printf("yes");
	}
	return 0;
}
1147:最高分数的学生姓名
#include <stdio.h>
#include <string.h>
int main() {
    int i, n, max = 0, score_max = 0;
    char str_max[100] = "name";
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        int score;
        char str[100];
        scanf("%d %s", &score, str);
        if (score > score_max) {
            score_max = score;
            strcpy(str_max, str);
        }
    }
    printf("%s", str_max);
    return 0;
}
1148:连续出现的字符
#include<stdio.h>
#include<string.h>
int main(){
	int i, j, count, k;
	char str[2500];
	scanf("%d", &k);
	getchar();
	fgets(str, 2500, stdin);
	int len = strlen(str), flag = 0;
	for (i = 0; i < len; i++){
		count = 0;
		for (j = i; j  < len; j++){
			if (str[i] == str[j]){
				count++;
			}else{
				break;
			}
		}
		i = j;
		if (count >= k){
			flag = 1;
			printf("%c", str[j-1]);
			break;
		}
	}
	if (flag == 0){
		printf("No");
	}
	return 0;
}
1149:最长单词2
#include <stdio.h>
#include <string.h>

int main() {
    char str[510];
    fgets(str, sizeof(str), stdin);
    int len = strlen(str) - 1;
    int max_length = 0;
    char longest_word[100];
    char current_word[100];
    int j = 0, i;
    for (i = 0; i < len; i++) {
        if (str[i] != ' ' && str[i] != '.') {
            current_word[j] = str[i];
            j++;
        } else {
            current_word[j] = '\0'; // 在单词末尾添加字符串结束符
            if (j > max_length) {
                max_length = j;
                strcpy(longest_word, current_word);
            }
            j = 0; // 重置单词字符索引
        }
    }
    printf("%s\n", longest_word);
    return 0;
}
1150:求正整数2和n之间的完全数
#include<stdio.h>
int main(){
	int i, j, n, sum;
	scanf("%d", &n);
	for (i = 2; i <= n; i++){
		sum = 0;
		for (j = 1; j < i; j++){
			if (i % j == 0){
				sum += j;
			}
		}
		if (sum == i){
			printf("%d\n", i);
		}
	}
	return 0;
}
1151:素数个数
#include<stdio.h>
int main(){
	int i, j, n, count = 0;
	scanf("%d", &n);
	for (i = 2; i <= n; i++){
		int flag = 1;
		for (j = 2; j < i; j++){
			if (i % j == 0){
				flag = 0;
				break;
			}
		}
		if (flag == 1){
			count++;
		}
	}
	printf("%d", count);
}
1152:最大数max(x,y,z)
#include<stdio.h>
double MAX(double a, double b, double c){
	int max = a;
	if (max < b){
		max = b;
		if (max < c){
			max = c;
		}
	}else{
		if (max < c){
			max = c;
		}
	}
	return max;
}
int main(){
	double m, a, b, c;
	scanf("%lf %lf %lf", &a, &b, &c);
	double i = MAX(a, b, c);
	double j = MAX(a + b, b, c);
	double k = MAX(a, b, b + c);
	m = i / (j * k);
	printf("%.3lf", m);
	return 0;
}
1153:绝对素数
#include<stdio.h>
int IF_P(int a){
	int i;
	for (i = 2; i < a; i++){
		if (a % i == 0){
			return -1;
			break;
		}
	}
	return 1;
}
int main(){
	int i, num, num_change, deg_1, deg_2;
	for (i = 10; i <= 99; i++){
		num = i;
		deg_1 = num % 10;
		deg_2 = num / 10;
		num_change = deg_1 * 10 + deg_2;
		if (IF_P(num) == 1 && IF_P(num_change) == 1){
			printf("%d\n", num);
		}
	}
	return 0;
}
1154:亲和数
#include <stdio.h>
int SUM(int a) {
    int sum = 0, j;
    for (j = 1; j < a; j++) {
        if (a % j == 0) {
            sum += j;
        }
    }
    return sum;
}

int main() {
	int i;
    for (i = 1;; i++) {
        int b = SUM(i);
        if (SUM(b) == i && i < b) {
            printf("%d %d\n", i, b);
            break;
        }
    }
    return 0;
}
1155:回文三位数
#include<stdio.h>
int IF_P(int a){
	int i;
	for (i = 2; i < a; i++){
		if (a % i == 0){
			return -1;
			break;
		}
	}
	return 1;
}
int main(){
	int i, deg_1, deg_3;
	for (i = 100; i <= 999; i++){
		deg_1 = i % 10;
		deg_3 = i / 100;
		if (deg_1 == deg_3 && IF_P(i) == 1){
			printf("%d\n", i);
		}
	}
	return 0;
}
1156:求π的值
#include <stdio.h>
#include <math.h>
double f(double x)
{
    double s = 0, t = x;
    int i = 1;
    while (fabs(t / i) >= 1e-6) 
    {
        s += t / i;        
        t = -1 * x * x * t; 
        i += 2;             
    }
    return s;
}
int main()
{
    double pi;
    pi = 6 * (f(1 / sqrt(3)));  
    printf("%.10lf\n", pi);   
    return 0;
}
1157:哥德巴赫猜想
#include <stdio.h>
#include <math.h>
int judge(int x);
int main()
{
    int x;
    int i;
    for (x = 6; x <= 100; x += 2)
    {
        for (i = 2; i <= x / 2; i++)
        {
            if (judge(i) && judge(x - i))
            {
                printf("%d=%d+%d\n", x, i, x - i);
                break;                           
            }
        }
    }
    return 0;
}
int judge(int x)
{
    int i = 2;
    while (i <= floor(sqrt(x)) && (x % i != 0))
        i++;
    if (i > floor(sqrt(x)))
        return 1;
    return 0;
}
1397:简单算术表达式求值
#include <stdio.h>
int arith(int x, char c, int y)
{
    switch(c)
    {
        case '+': return x + y;
        case '-': return x - y;
        case '*': return x * y;
        case '/': return x / y;
        case '%': return x % y;
    }
}
int main()
{
    int a, b;
    char c;
    scanf("%d%c%d", &a, &c, &b); //scanf接受字符不需要空格隔开
    printf("%d", arith(a, c, b));
    return 0;
}
1398:短信计费
#include<stdio.h>
int main(){
	int n, i, count = 0;
	scanf("%d", &n);
	for (i = 1; i <= n ;i ++){
		int num_char;
		scanf("%d", &num_char);
		if (num_char % 70 != 0){
			count += num_char / 70 + 1;
		}
		if (num_char % 70 == 0){
			count += num_char / 70;
		}
		
	}
	printf("%.1f", count * 0.1);
	return 0;
}
1399:甲流病人初筛
#include <stdio.h>
#include <string.h>
int main()
{
    int n, i, count = 0;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
		char str[100];
		double temp;
		int flag;
        scanf("%s %lf %d", str, &temp, &flag);
        if (temp >= 37.5 && flag == 1)
        {
            printf("%s\n", str);
            count++;
        }
    }
    printf("%d", count);
    return 0;
}
1400:统计单词数
法一:(×)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
    int t = 0, t1 = 0, i;
    char s[1000], ss[100];
    fgets(ss, sizeof(ss), stdin);
    ss[strcspn(ss, "\n")] = '\0';
    fgets(s, sizeof(s), stdin);
    s[strcspn(s, "\n")] = '\0';
    int l2 = strlen(ss);
    int l = strlen(s);
    for (i = 0; i <= l - l2; i++)
    {
        int j;
        for (j = 0; j < l2; j++)
        {
            if (toupper(s[i + j]) != toupper(ss[j]))
                break;
            if (i > 0 && s[i - 1] != ' ')
                break;
            if (j == l2 - 1 && (s[i + j + 1] == ' ' || i + j == l))
            {
                t++;
                if (t == 1)
                    t1 = i;
            }
        }
    }
    if (t == 0)
        printf("-1");
    else
        printf("%d %d", t, t1);
    return 0;
}
法二:
#include<bits/stdc++.h>
using namespace std;
string s_a, s_w, s_t;
string lower(string s)
{
	for(int i = 0; i < s.length(); ++i)
		if(s[i] >= 'A' && s[i] <= 'Z')
			s[i] += 32;
	return s;
}
int main()
{
	int k = 0, firstPos = -1, ct = 0, b;
	cin >> s_w;
	cin.get();
	getline(cin, s_a);
	s_w = lower(s_w);
	s_a = lower(s_a);
	b = 0;
	for(int i = 0; i <= s_a.length(); ++i)
	{
		if(s_a[i] == ' ' || s_a[i] == '\0')
		{
			s_t = s_a.substr(b, i - b);
			if(s_t == s_w)
			{
				if(firstPos == -1)
					firstPos = b;
				ct++;
			}
			b = i + 1;
		}
	}
	if(firstPos == -1)
		cout << -1;
	else
		cout << ct << ' ' << firstPos;
    return 0;
}
1401:机器翻译
法一:
#include <stdio.h>
int main()
{
    int M, N, i, j;
    scanf("%d %d", &M, &N);
    int words[N];
    for (i = 0; i < N; i++)
    {
        scanf("%d", &words[i]);
    }
    int memory[M];
    int translations = 0;
    for (i = 0; i < N; i++)
    {
        int word = words[i];
        int found = 0;
        for (j = 0; j < M; j++)
        {
            if (memory[j] == word)
            {
                found = 1;
                break;
            }
        }
        if (!found)
        {
            translations++;

            if (translations <= M)
            {
                memory[translations - 1] = word;
            }
            else
            {
                for (j = 0; j < M - 1; j++)
                {
                    memory[j] = memory[j + 1];
                }
                memory[M - 1] = word;
            }
        }
    }
    printf("%d", translations);
    return 0;
}
法二:
#include <stdio.h>
#include <stdlib.h>

#define N 1010

int h[N], idx, len;

int main() {
    int n, m, cnt = 0;
    scanf("%d %d", &m, &n);
    while (n--) {
        int w;
        scanf("%d", &w);
        int i = idx;
        for (i = idx; i < idx + len; i++) {
            if (h[i] == w) break;
        }
        if (i == idx + len) {
            if (len == m) {
                h[idx + len] = w;
                idx++;
            } else {
                h[idx + len] = w;
                len++;
            }
            cnt++;
        }
    }
    printf("%d\n", cnt);
    return 0;
}
1402:Vigenère密码
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
	char k[110], c[1010];
	fgets(k, sizeof(k), stdin);
	fgets(c, sizeof(c), stdin);
	int i, len1, len2;
	len1 = strlen(k) - 1;
	len2 = strlen(c) - 1;
	for (i = 0; i < len2;  i++){
		char ch = k[i%len1];
		if (tolower(c[i]) >= tolower(ch)){
			if (ch >= 'A' && ch <= 'Z'){
				int num = (int)c[i] - ((int)k[i%len1] - 65);
				printf("%c", (char)num);
			}else{
				int num = (int)c[i] - ((int)k[i%len1] - 97);
				printf("%c", (char)num);
			}
		}else{
			if (ch >= 'A' && ch <= 'Z'){
				int num = (int)c[i] + (90 - (int)k[i%len1] + 1);
				printf("%c", (char)num);
			}else{
				int num = (int)c[i] + (122 - (int)k[i%len1] + 1);
				printf("%c", (char)num);
			}
		}
	}
	return 0;
}
1403:素数对
#include <stdio.h>
int zs(int n) {
    if (n <= 1)
        return 0;
	int i;
    for (i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return 0;
    }
    return 1;
}
int main() {
    int i, n, s = 0;
    scanf("%d", &n);
    for (i = 2; i <= n - 2; i++) {
        if (zs(i) && zs(i + 2))
            printf("%d %d\n", i, i + 2);
    }
    return 0;
}
1404:我家的门牌号
#include<stdio.h>
int main(){
	int n, x, y;
	scanf("%d", &n);
	for(y = 1; y <= 100000; ++y)
		for(x = 1; x <= y; ++x)
			if ((1 + y) * y / 2 - 3 * x == n)
			{
				printf("%d %d", x, y);
				return 0;
			}
	return 0;
}
1405:质数的和与积
#include <stdio.h>
#include <math.h>
int primer(int n);
int main()
{
    int i, s, n;
    int x, y;
    scanf("%d", &s);
    n = s / 2;
    for (i = s / 2; i <= s; i++)
    {
        if (primer(i) && primer(s - i))
        {
            x = i;
            y = s - i;
            break;
        }
    }
    printf("%d\n", x * y);
    return 0;
}
int primer(int n)
{
	int k;
    for (k = 2; k <= sqrt(n); k++)
    {
        if (n % k == 0)
            return 0;
    }
    return 1;
}
1406:单词替换
#include <stdio.h>
#include <string.h>
char s[201][101], a[101], b[101];
char space;
int n;
int main()
{
    do {
        n++;
        scanf("%s", s[n]);
        scanf("%c", &space);
    } while (space == ' ');
    scanf("%s %s", a, b);
    int i;
    for (i = 1; i <= n; i++) {
        if (strcmp(a, s[i]) == 0)
            printf("%s ", b);
        else
            printf("%s ", s[i]);
    }
    return 0;
}
1407:笨小猴
#include <stdio.h>
#include <string.h>
int f(int x)
{
	int i;
    for (i = 2; i <= x / 2; i++)
        if (x % i == 0)
            return 0;
    return 1;
}
int main()
{
    char a[101];
    int b[26] = {0}, i;
    scanf("%s", a);
    int len = strlen(a);
    for (i = 0; i < len; i++)
    {
        b[a[i] - 97]++;
    }
    int max = -1, min = 101;
    for (i = 0; i < 26; i++)
    {
        if (b[i] > max)
            max = b[i];
        if (b[i] != 0 && b[i] < min)
            min = b[i];
    }
    if (f(max - min) && (max - min) > 1)
        printf("Lucky Word\n%d\n", max - min);
    else
        printf("No Answer\n0\n");
    return 0;
}
1408:素数回文数的个数
#include <stdio.h>
#include <string.h>
int iss(int x)
{
	int i;
    for (i = 2; i * i <= x; i++)
        if (x % i == 0)
            return 0;
    return 1;
}
int ishws(int y)
{
    char s[21];
    sprintf(s, "%d", y);
    int len = strlen(s), i, j;
    for (i = 0, j = len - 1; i <= len / 2; i++, j--)
        if (s[i] != s[j])
            return 0;
    return 1;
}
int main()
{
	int i, n, cnt = 0;
    scanf("%d", &n);
    for (i = 11; i <= n; i++)
        if (iss(i) && ishws(i))
            cnt++;
    printf("%d", cnt);
    return 0;
}
1409:判决素数个数
#include <stdio.h>
#include <string.h>
char a[201];
int mxx(int y)
{
	int i;
    if (y == 1 || y == 0)
        return 0;
    if (y == 2)
        return 1;
    else
    {
        for (i = 2; i < y; i++)
            if (y % i == 0)
                return 0;
        return 1;
    }
}
int main()
{
    int m, n, i = 0, j = 0, k = 0, max = 0, min = 100;
    char c, x[101], y[101];
    scanf("%d %d", &m, &n);
    max = m > n ? m : n;
    min = m < n ? m : n;
    for (i = min; i <= max; i++)
    {
        if (mxx(i))
            k++;
    }
    printf("%d", k);
    return 0;
}
1410:最大质因子序列
#include <stdio.h>
int m, n;
int iss(int y)
{
	int j;
    for (j = 2; j * j <= y; j++)
    {
        if (y % j == 0)
            return 0;
    }
    return 1;
}
void zym(int x)
{
    int mx = 0, i;
    for (i = 2; i <= x; i++)
    {
        if (x % i == 0 && iss(i) && i > mx) 
            mx = i;
    }
    printf("%d", mx);
    if (x != n)
        printf(","); 
}
int main()
{	int i;
    scanf("%d %d", &m, &n);
    for (i = m; i <= n; i++)
        zym(i);
    return 0;
}
1411:区间内的真素数
#include <stdio.h>
#include <math.h>
int isPrime(int n)
{
	int i;
    if (n < 2)
        return 0;
    for (i = 2; i <= sqrtf(n); ++i)
        if (n % i == 0)
            return 0;
    return 1;
}
int rev(int n)
{
    int num = 0, a;
    for (a = n; a > 0; a /= 10)
        num = num * 10 + a % 10;
    return num;
}
int main()
{
    int m, n, i;
    scanf("%d %d", &m, &n);
    int hasShow = 0;
    for (i = m; i <= n; ++i)
    {
        if (isPrime(i) && isPrime(rev(i)))
        {
            if (hasShow == 1)
                printf(",");
            else
                hasShow = 1;
            printf("%d", i);
        }
    }
    if (hasShow == 0)
        printf("No");
    return 0;
}
1412:二进制分类
#include <stdio.h>
int isClassA(int n)
{
    int s1 = 0, s0 = 0, a;
    for (a = n; a > 0; a /= 2)
    {
        if (a % 2 == 1)
            s1++;
        else
            s0++;
    }
    if (s1 > s0)
        return 1;
    else
        return 0;
}
int main()
{
    int ctA = 0, ctB = 0, i;
    for (i = 1; i <= 1000; ++i)
    {
        if (isClassA(i))
            ctA++;
        else
            ctB++;
    }
    printf("%d %d\n", ctA, ctB);
    return 0;
}
1413:确定进制
#include <stdio.h>
#include <string.h>
long long nbits(int n, int m)
{
    char str[20];
    sprintf(str, "%d", n);
    long long sum = 0, ans = 1, i;
    for (i = strlen(str) - 1; i >= 0; i--)
    {
        if ((str[i] - '0') >= m)
            return -1;
        sum += ans * (str[i] - '0');
        ans *= m;
    }
    return sum;
}
int main()
{
    int p, q, r, i;
    int flag = 0;
    scanf("%d%d%d", &p, &q, &r);
    for (i = 2; i <= 40; i++)
    {
        if ((nbits(p, i) * nbits(q, i)) == nbits(r, i))
        {
            printf("%d\n", i);
            flag = 1;
            break;
        }
    }
    if (!flag)
        printf("0\n");
    return 0;
}
1158:求1+2+3+...
法一:
#include<stdio.h>
int main(){
	int n;
	scanf("%d", &n);
	printf("%d", n * (n + 1) / 2);
	return 0;
}
法二:
#include<stdio.h>
int main(){
	int i, n, sum = 0;
	scanf("%d", &n);
	for (i = 1; i <= n; i++){
		sum += i;
	}
	printf("%d", sum);
	return 0;
}
1159:斐波那契数列
#include<stdio.h>
int FN(int n)
{
	if(n == 1)
	   return 0;
	else if(n == 2)
		   return 1;
	else
		return FN(n - 1) + FN(n - 2);
}
int main()
{
	int n;
	scanf("%d", &n);
	printf("%d", FN(n));
	return 0;
}
1160:倒序数
#include<stdio.h>
int main(){
	int num;
	scanf("%d", &num);
	while (num > 0){
		printf("%d", num % 10);
		num = num / 10;
	}
	return 0;
}
1161:转进制
#include<stdio.h>
char d[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void t(int a, int b)
{
    int r = a % b;
    a = a / b;
    if (a != 0)
    {
        t(a, b);
    }
    printf("%c", d[r]);
}
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    t(a, b);
    printf("\n");
    return 0;
}
1162:字符串逆序
#include<stdio.h>
#include<string.h>
void revShow(char s[], int len, int isFirstExclamation)
{
    if (len == 0)
        return;
    if (isFirstExclamation && s[len-1] == '!')
    {
        revShow(s, len-1, 0);
    }
    else
    {
        printf("%c", s[len-1]);
        revShow(s, len-1, isFirstExclamation);
    }
}
int main()
{
    char s[1005];
    fgets(s, 1005, stdin);
    int len = strlen(s);
    s[--len] = '\0';
    revShow(s, len, 1);
    printf("\n");
    return 0;
}
1163:阿克曼(Ackmann)函数
#include<stdio.h>
int akm(int m,int n)
{
    if(m == 0)
        return n + 1;
    else if(m > 0 && n == 0)
        return akm(m - 1, 1);
    else
        return akm(m - 1, akm(m, n - 1));
}
int main()
{
    int m,n;
    scanf("%d %d", &m, &n);
    akm(m,n);
    printf("%d", akm(m, n));
    return 0;
}
1164:digit函数
#include<stdio.h>
int main(){
	int n, k, i = 1, num;
	scanf("%d %d", &n, &k);
	while(i <= k){
		num = n %10;
		n = n / 10;
		i++;
	}
	printf("%d", num);
	return 0;
}
1165:Hermite多项式
#include<stdio.h>
double h(int n, int x)
{
    if (n == 0)
        return 1;
    if (n == 1)
        return 2 * x;
    if (n > 1)
        return 2 * x * h(n - 1, x) - 2 * (n - 1) * h(n - 2, x);
}
int main()
{
    int n, x;
    double m;
    scanf("%d %d", &n, &x);
    m = h(n, x);
    printf("%.2lf", m);
    return 0;
}
1166:求f(x,n)
#include<stdio.h>
#include<math.h>
double x, n;
double f(double _n, double _x) {
    if(_n == 1)
        return sqrt(1 + _x);
    return sqrt(_n + f(_n - 1, _x));
}
int main() {
    scanf("%lf%lf", &x, &n);
    printf("%.2lf", f(n, x));
    return 0;
}
1167:再求f(x,n)
#include <stdio.h>
double f(double x,double n){
	if(n==1)
		return x/(1+x);
	else
		return x/(n+f(x,n-1));
}
int main() {
	double n,x;
	scanf("%lf%lf",&x,&n);
	printf("%.2lf\n",f(x,n));
	return 0;
}
1307:【例1.3】高精度乘法
#include<stdio.h>
#include<string.h>
#define N 310
char a[N], b[N];
int as[N], bs[N], ans[N], lena, lenb;
void jian() {
	int i;
    for (i = 0; i < lena; i++) as[i] -= bs[i];
    for (i = 0; i < lena; i++) {
        if (as[i] < 0) {
            as[i] += 10;
            as[i + 1]--;
        }
    }
    while (as[lena - 1] == 0 && lena > 0) lena--;
}
int dx() {
	int i;
    if (lena > lenb) return 1;
    if (lena < lenb) return 0;
    for (i = lena - 1; i >= 0; i--) {
        if (as[i] > bs[i]) return 1;
        if (as[i] < bs[i]) return 0;
    }
    return 1;
}
void jy(int k) {
	int i;
    if (k == 0) k = 1;
    if (k != 1) {
        for (i = lenb - 1; i >= 0; i--) {
            bs[i + k - 1] = bs[i];
            bs[i] = 0;
        }
        lenb += k - 1;
    }
    while (dx()) {
        jian();
        ans[k - 1]++;
        int ls = k - 1;
        for (i = k - 1; i <= ls; i++) {
            if (ans[i] > 9) {
                ans[i + 1]++;
                ans[i] -= 10;
                if (ans[i + 1] > 9) ls++;
            }
        }
    }
    if (k != 1) {
        for (i = k - 1; i < lenb; i++) {
            bs[i - k + 1] = bs[i];
            bs[i] = 0;
        }
        lenb -= k - 1;
    }
}
int main() {
	int i;
    scanf("%s%s", a, b);
    if (strcmp(a, b) == 0) {
        printf("1\n0\n");
        return 0;
    }
    lena = strlen(a);
    lenb = strlen(b);
    for (i = 0; i < lena; i++) as[i] = a[lena - i - 1] - '0';
    for (i = 0; i < lenb; i++) bs[i] = b[lenb - i - 1] - '0';
    memset(ans, 0, sizeof(ans));
    int k = lena - lenb;
    if (k < 0) {
        printf("0\n%s\n", a);
        return 0;
    }
    while (k >= 0) {
        if (k == 0 && !dx()) break;
        jy(k);
        k = lena - lenb;
    }
    int h1 = 301, h2 = 301;
    while (ans[h1] == 0 && h1 > 0) h1--;
    while (as[h2] == 0 && h2 > 0) h2--;
    for (i = h1; i >= 0; i--) printf("%d", ans[i]);
    printf("\n");
    for (i = h2; i >= 0; i--) printf("%d", as[i]);
    printf("\n");
    return 0;
}
1309:【例1.6】回文数(Noip1999)
#include<stdio.h>
#include<string.h>
#define N 105
int n, a[N];
void tonum(char s[], int a[]) {
    int len = strlen(s), i;
    for (i = 1; i <= len; ++i) {
        if (s[len - i] >= '0' && s[len - i] <= '9')
            a[i] = s[len - i] - '0';
        else
            a[i] = s[len - i] - 'A' + 10;
    }
    a[0] = len;
}
void genOpp(int a[], int b[]) {
	int i;
    for (i = 1; i <= a[0]; ++i)
        b[i] = a[a[0] + 1 - i];
    b[0] = a[0];
}
void addToA(int a[], int b[]) {
    int c = 0, i;
    for (i = 1; i <= a[0] || i <= b[0]; ++i) {
        a[i] += b[i] + c;
        c = a[i] / n;
        a[i] %= n;
    }
    if (c > 0)
        a[++a[0]] = c;
}
int isPalin(int a[]) {
	int i;
    for (i = 1; i <= a[0] / 2; ++i) {
        if (a[i] != a[a[0] - i + 1])
            return 0;
    }
    return 1;
}
int main() {
    int b[N] = {}, i;
    char s[N];
    scanf("%d%s", &n, s);
    tonum(s, a);
    for (i = 0; i <= 30; ++i) {
        if (isPalin(a)) {
            printf("%d\n", i);
            return 0;
        }
        genOpp(a, b);
        addToA(a, b);
    }
    printf("Impossible\n");
    return 0;
}
1168:大整数加法
#include <stdio.h>
#include <string.h>
int main() {
    int a[100001] = {0}, b[100001] = {0}, c[100001] = {0};
    int lena, lenb, lenc, i, j, x;
    char n1[100001], n2[100001];
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(c, 0, sizeof(c));
    scanf("%s", n1);
    scanf("%s", n2);
    lena = strlen(n1);
    lenb = strlen(n2);
    for (j = 0; j <= lena - 1; j++)
        a[lena - j] = n1[j] - '0';

    for (j = 0; j <= lenb - 1; j++)
        b[lenb - j] = n2[j] - '0';
    i = 1;
    x = 0;
    while (i <= lena || i <= lenb) {
        x = (a[i] + b[i] + c[i]) / 10;
        c[i] = (a[i] + b[i] + c[i]) % 10;
        c[i + 1] += x;
        i++;
    }
    lenc = i;
    while (c[lenc] == 0 && lenc > 1)
        lenc--;
    for (i = lenc; i >= 1; i--)
        printf("%d", c[i]);
    return 0;
}
1169:大整数减法
#include <stdio.h>
#include <string.h>
char ArrString[205], BrrString[205];
int max(int a, int b){
    if(a >= b){
        return a;
    }else{
        return b;
    }
}
int main(){
    int len = 0, i;
    scanf("%s", ArrString);
    scanf("%s", BrrString);
    len = max(strlen(ArrString), strlen(BrrString));
    int ArrInt[205] = {0};
    int BrrInt[205] = {0};
    int CrrInt[205] = {0};
    int Alen = strlen(ArrString);
    int Blen = strlen(BrrString);
    for(i = 0; i < Alen; i++){
        ArrInt[Alen - i] = ArrString[i] - '0';
    }
    for(i = 0; i < Blen; i++){
        BrrInt[Blen - i] = BrrString[i] - '0';
    }
    for(i = 1; i <= len; i++){
        CrrInt[i] += ArrInt[i] - BrrInt[i];
        if(CrrInt[i] < 0){
            CrrInt[i + 1]--;
            CrrInt[i] = 10 + CrrInt[i];
        }
        CrrInt[i] = CrrInt[i] % 10;
    }
    while(CrrInt[len + 1] == 0 && len > 0){
        len--;
    }
    for(i = len + 1; i > 0; i--){
        printf("%d", CrrInt[i]);
    }
}
1170:计算2的N次方
#include <stdio.h>
#include <string.h>
int main() {
    int n, i;
    scanf("%d", &n);
    int arr[100];
    memset(arr, -1, sizeof(arr));
    arr[0] = 1;
    int j;
    for (i = 0; i < n; i++) {
        j = 0;
        while (arr[j] != -1) {
            arr[j++] *= 2;
        }
    	j = 0;
        while (arr[j] != -1) {
            if (arr[j] >= 10 && arr[j + 1] == -1) {
                arr[j + 1] = 0;
            }
            arr[j + 1] += arr[j] / 10;
            arr[j] %= 10;
            j++;
        }
    }
    for (i = j - 1; i >= 0; i--) {
        printf("%d", arr[i]);
    }
    return 0;
}
1171:大整数的因子
#include <stdio.h>
#include <string.h>
int main()
{
    int i, j, k, sum = 0, m, flag = 0;
    char a[40];
    scanf("%s", a);
    for (i = 2; i <= 9; i++)
    {
        k = strlen(a);
        sum = 0;
        for (j = 0; j < k; j++)
        {
            m = (a[j] - '0');
            sum = (sum * 10 + m) % i;
        }
        if (sum == 0)
        {
            printf("%d ", i);
            flag = 1;
        }
    }
    if (flag == 0)
        printf("none");
    return 0;
}*/

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值