开发者

Print without loops [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Printing 1 to 1000 without loop or conditionals

#include <stdio.h>
#include<conio.h>

int fun(int n) {
--n && fun(n);
return printf( "\n%d", n+1);
}

int main(void) {
fun(1000);
get开发者_高级运维ch();
return 0;
}

this is a program which prints 1 to 1000 without using loops or if-else. Are there other ways of doing that not using loops or if-else.


Recursion is really a method of looping. If you just want to avoid the while, for, and if keywords then you could do something nasty like:

int i = 0;
label:
printf("\n%d", n);
n++;
switch (i) {
  case 1000:
    break;
  default:
    goto label;
}


This works well and is pretty fast :)

#include <stdio.h>

int main()
{
    printf("1\n");
    printf("2\n");
    printf("3\n");
    printf("4\n");
    printf("5\n");
    printf("6\n");
    printf("7\n");
    printf("8\n");
    printf("9\n");
    printf("10\n");
    printf("11\n");
    printf("12\n");
    printf("13\n");
    printf("14\n");
    printf("15\n");
    printf("16\n");
    printf("17\n");
    printf("18\n");
    printf("19\n");
    printf("20\n");
    printf("21\n");
    printf("22\n");
    printf("23\n");
    printf("24\n");
    printf("25\n");
    printf("26\n");
    printf("27\n");
    printf("28\n");
    printf("29\n");
    printf("30\n");
    printf("31\n");
    printf("32\n");
    printf("33\n");
    printf("34\n");
    printf("35\n");
    printf("36\n");
    printf("37\n");
    printf("38\n");
    printf("39\n");
    printf("40\n");
    printf("41\n");
    printf("42\n");
    printf("43\n");
    printf("44\n");
    printf("45\n");
    printf("46\n");
    printf("47\n");
    printf("48\n");
    printf("49\n");
    printf("50\n");
    printf("51\n");
    printf("52\n");
    printf("53\n");
    printf("54\n");
    printf("55\n");
    printf("56\n");
    printf("57\n");
    printf("58\n");
    printf("59\n");
    printf("60\n");
    printf("61\n");
    printf("62\n");
    printf("63\n");
    printf("64\n");
    printf("65\n");
    printf("66\n");
    printf("67\n");
    printf("68\n");
    printf("69\n");
    printf("70\n");
    printf("71\n");
    printf("72\n");
    printf("73\n");
    printf("74\n");
    printf("75\n");
    printf("76\n");
    printf("77\n");
    printf("78\n");
    printf("79\n");
    printf("80\n");
    printf("81\n");
    printf("82\n");
    printf("83\n");
    printf("84\n");
    printf("85\n");
    printf("86\n");
    printf("87\n");
    printf("88\n");
    printf("89\n");
    printf("90\n");
    printf("91\n");
    printf("92\n");
    printf("93\n");
    printf("94\n");
    printf("95\n");
    printf("96\n");
    printf("97\n");
    printf("98\n");
    printf("99\n");
    printf("100\n");
    printf("101\n");
    printf("102\n");
    printf("103\n");
    printf("104\n");
    printf("105\n");
    printf("106\n");
    printf("107\n");
    printf("108\n");
    printf("109\n");
    printf("110\n");
    printf("111\n");
    printf("112\n");
    printf("113\n");
    printf("114\n");
    printf("115\n");
    printf("116\n");
    printf("117\n");
    printf("118\n");
    printf("119\n");
    printf("120\n");
    printf("121\n");
    printf("122\n");
    printf("123\n");
    printf("124\n");
    printf("125\n");
    printf("126\n");
    printf("127\n");
    printf("128\n");
    printf("129\n");
    printf("130\n");
    printf("131\n");
    printf("132\n");
    printf("133\n");
    printf("134\n");
    printf("135\n");
    printf("136\n");
    printf("137\n");
    printf("138\n");
    printf("139\n");
    printf("140\n");
    printf("141\n");
    printf("142\n");
    printf("143\n");
    printf("144\n");
    printf("145\n");
    printf("146\n");
    printf("147\n");
    printf("148\n");
    printf("149\n");
    printf("150\n");
    printf("151\n");
    printf("152\n");
    printf("153\n");
    printf("154\n");
    printf("155\n");
    printf("156\n");
    printf("157\n");
    printf("158\n");
    printf("159\n");
    printf("160\n");
    printf("161\n");
    printf("162\n");
    printf("163\n");
    printf("164\n");
    printf("165\n");
    printf("166\n");
    printf("167\n");
    printf("168\n");
    printf("169\n");
    printf("170\n");
    printf("171\n");
    printf("172\n");
    printf("173\n");
    printf("174\n");
    printf("175\n");
    printf("176\n");
    printf("177\n");
    printf("178\n");
    printf("179\n");
    printf("180\n");
    printf("181\n");
    printf("182\n");
    printf("183\n");
    printf("184\n");
    printf("185\n");
    printf("186\n");
    printf("187\n");
    printf("188\n");
    printf("189\n");
    printf("190\n");
    printf("191\n");
    printf("192\n");
    printf("193\n");
    printf("194\n");
    printf("195\n");
    printf("196\n");
    printf("197\n");
    printf("198\n");
    printf("199\n");
    printf("200\n");
    printf("201\n");
    printf("202\n");
    printf("203\n");
    printf("204\n");
    printf("205\n");
    printf("206\n");
    printf("207\n");
    printf("208\n");
    printf("209\n");
    printf("210\n");
    printf("211\n");
    printf("212\n");
    printf("213\n");
    printf("214\n");
    printf("215\n");
    printf("216\n");
    printf("217\n");
    printf("218\n");
    printf("219\n");
    printf("220\n");
    printf("221\n");
    printf("222\n");
    printf("223\n");
    printf("224\n");
    printf("225\n");
    printf("226\n");
    printf("227\n");
    printf("228\n");
    printf("229\n");
    printf("230\n");
    printf("231\n");
    printf("232\n");
    printf("233\n");
    printf("234\n");
    printf("235\n");
    printf("236\n");
    printf("237\n");
    printf("238\n");
    printf("239\n");
    printf("240\n");
    printf("241\n");
    printf("242\n");
    printf("243\n");
    printf("244\n");
    printf("245\n");
    printf("246\n");
    printf("247\n");
    printf("248\n");
    printf("249\n");
    printf("250\n");
    printf("251\n");
    printf("252\n");
    printf("253\n");
    printf("254\n");
    printf("255\n");
    printf("256\n");
    printf("257\n");
    printf("258\n");
    printf("259\n");
    printf("260\n");
    printf("261\n");
    printf("262\n");
    printf("263\n");
    printf("264\n");
    printf("265\n");
    printf("266\n");
    printf("267\n");
    printf("268\n");
    printf("269\n");
    printf("270\n");
    printf("271\n");
    printf("272\n");
    printf("273\n");
    printf("274\n");
    printf("275\n");
    printf("276\n");
    printf("277\n");
    printf("278\n");
    printf("279\n");
    printf("280\n");
    printf("281\n");
    printf("282\n");
    printf("283\n");
    printf("284\n");
    printf("285\n");
    printf("286\n");
    printf("287\n");
    printf("288\n");
    printf("289\n");
    printf("290\n");
    printf("291\n");
    printf("292\n");
    printf("293\n");
    printf("294\n");
    printf("295\n");
    printf("296\n");
    printf("297\n");
    printf("298\n");
    printf("299\n");
    printf("300\n");
    printf("301\n");
    printf("302\n");
    printf("303\n");
    printf("304\n");
    printf("305\n");
    printf("306\n");
    printf("307\n");
    printf("308\n");
    printf("309\n");
    printf("310\n");
    printf("311\n");
    printf("312\n");
    printf("313\n");
    printf("314\n");
    printf("315\n");
    printf("316\n");
    printf("317\n");
    printf("318\n");
    printf("319\n");
    printf("320\n");
    printf("321\n");
    printf("322\n");
    printf("323\n");
    printf("324\n");
    printf("325\n");
    printf("326\n");
    printf("327\n");
    printf("328\n");
    printf("329\n");
    printf("330\n");
    printf("331\n");
    printf("332\n");
    printf("333\n");
    printf("334\n");
    printf("335\n");
    printf("336\n");
    printf("337\n");
    printf("338\n");
    printf("339\n");
    printf("340\n");
    printf("341\n");
    printf("342\n");
    printf("343\n");
    printf("344\n");
    printf("345\n");
    printf("346\n");
    printf("347\n");
    printf("348\n");
    printf("349\n");
    printf("350\n");
    printf("351\n");
    printf("352\n");
    printf("353\n");
    printf("354\n");
    printf("355\n");
    printf("356\n");
    printf("357\n");
    printf("358\n");
    printf("359\n");
    printf("360\n");
    printf("361\n");
    printf("362\n");
    printf("363\n");
    printf("364\n");
    printf("365\n");
    printf("366\n");
    printf("367\n");
    printf("368\n");
    printf("369\n");
    printf("370\n");
    printf("371\n");
    printf("372\n");
    printf("373\n");
    printf("374\n");
    printf("375\n");
    printf("376\n");
    printf("377\n");
    printf("378\n");
    printf("379\n");
    printf("380\n");
    printf("381\n");
    printf("382\n");
    printf("383\n");
    printf("384\n");
    printf("385\n");
    printf("386\n");
    printf("387\n");
    printf("388\n");
    printf("389\n");
    printf("390\n");
    printf("391\n");
    printf("392\n");
    printf("393\n");
    printf("394\n");
    printf("395\n");
    printf("396\n");
    printf("397\n");
    printf("398\n");
    printf("399\n");
    printf("400\n");
    printf("401\n");
    printf("402\n");
    printf("403\n");
    printf("404\n");
    printf("405\n");
    printf("406\n");
    printf("407\n");
    printf("408\n");
    printf("409\n");
    printf("410\n");
    printf("411\n");
    printf("412\n");
    printf("413\n");
    printf("414\n");
    printf("415\n");
    printf("416\n");
    printf("417\n");
    printf("418\n");
    printf("419\n");
    printf("420\n");
    printf("421\n");
    printf("422\n");
    printf("423\n");
    printf("424\n");
    printf("425\n");
    printf("426\n");
    printf("427\n");
    printf("428\n");
    printf("429\n");
    printf("430\n");
    printf("431\n");
    printf("432\n");
    printf("433\n");
    printf("434\n");
    printf("435\n");
    printf("436\n");
    printf("437\n");
    printf("438\n");
    printf("439\n");
    printf("440\n");
    printf("441\n");
    printf("442\n");
    printf("443\n");
    printf("444\n");
    printf("445\n");
    printf("446\n");
    printf("447\n");
    printf("448\n");
    printf("449\n");
    printf("450\n");
    printf("451\n");
    printf("452\n");
    printf("453\n");
    printf("454\n");
    printf("455\n");
    printf("456\n");
    printf("457\n");
    printf("458\n");
    printf("459\n");
    printf("460\n");
    printf("461\n");
    printf("462\n");
    printf("463\n");
    printf("464\n");
    printf("465\n");
    printf("466\n");
    printf("467\n");
    printf("468\n");
    printf("469\n");
    printf("470\n");
    printf("471\n");
    printf("472\n");
    printf("473\n");
    printf("474\n");
    printf("475\n");
    printf("476\n");
    printf("477\n");
    printf("478\n");
    printf("479\n");
    printf("480\n");
    printf("481\n");
    printf("482\n");
    printf("483\n");
    printf("484\n");
    printf("485\n");
    printf("486\n");
    printf("487\n");
    printf("488\n");
    printf("489\n");
    printf("490\n");
    printf("491\n");
    printf("492\n");
    printf("493\n");
    printf("494\n");
    printf("495\n");
    printf("496\n");
    printf("497\n");
    printf("498\n");
    printf("499\n");
    printf("500\n");
    printf("501\n");
    printf("502\n");
    printf("503\n");
    printf("504\n");
    printf("505\n");
    printf("506\n");
    printf("507\n");
    printf("508\n");
    printf("509\n");
    printf("510\n");
    printf("511\n");
    printf("512\n");
    printf("513\n");
    printf("514\n");
    printf("515\n");
    printf("516\n");
    printf("517\n");
    printf("518\n");
    printf("519\n");
    printf("520\n");
    printf("521\n");
    printf("522\n");
    printf("523\n");
    printf("524\n");
    printf("525\n");
    printf("526\n");
    printf("527\n");
    printf("528\n");
    printf("529\n");
    printf("530\n");
    printf("531\n");
    printf("532\n");
    printf("533\n");
    printf("534\n");
    printf("535\n");
    printf("536\n");
    printf("537\n");
    printf("538\n");
    printf("539\n");
    printf("540\n");
    printf("541\n");
    printf("542\n");
    printf("543\n");
    printf("544\n");
    printf("545\n");
    printf("546\n");
    printf("547\n");
    printf("548\n");
    printf("549\n");
    printf("550\n");
    printf("551\n");
    printf("552\n");
    printf("553\n");
    printf("554\n");
    printf("555\n");
    printf("556\n");
    printf("557\n");
    printf("558\n");
    printf("559\n");
    printf("560\n");
    printf("561\n");
    printf("562\n");
    printf("563\n");
    printf("564\n");
    printf("565\n");
    printf("566\n");
    printf("567\n");
    printf("568\n");
    printf("569\n");
    printf("570\n");
    printf("571\n");
    printf("572\n");
    printf("573\n");
    printf("574\n");
    printf("575\n");
    printf("576\n");
    printf("577\n");
    printf("578\n");
    printf("579\n");
    printf("580\n");
    printf("581\n");
    printf("582\n");
    printf("583\n");
    printf("584\n");
    printf("585\n");
    printf("586\n");
    printf("587\n");
    printf("588\n");
    printf("589\n");
    printf("590\n");
    printf("591\n");
    printf("592\n");
    printf("593\n");
    printf("594\n");
    printf("595\n");
    printf("596\n");
    printf("597\n");
    printf("598\n");
    printf("599\n");
    printf("600\n");
    printf("601\n");
    printf("602\n");
    printf("603\n");
    printf("604\n");
    printf("605\n");
    printf("606\n");
    printf("607\n");
    printf("608\n");
    printf("609\n");
    printf("610\n");
    printf("611\n");
    printf("612\n");
    printf("613\n");
    printf("614\n");
    printf("615\n");
    printf("616\n");
    printf("617\n");
    printf("618\n");
    printf("619\n");
    printf("620\n");
    printf("621\n");
    printf("622\n");
    printf("623\n");
    printf("624\n");
    printf("625\n");
    printf("626\n");
    printf("627\n");
    printf("628\n");
    printf("629\n");
    printf("630\n");
    printf("631\n");
    printf("632\n");
    printf("633\n");
    printf("634\n");
    printf("635\n");
    printf("636\n");
    printf("637\n");
    printf("638\n");
    printf("639\n");
    printf("640\n");
    printf("641\n");
    printf("642\n");
    printf("643\n");
    printf("644\n");
    printf("645\n");
    printf("646\n");
    printf("647\n");
    printf("648\n");
    printf("649\n");
    printf("650\n");
    printf("651\n");
    printf("652\n");
    printf("653\n");
    printf("654\n");
    printf("655\n");
    printf("656\n");
    printf("657\n");
    printf("658\n");
    printf("659\n");
    printf("660\n");
    printf("661\n");
    printf("662\n");
    printf("663\n");
    printf("664\n");
    printf("665\n");
    printf("666\n");
    printf("667\n");
    printf("668\n");
    printf("669\n");
    printf("670\n");
    printf("671\n");
    printf("672\n");
    printf("673\n");
    printf("674\n");
    printf("675\n");
    printf("676\n");
    printf("677\n");
    printf("678\n");
    printf("679\n");
    printf("680\n");
    printf("681\n");
    printf("682\n");
    printf("683\n");
    printf("684\n");
    printf("685\n");
    printf("686\n");
    printf("687\n");
    printf("688\n");
    printf("689\n");
    printf("690\n");
    printf("691\n");
    printf("692\n");
    printf("693\n");
    printf("694\n");
    printf("695\n");
    printf("696\n");
    printf("697\n");
    printf("698\n");
    printf("699\n");
    printf("700\n");
    printf("701\n");
    printf("702\n");
    printf("703\n");
    printf("704\n");
    printf("705\n");
    printf("706\n");
    printf("707\n");
    printf("708\n");
    printf("709\n");
    printf("710\n");
    printf("711\n");
    printf("712\n");
    printf("713\n");
    printf("714\n");
    printf("715\n");
    printf("716\n");
    printf("717\n");
    printf("718\n");
    printf("719\n");
    printf("720\n");
    printf("721\n");
    printf("722\n");
    printf("723\n");
    printf("724\n");
    printf("725\n");
    printf("726\n");
    printf("727\n");
    printf("728\n");
    printf("729\n");
    printf("730\n");
    printf("731\n");
    printf("732\n");
    printf("733\n");
    printf("734\n");
    printf("735\n");
    printf("736\n");
    printf("737\n");
    printf("738\n");
    printf("739\n");
    printf("740\n");
    printf("741\n");
    printf("742\n");
    printf("743\n");
    printf("744\n");
    printf("745\n");
    printf("746\n");
    printf("747\n");
    printf("748\n");
    printf("749\n");
    printf("750\n");
    printf("751\n");
    printf("752\n");
    printf("753\n");
    printf("754\n");
    printf("755\n");
    printf("756\n");
    printf("757\n");
    printf("758\n");
    printf("759\n");
    printf("760\n");
    printf("761\n");
    printf("762\n");
    printf("763\n");
    printf("764\n");
    printf("765\n");
    printf("766\n");
    printf("767\n");
    printf("768\n");
    printf("769\n");
    printf("770\n");
    printf("771\n");
    printf("772\n");
    printf("773\n");
    printf("774\n");
    printf("775\n");
    printf("776\n");
    printf("777\n");
    printf("778\n");
    printf("779\n");
    printf("780\n");
    printf("781\n");
    printf("782\n");
    printf("783\n");
    printf("784\n");
    printf("785\n");
    printf("786\n");
    printf("787\n");
    printf("788\n");
    printf("789\n");
    printf("790\n");
    printf("791\n");
    printf("792\n");
    printf("793\n");
    printf("794\n");
    printf("795\n");
    printf("796\n");
    printf("797\n");
    printf("798\n");
    printf("799\n");
    printf("800\n");
    printf("801\n");
    printf("802\n");
    printf("803\n");
    printf("804\n");
    printf("805\n");
    printf("806\n");
    printf("807\n");
    printf("808\n");
    printf("809\n");
    printf("810\n");
    printf("811\n");
    printf("812\n");
    printf("813\n");
    printf("814\n");
    printf("815\n");
    printf("816\n");
    printf("817\n");
    printf("818\n");
    printf("819\n");
    printf("820\n");
    printf("821\n");
    printf("822\n");
    printf("823\n");
    printf("824\n");
    printf("825\n");
    printf("826\n");
    printf("827\n");
    printf("828\n");
    printf("829\n");
    printf("830\n");
    printf("831\n");
    printf("832\n");
    printf("833\n");
    printf("834\n");
    printf("835\n");
    printf("836\n");
    printf("837\n");
    printf("838\n");
    printf("839\n");
    printf("840\n");
    printf("841\n");
    printf("842\n");
    printf("843\n");
    printf("844\n");
    printf("845\n");
    printf("846\n");
    printf("847\n");
    printf("848\n");
    printf("849\n");
    printf("850\n");
    printf("851\n");
    printf("852\n");
    printf("853\n");
    printf("854\n");
    printf("855\n");
    printf("856\n");
    printf("857\n");
    printf("858\n");
    printf("859\n");
    printf("860\n");
    printf("861\n");
    printf("862\n");
    printf("863\n");
    printf("864\n");
    printf("865\n");
    printf("866\n");
    printf("867\n");
    printf("868\n");
    printf("869\n");
    printf("870\n");
    printf("871\n");
    printf("872\n");
    printf("873\n");
    printf("874\n");
    printf("875\n");
    printf("876\n");
    printf("877\n");
    printf("878\n");
    printf("879\n");
    printf("880\n");
    printf("881\n");
    printf("882\n");
    printf("883\n");
    printf("884\n");
    printf("885\n");
    printf("886\n");
    printf("887\n");
    printf("888\n");
    printf("889\n");
    printf("890\n");
    printf("891\n");
    printf("892\n");
    printf("893\n");
    printf("894\n");
    printf("895\n");
    printf("896\n");
    printf("897\n");
    printf("898\n");
    printf("899\n");
    printf("900\n");
    printf("901\n");
    printf("902\n");
    printf("903\n");
    printf("904\n");
    printf("905\n");
    printf("906\n");
    printf("907\n");
    printf("908\n");
    printf("909\n");
    printf("910\n");
    printf("911\n");
    printf("912\n");
    printf("913\n");
    printf("914\n");
    printf("915\n");
    printf("916\n");
    printf("917\n");
    printf("918\n");
    printf("919\n");
    printf("920\n");
    printf("921\n");
    printf("922\n");
    printf("923\n");
    printf("924\n");
    printf("925\n");
    printf("926\n");
    printf("927\n");
    printf("928\n");
    printf("929\n");
    printf("930\n");
    printf("931\n");
    printf("932\n");
    printf("933\n");
    printf("934\n");
    printf("935\n");
    printf("936\n");
    printf("937\n");
    printf("938\n");
    printf("939\n");
    printf("940\n");
    printf("941\n");
    printf("942\n");
    printf("943\n");
    printf("944\n");
    printf("945\n");
    printf("946\n");
    printf("947\n");
    printf("948\n");
    printf("949\n");
    printf("950\n");
    printf("951\n");
    printf("952\n");
    printf("953\n");
    printf("954\n");
    printf("955\n");
    printf("956\n");
    printf("957\n");
    printf("958\n");
    printf("959\n");
    printf("960\n");
    printf("961\n");
    printf("962\n");
    printf("963\n");
    printf("964\n");
    printf("965\n");
    printf("966\n");
    printf("967\n");
    printf("968\n");
    printf("969\n");
    printf("970\n");
    printf("971\n");
    printf("972\n");
    printf("973\n");
    printf("974\n");
    printf("975\n");
    printf("976\n");
    printf("977\n");
    printf("978\n");
    printf("979\n");
    printf("980\n");
    printf("981\n");
    printf("982\n");
    printf("983\n");
    printf("984\n");
    printf("985\n");
    printf("986\n");
    printf("987\n");
    printf("988\n");
    printf("989\n");
    printf("990\n");
    printf("991\n");
    printf("992\n");
    printf("993\n");
    printf("994\n");
    printf("995\n");
    printf("996\n");
    printf("997\n");
    printf("998\n");
    printf("999\n");
    printf("1000\n");
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜