C语言——PTA

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

#include <stdio.h>
#include <string.h>
int main(){
    char s[1000];
    int sum=0;
    int idx=0;
    int a[1000];
    char pingyin[10][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};    
    scanf("%s",&s);
    int len=strlen(s);
    for(int i=0;i<len;i++){
        sum+=s[i]-'0';
    }
    while(sum){
        a[idx++]=sum%10;
        sum/=10;
    } //拆分一个数的位数
    for(int i=idx-1;i>=0;i--){
        if(i!=0){
            printf("%s ",pingyin[a[i]]);
        }
        else{
            printf("%s\n",pingyin[a[i]]);
        }
    }
}

换个格式输出整数

#include <stdio.h>
int main(){
    int n;
    scanf("%d",&n);
    int idx=0;
    int a[3];
    while(n){
        a[idx++]=n%10;
        n=n/10;
    }
    if(idx==3){
        for(int i=0;i<a[2];i++){
            printf("B");
        }
        for(int i=0;i<a[1];i++){
            printf("S");
        }
        for(int i=1;i<=a[0];i++){
            printf("%d",i);
        }
    }
    if(idx==2){
        for(int i=0;i<a[1];i++){
            printf("S");
        }
        for(int i=1;i<=a[0];i++){
            printf("%d",i);
        }
    }
    if(idx==1){
        for(int i=1;i<=a[0];i++){
            printf("%d",i);
        }
    }
}

数组元素循环右移

#include<stdio.h>
int main()
{
    int n,m;
    int i,j;
    int k;
    scanf("%d%d",&n,&m);//n为数的个数,m为右移的个数;
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(j=0;j<m;j++)   //循环m次,每次将数组中的元素向右平移一个位置
    {
        k=a[n-1];  //循环时,先将最后一个数保存,然后将前面的数想后移动一位,即依次从右面移动。
        for(i=n-1;i>0;i--)
        {
            a[i]=a[(i-1+n)%n];//相当于将a[4]赋值给a[5]
        }
        a[0]=k;
    }
    for(i=0;i<n;i++)    //注意输出格式,输出的第一位前面没有空格,最后一位后面也没有空格
    {
        if(i==n-1) 
        	printf("%d",a[i]);
        else
            printf("%d ",a[i]);
    }    
    printf("\n");
    return 0;
}

将句中所有单词的顺序颠倒输出

#include<stdio.h>
#include<string.h>
int main()
{
    char str[90];
    gets(str);
    int len=strlen(str),r=0,h=0;//r为行,h为列 
    char ans[90][90];//ans[0]~ans[r]存放单词
    for (int i=0;i<len;i++){
        if(str[i]!=' '){//不是空格则存放至ans[r][h],并令h++ 
            ans[r][h++]=str[i];
        }else{//是空格则说明一个单词结束,行r增一,列h恢复至0 
            ans[r][h]='\0';//末尾是结束符\0 
            r++;
            h=0;
        }
    }
    for (int i=r;i>=0;i--){//输出单词 
        printf("%s",ans[i]);
        if(i>0){
            printf(" ");
        }
    }
    return 0;
}

一元多项式求导

#include <stdio.h>
int main(){
    int a,b;
    int flag = 0; //flag=0表示是第一对
    while(scanf("%d %d",&a,&b)==2){ //2对2对的输入
        if(b!=0){//b不为0才有输出
            if(flag == 0)printf("%d",a*b);
            else printf(" %d",a*b);
            printf(" %d",b-1);
            flag = 1;
        }
    }
    if(flag==0)printf("0 0");//如果只有一个b为0的对子,则就输出0 0
    return 0;
} 

第m个到第n个素数

#include<stdio.h>
#include<math.h>
int issushu(int x)
{
    for (int i = 2; i <=sqrt(x); i++)
    {
        if (x % i == 0)return 0;
    }
    return 1;
} 
int main()//输出第m个到第n个素数
{
    int m = 0, n = 0, j = 0, k = 0;
    int a[10001];
    scanf("%d %d", &m, &n);
    for (int i = 2; j <= n; i++)
    {
        if (issushu(i))
        {
            a[++j] = i;
        }
    } 
    for (int i = m; i <= n; i++)
    {
        printf("%d", a[i]);
        if (++k % 10 != 0&&i!=n)printf(" ");
        else printf("\n");
    }
}

福尔摩斯的约会

#include <stdio.h>
#include <string.h>
int main(){
int i,flag=0;
char week[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
char s1[60],s2[60],s3[60],s4[60];
scanf("%s",s1);
scanf("%s",s2);
scanf("%s",s3);
scanf("%s",s4);
for(i=0;(s1[i]!='\0')&&(s2[i]!='\0');i++){	//比较前两行
    if(s1[i]==s2[i]){
        if(flag==0&&s1[i]>='A'&&s1[i]<='G'){	//判断星期几
            printf("%s",week[s1[i]-'A']);
            flag=1;
            continue; //继续for的下一次循环
        }
        if(flag==1){ //找到了星期
            if(s1[i]>='0'&&s1[i]<='9'){		//判断小时数 0点到9点
                printf(" 0%c",s1[i]);
                break;
            }
            if(s1[i]>='A'&&s1[i]<='N'){		//10点到23点
                printf(" %d",s1[i]-'A'+10);
                break;
            }
        }
    }
}
for(i=0;(s3[i]!='\0')&&(s4[i]!='\0');i++){	//比较后两行
    if(s3[i]==s4[i]&&s3[i]>='A'&&s3[i]<='z'){	//判断分钟数
        printf(":%02d",i);
        break;
    }
}
return 0;
} 

部分A+B

给定 A=3862767,DA=6,则 A 的“6 部分”P A是 66,因为 A 中有 2 个6。
现给定 A、DA 、B、DB,请编写程序计算 PA+PB。

#include <stdio.h>
int main()
{
    char A[20],B[20];
    char DA,DB;
    int PA=0,PB=0;
    int i,n1,n2;
    int m1=0,m2=0;
    scanf("%s %c %s %c",&A,&DA,&B,&DB);
    n1=DA-'0';
    n2=DB-'0';
    for(i=0;A[i]!='\0';i++)
    {
        if(A[i]==DA)
        {
            m1++;//DA出现的个数
        }
    }
    for(i=0;i<m1;i++)
    {
        PA=PA+n1; 
        n1=n1*10;
    }
    
    for(i=0;B[i]!='\0';i++)
    {
        if(B[i]==DB)
        {
            m2++;
        }
    }
    for(i=0;i<m2;i++)
    {
        PB=PB+n2;
        n2=n2*10;
    }
    printf("%d",PA+PB);
}

锤子剪刀布

#include <stdio.h>
int main() {
    //定义循环的次数
    int count;
    //定义计算相同的次数
    int equals = 0;
    int countAB = 0, countAC = 0, countAJ = 0;
    int countBB = 0, countBC = 0, countBJ = 0;
    //定义接收的字符
    char A, B;
    scanf("%d", &count);
    //边界条件
    if (count > 100000) {
        return 0;
    }
    //循环接收
    for (int i = 0; i < count; ++i) {
        getchar();//getchar  把回车清了 不然获取的时候有问题
        scanf("%c %c", &A, &B);
        if (A > B) {
            if (A == 'C' && B == 'B') {
                //B的 B赢了 计数++
                countBB++;
            } else if (A == 'J' && B == 'C') {
                //B的 C赢了 计数++
                countBC++;
            } else if (A == 'J' && B == 'B') {
                //A的 J赢了 计数++
                countAJ++;
            }
        } else if (A == B) {
            //相等
            equals++;
        } else if (A < B) {
            if (B == 'C' && A == 'B') {
                //A的 B赢了 计数++
                countAB++;
            } else if (B == 'J' && A == 'C') {
                //B的 C赢了 计数++
                countAC++;
            } else if (B == 'J' && A == 'B') {
                //B的 J赢了 计数++
                countBJ++;
            }
        }
    }
    //计算赢的和
    int jiaS = countAB + countAC + countAJ;
    int yiS= countBB + countBC + countBJ;
    printf("%d %d %d\n", jiaS, equals, yiS);
    printf("%d %d %d\n", yiS, equals, jiaS);
    //判断一下计数数字大小 输出结果
    if (countAB >= countAC && countAB >= countAJ) {
        printf("B ");
    }
    if (countAC > countAB && countAC >= countAJ) {
        printf("C ");
    }
    if (countAJ > countAC && countAJ > countAB) {
        printf("J ");
    }
    if (countBB >= countBC && countBB >= countBJ) {
        printf("B");
    }
    if (countBC > countBB && countBC >= countBJ) {
        printf("C");
    }
    if (countBJ > countBC && countBJ > countBB) {
        printf("J");
    }
    return 0;
}

数字漏洞

#include<stdio.h>
int main()
{
    int N, max, min, c = 1;            //max存放由大到小的数,min存放由小到大的数,c存放差
    int i, z, j;
    int a[4];                          //存放该数的四位数字
    scanf("%d", &N);
    while(c != 6174 && c != 0)         //当差为6174或0时退出循环
    {
        max = 0;
        min = 0;
        for(i = 0; i < 4; i++)         //将4位数字存放到数组中
        {
            a[i] = N%10;
            N /= 10;
        }
        for(i = 0; i < 4; i++)         //将四位数字排序(从大到小)
        {
            for(j = i; j < 4; j++)
            {
                if(a[i] < a[j])
                {
                    z = a[j];
                    a[j] = a[i];
                    a[i] = z;
                }
            }
        }
        for(i = 0; i < 4; i++)
        {
            max = max*10+a[i];
        }
        for(i = 3; i >= 0; i--)
        {
            min = min*10+a[i];
        }
        c = max - min;
        N = c;
        printf("%04d - %04d = %04d\n", max, min, c);
    }
    return 0;
}

月饼

#include<stdio.h>
struct node{
 double price;//总售价
 double kc;//库存
}moon[1001];
int main(){
   int n,d;
   scanf("%d%d",&n,&d);//月饼种类,最大需求量
   for(int i=0;i<n;i++){
    scanf("%lf",&moon[i].kc);
   }
    for(int i=0;i<n;i++){
    scanf("%lf",&moon[i].price);
   }
   for(int i=0;i<n;i++){
    moon[i].price=moon[i].price/moon[i].kc;
   }
   for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(moon[i].price<moon[j].price){//按价格从大到小将月饼排列
                double t=moon[i].price;
                moon[i].price=moon[j].price;
                moon[j].price=t;
                double t1=moon[i].kc;
                moon[i].kc=moon[j].kc;
                moon[j].kc=t1;
           }
        }
    }
double w=0;
for(int i=0;i<n;i++){
    if(moon[i].kc<d){//第一个的库存<最大需求量
        w+=moon[i].kc*moon[i].price;//拿出1的所有库存
        d-=moon[i].kc;//最大需求量减少,再循环取第二种月饼
    }
    else{//第一个的库存>最大需求量
        w+=d*moon[i].price;//全用第一个
        break;
    }
}
printf("%.2f",w);
return 0;
}

最大公约数

#include<stdio.h>
int isyue(int a,int b);
int main(){
	int t;
	int a[100],b[100];
	int yue;
	
	scanf("%d",&t);
	for(int i=0;i<t;i++){
		scanf("%d %d",&a[i],&b[i]);
	}
	
	for(int i=0;i<t;i++){
		yue=isyue(a[i],b[i]);
		printf("%d\n",yue);
	}
	return 0;
}
int isyue(int a, int b){  
	while (b != 0) {  
		int temp = b;  
		b = a % b;  
		a = temp;  
	}  
	return a; 
}

整数划分问题

#include <stdio.h>  
int partitionCount = 0;  
void backtrack(int n, int max) {    
	if (n == 0) {  
		partitionCount++;  
		return;  
	}  
	for (int i = max; i > 0; --i) {    
		if (i > n) continue;   
		backtrack(n - i, i);  
	}  
}  
int partition(int n) {  
	partitionCount = 0;  
	backtrack(n, n);  
	return partitionCount;  
}  
int main() {  
	int n;  
	while (scanf("%d", &n) != EOF) {  
		printf("%d\n", partition(n));  
	}  
	return 0;  
}

统计不同数字出现的次数

#include<stdio.h>
int main()
{
    char n[1001];//字符数组代表位数
    gets(n);
    int count[10] = {0};//数组下标代表数字
    int j = 0;
    while(n[j] != '\0')
    {
        count[n[j] - '0']++;
        j++;
    }
    for (int i = 0; i < 10; i++)
    {
        if (count[i] != 0)
        {
            printf("%d:%d\n",i,count[i]);
        }
    }
    return 0;
}

输入两个非负 10 进制整数 A 和 B,输出 A+B 的 D (1<D≤10)进制数。

#include<stdio.h>
int main()
{
    int i=0,j,A,B,C,D,R,change[32];
    scanf("%d%d%d",&A,&B,&D);
    C=A+B;
    if(C==0){
        printf("0");
        return 0;
    }
    while(C!=0)
    {
        R=C%D;
        C=C/D;
        change[i++]=R;
    }
    for(j=i-1;j>=0;j--)
        if(change[j]>=10)
            printf("%c",change[j]+55);
        else
            printf("%d",change[j]);
    return 0;
}

组个最小数

输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0、数字 1、……数字 9 的个数。
在一行中输出能够组成的最小的数。

#include<stdio.h>
int main()
{
    int flag[10];
    int i,j;
    for(i=0;i<10;i++)
        scanf("%d",&flag[i]);//出现0,1,2~的次数
    for(i=1;i<10;i++)//从i=1开始找,检查数字i是否出现过
    {
        if(flag[i]!=0)//找到最小的数字
           {
                printf("%d",i);//输出第一位数字
                flag[i]--;//i的个数-1
                break;
           }
    }
    for(i=0;i<10;i++)
    {
        for(j=0;j<flag[i];j++)//从0到9,若数字有余量,就顺序输出
        {
            printf("%d",i);
        }
    }
    printf("\n");
    return 0;
}

程序运行时间

#include<stdio.h>
#include<string.h>
int main()
{
    int c1,c2,hour,min,sec,secs;
    scanf("%d %d",&c1,&c2);
    // secs=(c2-c1)/100;//以秒为单位
    secs=((c2-c1)/10+5)/10;//要求四舍五入
    hour=secs/3600;//转换为小时
    min=(secs-hour*3600)/60;
    sec=secs-hour*3600-min*60;
    printf("%02d:%02d:%02d",hour,min,sec);
}

打印沙漏

#include <stdio.h>
#include <math.h>
int main()
{
    int n,x,i,j;
    char c;
    scanf("%d %c", &n, &c);
    x = sqrt((n + 1) / 2);    //利用整型的特性将小数去除(x为上层层数)
    for (i = 0; i < x; i++)    //打印上层
    {
        for (j = 0; j < i; j++)
            printf(" ");
        for (j = 0; j < 2 * (x - i) - 1; j++)   //由2x-1演变
            printf("%c", c);
        printf("\n");
    }
    for (i = 2; i <= x; i++)    //打印下层(下层无中心点)
    {
        for (j = 0; j < x - i; j++)
            printf(" ");
        for (j = 0; j < 2 * i - 1; j++)
            printf("%c", c);
        printf("\n");
    }
    printf("%d", n - 2 * x * x + 1);    //给的个数-(2x^2-1)=剩下个数
 
    return 0;
}

人口普查

#include <stdio.h>
#include <string.h>
int main() {
    int n, cnt = 0;
    char name[6] = { 0 }, elder[6] = { 0 }, young[6] = { 0 }, birth[11] = { 0 }, min_birth[] = "1814/09/06", max_birth[] = "2014/09/06";
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s %s", name, birth);
        if (strcmp(birth, "1814/09/06") >= 0 && strcmp(birth, "2014/09/06") <= 0) {
            if (strcmp(birth, min_birth) >= 0) {
                strcpy(min_birth, birth);
                strcpy(young, name);
            }
            if (strcmp(birth, max_birth) <= 0) {
                strcpy(max_birth, birth);
                strcpy(elder, name);
            }
            cnt++;	//合法输入个数
        }
    }
    cnt ? printf("%d %s %s", cnt, elder, young) : printf("0");
    return 0;
}

旧键盘

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int s3[256];
int main(){
	char s[100],s2[100];
	scanf("%s %s",s,s2);
	for(int i=0;i<strlen(s2);i++){
		if(s2[i]>='a'&&s2[i]<='z'){
			s2[i]+='A'-'a';
		}
		s3[(unsigned char)s2[i]]=1;
	}
	for(int i=0;i<strlen(s);i++){
		if(s[i]>='a'&&s[i]<='z'){
			s[i]+='A'-'a';
		}
		if(s3[(unsigned char)s[i]]!=1){ //说明s2中没有
			printf("%c",s[i]);
			s3[(unsigned char)s[i]]=1;//保证只输出1个,这次输出了,下次不再输
		}
	}
	return 0;
}```
## 分解质因数

```c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int isprime(int n)
{
	int i = 0;
	for (i = 2; i <= sqrt(n); i++)//别忘了sqrt要引头文件math.h
	{
		if (n % i == 0)
		{
			return 0;
		}
	}
	return 1;
	
}
int main()
{

	int shu = 0;
	int i = 0;
	int j = 0;
	printf("请输入你想获得质因数的数\n");
	scanf("%d", &shu);
	//2、写一个判断是否为素数的函数isprime()
	//如果是素数直接写出该素数=该素数
	//如果判断为合数,就要用合数分解质因数的方法。
	if (isprime(shu))
	{//为真则为质数,直接输出
		printf("%d=%d\n", i, i);
	}
	else
	{
		//既然进了else就说明是合数了
		int tmp = shu;//tmp为临时保存i的值,防止i在后面的操作中被改变
		printf("%d=", tmp);
		for (j = 2; j < tmp; j++)
		{//这里j从二开始,因为我此时分解合数,最小的质数是2,所以从2开始
			//还应注意的是必须是j<tmp,如果多一个=,那还分解什么合数,直接等于本身*1了
			if (isprime(j))
			{//判断是否为质数,因为分解质因数的前提是因子必须为质数的前提下
				while (tmp % j == 0)
				{
					printf("%d*", j);
					tmp /= j;
					if (isprime(tmp))
					{//一直除以质因子,直到有一次除出来结果是质数,说明分解质因数完毕
						printf("%d", tmp);
						break;
					}
				}
				
			}
		}
		printf("\n");
	}
	return 0;
}

树的层序遍历

#include <stdio.h>  
#include <stdlib.h>  

// 定义二叉树节点结构体  
typedef struct TreeNode {  
	int val;  
	struct TreeNode *left;  
	struct TreeNode *right;  
} TreeNode;  

// 定义队列节点结构体  
typedef struct QueueNode {  
	TreeNode *data;  
	struct QueueNode *next;  
} QueueNode;  

// 定义队列结构体  
typedef struct {  
	QueueNode *front;  
	QueueNode *rear;  
} Queue;  

// 创建队列  
Queue* createQueue() {  
	Queue* q = (Queue*)malloc(sizeof(Queue));  
	q->front = q->rear = NULL;  
	return q;  
}  

// 入队操作  
void enqueue(Queue* q, TreeNode* data) {  
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));  
	newNode->data = data;  
	newNode->next = NULL;  
	if (q->rear == NULL) {  
		q->front = q->rear = newNode;  
	} else {  
		q->rear->next = newNode;  
		q->rear = newNode;  
	}  
}  

// 出队操作  
TreeNode* dequeue(Queue* q) {  
	if (q->front == NULL) {  
		return NULL;  
	}  
	QueueNode* temp = q->front;  
	TreeNode* data = temp->data;  
	q->front = q->front->next;  
	if (q->front == NULL) {  
		q->rear = NULL;  
	}  
	free(temp);  
	return data;  
}  

// 检查队列是否为空  
int isQueueEmpty(Queue* q) {  
	return q->front == NULL;  
}  

// 层序遍历二叉树  
void levelOrder(TreeNode* root) {  
	if (root == NULL) {  
		return;  
	}  
	Queue* q = createQueue();  
	enqueue(q, root);  
	while (!isQueueEmpty(q)) {  
		TreeNode* node = dequeue(q);  
		printf("%d ", node->val);  
		if (node->left) {  
			enqueue(q, node->left);  
		}  
		if (node->right) {  
			enqueue(q, node->right);  
		}  
	}  
	free(q);  
}  

// 创建二叉树节点  
TreeNode* createNode(int val) {  
	TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));  
	node->val = val;  
	node->left = NULL;  
	node->right = NULL;  
	return node;  
}  

// 主函数  
int main() {  
	// 创建二叉树  
	TreeNode* root = createNode(1);  
	root->left = createNode(2);  
	root->right = createNode(3);  
	root->left->left = createNode(4);  
	root->left->right = createNode(5);  
	root->right->left = createNode(6);  
	root->right->right = createNode(7);  
	
	// 层序遍历二叉树  
	printf("Level order traversal: ");  
	levelOrder(root);  
	printf("\n");  
	
	// 释放二叉树内存(此处仅释放了根节点,实际应用中需要递归释放所有节点)  
	free(root);
	return 0;  
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值