求C语言答案

来源:百度知道 编辑:UC知道 时间:2024/05/07 13:53:06
1.Write a program to display a multiplication table with the format shown below.
x 1 2 3 4
---------
1 2 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16

2.Given a sequence of characters of unknown length as program input, write a program to compress repeated characters. The program copies its input to its output, replacing strings of repeating character sequences by [nX], where n is an integer count of the number of repetitions, and Xis the character. Restrict each input is combination of the seven characters: A,B,C,D,E,F,G only. For example, the input:
ABCCCDEEFFFFG
produces the output:
AB[3C]D[2E][4F]G

第一题不知道楼主有没写错。。
第二题
#include<stdio.h>
#include <string.h>
#define N 20
main(){
char a[N],b[N];
int i,j,n=1;
printf("intput:");
scanf("%s",b);/*把字符放进b[]中*/
a[0]=b[0];
for(i=0,j=1;i<(strlen(b)-1);i++)
{
if(b[i]==b[i+1])/*如果字符连续*/
{n++;continue;}
else/*如果字符不连续*/
{
if(n==1)
{a[j++]=b[i+1];}
else
{a[j++]=n+48;a[j++]=b[i+1];}
n=1;
}
}
if(n!=1)
a[j++]=n+48;
a[j]='\0';
printf("output:%s",a);
getchar();
}

第一题~~
#include"stdio.h"
main()
{ int i,j,n,a[80];
scanf("%d",&n);
printf("x ");
for (i=1;i<=n;i++)
{ a[i]=i;
printf("%2d ",a[i]);}
printf("\n");
for (i=1;i<=3*n+1;i++)
pri