当前位置: 代码迷 >> 综合 >> 杭电OJ(HDOJ)之1000——1002、1089——1096
  详细解决方案

杭电OJ(HDOJ)之1000——1002、1089——1096

热度:15   发布时间:2023-12-26 22:18:44.0

1000

#include <stdio.h>int main()
{
    int a,b;while(~scanf("%d %d",&a,&b))   //多次输入a和b。等效于 while(scanf("%d %d",&a,&b)!=EOF) {
    printf("%d\n",a+b);}
}

1001

#include<stdio.h>int main()
{
    int a,b,sum = 0,i=1;while(scanf("%d",&b)!=EOF){
    for(i =1;i<=b;i++)sum+=i;printf("%d\n\n",sum);sum=0;}return 0;
}

1002

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000],b[1000];int sum[10000]={
    0};int c,d,e,f,g,i,n,s;scanf("%d",&n);f=48;s=1;c=n;while(n--){
    scanf("%s",a+1);scanf("%s",b+1);printf("Case %d:\n",s);printf("%s + %s = ",a+1,b+1);d=strlen(a+1);e=strlen(b+1);for(i=1;i<=d;i++)a[i]=a[i]-48;for(i=1;i<=e;i++)b[i]=b[i]-48;if(d==e)		  //数字位数相同的情况 {
    for(i=1;i<=d;i++){
    if(a[i]+b[i]>=10&&i>=2)	{
    sum[i-2]=sum[i-2]+1;//向前面的一个sum加一进位,比如10+1sum[i-1]=(a[i]+b[i]-10);//进一位后再减去10,比如15-1=5}else sum[i-1]=a[i]+b[i];}}else if(d>e)      //数字位数不相同的情况 {
    for(i=1;i<=d-e;i++)sum[i]=a[i];f=1;for(i=d-e+1;i<=d;i++){
    if(a[i]+b[f]>=10){
    sum[i-1]=sum[i-1]+1;sum[i]=a[i]+b[f]-10;if(sum[i-1]>=10){
    for(g=i-1;g>=1;g--)//考虑前面位数为10的进位{
    if(sum[g]>=10){
    sum[g-1]=sum[g-1]+1;sum[g]=sum[g]-10;}}}}elsesum[i]=a[i]+b[f];f++;}}else if(d<e)      //数字位数不相同的情况 {
    for(i=1;i<=e-d;i++)sum[i]=b[i];f=1;for(i=e-d+1;i<=e;i++){
    if(a[f]+b[i]>=10){
    sum[i-1]=sum[i-1]+1;sum[i]=a[f]+b[i]-10;if(sum[i-1]>=10){
    for(g=i-1;g>=1;g--){
    if(sum[g]>=10){
    sum[g-1]=sum[g-1]+1;sum[g]=sum[g]-10;}}}}elsesum[i]=a[f]+b[i];f++;}}if(d==e){
    for(i=0;i<d;i++)printf("%d",sum[i]);sum[0]=0;s++;}if(d>e){
    if(sum[0]==0){
    for(i=1;i<=d;i++)printf("%d",sum[i]);}else{
    for(i=0;i<=d;i++)printf("%d",sum[i]);sum[0]=0;}s++;}if(d<e){
    if(sum[0]==0){
    for(i=1;i<=e;i++)printf("%d",sum[i]);}else{
    for(i=0;i<=e;i++)printf("%d",sum[i]);sum[0]=0;}s++;}if(s!=c+1){
    printf("\n");printf("\n");}elseprintf("\n");}
}

1089

#include<stdio.h>
int main() {
    int i,j;while(~scanf("%d %d",&i,&j))printf("%d\n",i+j);return 0;
}

1090

#include <stdio.h>
int main()
{
    int u;int a,b,sum;scanf ("%d",&u);while (u--){
    scanf ("%d%d",&a,&b);sum=a+b;printf ("%d\n",sum);}return 0;
}

1091

#include<stdio.h>
int main() {
    int i,j;while(~scanf("%d %d",&i,&j)&&(i!=0||j!=0))printf("%d\n",i+j);return 0;
}

1092

#include <stdio.h>int main()
{
    int n, number, sum;while(scanf("%d", &n), n)  //while里面是逗号表达式{
    sum = 0;while (n--){
    scanf("%d", &number);sum += number;}printf("%d\n", sum);}return 0;
}

1093

#include <stdio.h>
int main()
{
    int n, number, sum, u ;scanf ("%d",&u);while (u--){
    sum = 0;scanf("%d", &n);while(n--){
    scanf("%d", &number);sum += number;    }printf("%d\n", sum);}return 0;
}

1094

#include <stdio.h>
int main()
{
    int n, a, sum, i;while(scanf("%d", &n) != EOF){
    sum = 0;for(i = 0; i < n; i ++){
    scanf("%d", &a);sum = sum + a;}printf("%d\n", sum);}return 0;
}

1095

#include<stdio.h>
int main()
{
    int a,b;while((scanf("%d%d",&a,&b)!=EOF))printf("%d\n\n",a+b);return 0;
}

1096

#include<stdio.h>int main()
{
    int n,N,a,sum;scanf("%d",&N);while(N--){
    scanf("%d",&n);sum=0;while(n--){
    scanf("%d",&a);sum+=a;}printf("%d\n",sum);if(N!=0)printf("\n");//陷阱}
}
  相关解决方案